0

I would like to know if there is any chance to check which Windows version I really use. Something similar to: How do I check OS with a preprocessor directive?.

I tried code from MSDN:

  1. http://msdn.microsoft.com/en-us/library/windows/desktop/ms724451(v=vs.85).aspx
  2. http://msdn.microsoft.com/en-us/library/windows/desktop/ms724439(v=vs.85).aspx

But any of them gave me good results (for example: according to http://msdn.microsoft.com/en-us/library/windows/desktop/ms724832(v=vs.85).aspx my code should print 5.1 when Im on Windows XP but it showed 5 ...)

Is there any reliable way (I would prefer preprocessor directives way) to find out which Windows I'm using?

My code:

#include <windows.h>
#include <iostream>

int main()
{
    OSVERSIONINFO osvi;
    BOOL bIsWindowsXPorLater;

    ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    GetVersionEx(&osvi);

    // I HAD THIS, AND IT WAS WRONG! :<
    std::cout << osvi.dwMajorVersion << "\n";

    // CHANGED THE ABOVE LINE TO THE LINE BELOW AND IT IS OK NOW :D
    std::cout << osvi.dwMajorVersion << "." << osvi.dwMinorVersion << "\n";


    return 0;
}
Community
  • 1
  • 1
mazix
  • 2,540
  • 8
  • 39
  • 56
  • That would be a bit hard to set up with preprocessor directives. There's a reason you have to tell the headers when you need a newer feature. – chris Jul 07 '13 at 16:38
  • @chris: ok, and without preprocessor directives - is it possible? – mazix Jul 07 '13 at 16:39
  • The best I can say is the `GetVersion` stuff. I'm not sure why you'd get the wrong answer. – chris Jul 07 '13 at 16:40
  • @chris: huh, checked it also on Windows 7 and get 6, instead of 6.1. Thats why I dont want to use it – mazix Jul 07 '13 at 16:41
  • 1
    @mazix how are you calling the GetVersionEx function? – Floris Velleman Jul 07 '13 at 16:41
  • You *did* set the `dwOSVersionInfoSize` member to `sizeof()`, right? I get 6.1 on Windows 7. – chris Jul 07 '13 at 16:45
  • 1
    You're only outputting the major version part. – chris Jul 07 '13 at 16:47
  • that explains everything ... :) cheers, guys! – mazix Jul 07 '13 at 16:50
  • Are you trying to get the Windows version that the program is *compiled* on or the version that it's *run* on? You're asking about preprocessor directives, which implies the former, but why should you care where it's compiled? – jamesdlin Jul 07 '13 at 18:54

1 Answers1

2

You are actually getting the right result. But you are only printing the major version:

std::cout << osvi.dwMajorVersion << "\n";

Instead try using:

if (osvi.dwMinorVersion >= 1) {
     std::cout << osvi.dwMajorVersion << "." << osvi.dwMinorVersion << std::endl;
} else {
     std::cout << osvi.dwMajorVersion << std::endl;
}
Floris Velleman
  • 4,848
  • 4
  • 29
  • 46