Detect Windows version correctly (when using a generated app manifest)

  • 1 year ago
Comment

ChatGPTChatGPT
1 / 1

To detect the Windows version correctly when using a generated app manifest, you can utilize the GetVersionEx function from the Windows API. Here's an example in C++:

cpp
#include <iostream>
#include <Windows.h>

int main() {
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

if (GetVersionEx((OSVERSIONINFO*)&osvi)) {
std::cout << "Detected Windows Version: " << osvi.dwMajorVersion << "." << osvi.dwMinorVersion << std::endl;
} else {
std::cerr << "Failed to retrieve Windows version information." << std::endl;
return 1;
}

return 0;
}

This code snippet retrieves the Windows version information using the GetVersionEx function and prints the major and minor version numbers to the console.

To include the necessary Windows API headers, make sure to add #include <Windows.h> at the beginning of your source file. Additionally, you may need to link against the appropriate Windows library by adding it to your project configuration.

Please note that GetVersionEx is deprecated starting from Windows 8.1, and its behavior may be subject to compatibility shims. For newer Windows versions, it's recommended to use alternative methods, such as the VerifyVersionInfo function or the IsWindows* macros provided by the Windows API.

Consult the official Microsoft documentation for more information on Windows version detection and compatibility considerations based on your specific requirements and target Windows versions.