简体   繁体   中英

C++ - Get Windows version

Im trying to get Windows version as result in C++. I have tried codes but it gives me wrong versions. Example:

#include<windows.h>
#include<stdio.h>
int main()
{
     OSVERSIONINFOEX info;
     ZeroMemory(&info, sizeof(OSVERSIONINFOEX));
     info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
     GetVersionEx((LPOSVERSIONINFO)&info);//info requires typecasting

     printf("Windows version: %u.%u\n", info.dwMajorVersion, info.dwMinorVersion);

}

Output: Windows version: 6.2 Im using Windows 10 and NT 6.2 corresponds to Windows 8/8.1. Im using CodeBlocks, Thanks for your replies.

As per the documentation :

With the release of Windows 8.1, the behavior of the GetVersionEx API has changed in the value it will return for the operating system version. The value returned by the GetVersionEx function now depends on how the application is manifested.

Applications not manifested for Windows 8.1 or Windows 10 will return the Windows 8 OS version value (6.2). Once an application is manifested for a given operating system version, GetVersionEx will always return the version that the application is manifested for in future releases. To manifest your applications for Windows 8.1 or Windows 10, refer to Targeting your application for Windows .

(I think what they actually mean by that is that the maximum version number that will be returned is the value you have in your manifest).

If you only want to run on Vista and later, then an easier way to get the Windows version is to use GetProductInfo() . This works without messing around with manifests (although you might want to do that for other reasons).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM