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;
}
9
  • 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. Commented Jul 7, 2013 at 16:38
  • @chris: ok, and without preprocessor directives - is it possible? Commented Jul 7, 2013 at 16:39
  • The best I can say is the GetVersion stuff. I'm not sure why you'd get the wrong answer. Commented Jul 7, 2013 at 16:40
  • 1
    @mazix how are you calling the GetVersionEx function? Commented Jul 7, 2013 at 16:41
  • 1
    You're only outputting the major version part. Commented Jul 7, 2013 at 16:47

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.