Need a function to extract Windows Name & Version in C/C++
-
I don't need a cross platform solution, i just need a function in C/C++ which will extract name and version of the WINDOWS OS e.g. the prototype should look like below:-
getOSNameAndVersion(std::string &name, std::string &version);
Name can be "Windows 7/8/8.1/10/10.1" etc
And the same is for version.I don't need to cover old windows version like xp, 98 etc etc. The function should not depend on QT at all.
-
I don't need a cross platform solution, i just need a function in C/C++ which will extract name and version of the WINDOWS OS e.g. the prototype should look like below:-
getOSNameAndVersion(std::string &name, std::string &version);
Name can be "Windows 7/8/8.1/10/10.1" etc
And the same is for version.I don't need to cover old windows version like xp, 98 etc etc. The function should not depend on QT at all.
@johndummy I'm kinda confused by the results I personally get, bit this should work:
#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); }
-
GetVersionEx is not a reliable way to get OS version. Rather what your OS tells your app what it is, and that can vary in certain compatibility conditions.
Starting with Windows 8.1 you need a version manifest in your app. Without a manifest GetVersionEx will return 6.2 for any Windows 8 and above.
Even if you use a manifest you get 10.0 for Windows 10 and above. To detect Windows 11 you need to look at build number. Anything above 22000 is Windows 11. There are of course a lot of different versions of 10 and 11 and things will most likely change again with 12.
Of course in compatibility mode scenarios the OS will lie to you and give you different versions, ignoring the manifest if needed.That's basically why detecting OS version is generally not a good idea. Your app shouldn't care. It should care about availability of required features, and that you do by first targeting a defined minimum supported OS version by using appropriate SDK version and then dynamically loading libraries with features from above that version and checking if a query for the specific functions resolve or not.