开发Win32程序时, 有时候需要获取系统版本号,以便判断用户在使用软件时的系统。发现GetVersionEx获取的系统版本号是错的, win10以后的系统获取到的一直是版本号6.2.x,正确的应该是10.x.x什么情况?
查找资料后发现,Windows 8.1, Win10之后,通过GetVersion and GetVersionEx 方法获取WIndows操作系统版本号的功能需要添加manifest文件后才能查找到
实操步骤
1.在项目文件夹里添加一个xxx.manifest文件
内容如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>false</dpiAware>
</asmv3:windowsSettings>
</asmv3:application>
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 and Windows 11 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
</application>
</compatibility>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<!--
UAC settings:
- app should run at same integrity level as calling process
- app does not need to manipulate windows belonging to
higher-integrity-level processes
-->
<requestedExecutionLevel
level="asInvoker"
uiAccess="false"
/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
2.在项目属性-》清单工具-》输入和输出-》附加清单文件 里增加xxx.manifest文件
$(ProjectDir)os_version.manifest
测试代码:
wstring OS::GetOsVer(int& major_ver, int& minor_ver) {
wstring os_name;
wstring ret;
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
DWORD dwMajorVersion, dwMinorVersion, dwPlatformID;
BOOL flag = (BOOL)GetVersionEx((OSVERSIONINFO *)&osvi);
if (!flag) return L"";
//visit 6.0,win7-6.1,win8 6.2, win8.1 6.3, win10有BUG,得到的是6.2(添加manifest文件可解决)
major_ver = osvi.dwMajorVersion;
minor_ver = osvi.dwMinorVersion;
dwPlatformID = osvi.dwPlatformId;
if (GetVersionEx((LPOSVERSIONINFO)&osvi)) {
// 根据主版本号和次版本号来判断操作系统
if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1) {
os_name = L"Windows XP ";// Windows XP
}
else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0) {
os_name = L"Windows 2000"; // Windows 2000
}
else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 0) {
os_name = L"Windows Vista"; // Windows Vista
}
else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 1) {
os_name = L"Windows 7"; // Windows 7
}
else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 2) {
os_name = L"Windows 8"; // Windows 8
}
else if (osvi.dwMajorVersion == 6 && osvi.dwMinorVersion == 3) {
os_name = L"Windows 8.1"; // Windows 8.1
}
else if (osvi.dwMajorVersion == 10 && osvi.dwBuildNumber < 22000) {
os_name = L"Windows 10"; // Windows 10
}
else if (osvi.dwMajorVersion >= 10 && osvi.dwBuildNumber >= 22000) {
// 对于 Windows Server 和更高版本(例如: Win11)
os_name = L"Windows 11";
}
}
return !os_name.empty() ? os_name : L"Unknown Operating System";
}
正确获取到版本号