我有一些代码正在运行以检测Windows XP,我认为应该可以使用但我应该用什么来替换'??'以检测我是否在Windows XP上运行?
bool IsWindowsXP()
{
bool isWindowsXp = false;
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if( GetVersionEx((OSVERSIONINFO*)&osvi) )
{
const DWORD MinXpVersion = ??;
const DWORD MaxXpVersion = ??;
if ((osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(vi.dwMajorVersion >= MinXpVersion) &&
(vi.dwMajorVersion <= MinXpVersion))
{
isWindowsXp = false;
}
}
return isWindowsXp;
}
答案 0 :(得分:5)
在OSVERSIONINFOEX
结构的文档页面上,两个相关字段说明了这一点:
有关详细信息,请参阅备注。
在评论部分向下是一个方便的表:
Operating system Version number dwMajorVersion dwMinorVersion Other Windows 8 6.2 6 2 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION Windows Server 2012 6.2 6 2 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION Windows 7 6.1 6 1 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION Windows Server 2008 R2 6.1 6 1 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION Windows Server 2008 6 6 0 OSVERSIONINFOEX.wProductType != VER_NT_WORKSTATION Windows Vista 6 6 0 OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION Windows Server 2003 R2 5.2 5 2 GetSystemMetrics(SM_SERVERR2) != 0 Windows Home Server 5.2 5 2 OSVERSIONINFOEX.wSuiteMask & VER_SUITE_WH_SERVER Windows Server 2003 5.2 5 2 GetSystemMetrics(SM_SERVERR2) == 0 Windows XP Prof x64 Ed 5.2 5 2 (OSVERSIONINFOEX.wProductType == VER_NT_WORKSTATION) && (SYSTEM_INFO.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64) Windows XP 5.1 5 1 Not applicable Windows 2000 5 5 0 Not applicable
如表中所示,XP为5.1。
答案 1 :(得分:2)
不需要额外的库,标题,也可以使用 VC ++ Express:
BOOL chkxp(){
DWORD version = GetVersion();
DWORD major = (DWORD)(LOBYTE(LOWORD(version)));
DWORD minor = (DWORD)(HIBYTE(LOWORD(version)));
return ((major == 5) && (minor == 1)); // 5.1 is WIN Xp 5.2 is XP x64
}
答案 2 :(得分:0)
SDK有<VersionHelpers.h>
,它提供内联函数来检查Windows版本。从历史上看,许多开发人员都错误地检查了这些检查,因此添加了这些功能以使检查更加万无一失。
特别是IsWindowsXPOrGreater() && !IsWindowsVistaOrGreater()
似乎满足了您的需求。
请注意,使用Windows 10 SDK时,使用GetVersionEx会在编译时生成弃用警告。