获取Windows版本 [英] Get Windows Version
问题描述
我怎样才能准确地检测上的本机(C / C ++)项目在Visual Studio 2012.3对操作系统的Windows过去的8.1当前Windows操作系统?
我在MS建议在看向VersionHelperAPI,的但此解决方案仅适用于VS2013 。
以前,我们使用 GetVersionEx()
,但我想要一个更稳定和面向未来的解决方案在某种方式。它看起来像MS正计划在未来弃用此
重要的是,包括在清单文件兼容性标志:
定位您的Windows应用
以下是相关部分:
< compatibility xmlns =urn:schemas-microsoft-com:compatibility.v1>
< application>
<! - Windows 10 - >
< supportedOS Id ={8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}/>
<! - Windows 8.1 - >
< supportedOS Id ={1f676c76-80e1-4239-95bb-83d0f6d0da78}/>
<! - Windows Vista - >
< supportedos Id ={e2011457-1546-43c5-a5fe-008deee3d3f0}/>
<! - Windows 7 - >
< supportedOS Id ={35138b9a-5d96-4fbd-8e2d-a2440225f93a}/>
<! - Windows 8 - >
< supportedOS Id ={4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}/>
< / application>
< / compatibility>没有上面的标签,当在Windows 10上运行时, GetVersionEx $ c $ c>和 VerifyVersionInfoW
会看到Windows 8
Microsoft坚持你应该使用 VerifyVersionInfoW ,而不是 GetVersionEx
有些帮助函数在早期VS版。但您可以在此目录中的计算机上找到它们:
C:\Program Files(x86)\Windows Kits \8.1 \Include\um\VersionHelpers.h
这是他们是我修改它们稍作因为他们太长时间,并测试了它的Windows 10,你应该仔细检查用于Windows的早期版本)
的#define _WIN32_WINNT_NT4的0x0400
的#define _WIN32_WINNT_WIN2K在0x0500
的#define _WIN32_WINNT_WINXP 0x0501
的#define _WIN32_WINNT_WS03 0x0502
的#define _WIN32_WINNT_WIN6为0x0600
的#define _WIN32_WINNT_VISTA为0x0600
#定义_WIN32_WINNT_WS08为0x0600
的#define _WIN32_WINNT_LONGHORN为0x0600
的#define _WIN32_WINNT_WIN7 0x0601
的#define _WIN32_WINNT_WIN8 0x0602
的#define _WIN32_WINNT_WINBLUE 0x0603
的#define _WIN32_WINNT_WIN10 0x0A00
BOOL IsWinVersionOrGreater(DWORD id,WORD wServicePackMajor)
{
WORD wMajorVersion = HIBYTE(id);
WORD wMinorVersion = LOBYTE(id);
OSVERSIONINFOEXW osvi = {sizeof(osvi),0,0,0,0,{0},0,0};
DWORDLONG const dwlConditionMask =
VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(0,VER_MAJORVERSION,VER_GREATER_EQUAL),
VER_MINORVERSION,VER_GREATER_EQUAL),
VER_SERVICEPACKMAJOR,VER_GREATER_EQUAL) ;
osvi.dwMajorVersion = wMajorVersion;
osvi.dwMinorVersion = wMinorVersion;
osvi.wServicePackMajor = wServicePackMajor;
返回VerifyVersionInfoW(安培; osvi,VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR,dwlConditionMask)= FALSE!;
}
BOOL IsWindowsXPOrGreater(){返回IsWinVersionOrGreater(_WIN32_WINNT_WINXP,0);}
BOOL IsWindowsXPSP1OrGreater(){返回IsWinVersionOrGreater(_WIN32_WINNT_WINXP,1);}
BOOL IsWINVersionOrGreater(){return IsWinVersionOrGreater(_WIN32_WINNT_WINXP,3);}
BOOL IsWindowsVistaOrGreater(){return IsWinVersionOrGreater(_WIN32_WINNT_VISTA,0);} $ {// $ isWinVersionOrGreater(){return IsWinVersionOrGreater(_WIN32_WINNT_WINXP,2);}
BOOL IsWindowsXPSP3OrGreater b $ b BOOL IsWindowsVistaSP1OrGreater(){return IsWinVersionOrGreater(_WIN32_WINNT_VISTA,1);}
BOOL IsWindowsVistaSP2OrGreater(){return IsWinVersionOrGreater(_WIN32_WINNT_VISTA,2);}
BOOL IsWindows7OrGreater(){return IsWinVersionOrGreater(_WIN32_WINNT_WIN7,0 );}
BOOL IsWindows7SP1OrGreater(){return IsWinVersionOrGreater(_WIN32_WINNT_WIN7,1);}
BOOL IsWindows8OrGreater(){return IsWinVersionOrGreater(_WIN32_WINNT_WIN8,0);}
BOOL IsWindows8Point1OrGreater(){return IsWinVersionOrGreater (_WIN32_WINNT_WINBLUE,0); }
BOOL IsWindows10OrGreater(){return IsWinVersionOrGreater(_WIN32_WINNT_WIN10,0); }
BOOL IsWindowsServer()
{
OSVERSIONINFOEXW osvi = {sizeof(osvi),0,0,0,0,{0},0,0,VER_NT_WORKSTATION };
DWORDLONG const dwlConditionMask = VerSetConditionMask(0,VER_PRODUCT_TYPE,VER_EQUAL);
return!VerifyVersionInfoW(& osvi,VER_PRODUCT_TYPE,dwlConditionMask);
}
How can I accurately detect the current Windows operating system on a native (C/C++) project in Visual Studio 2012.3 for operating systems past Windows 8.1?
I've looked in to VersionHelperAPI under MS recommendation, but this solution only applies to VS2013.
Previously we used GetVersionEx()
, but I'd like a solution that is more stable and future-proof in a way. It seems like MS is planning to deprecate this in the future.
解决方案 The important thing is to include compatibility flags in manifest file:
Targeting your application for Windows
Here are the relevant parts:
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
<application>
<!-- Windows 10 -->
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
<!-- Windows 8.1 -->
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
<!-- Windows Vista -->
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
<!-- Windows 7 -->
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
<!-- Windows 8 -->
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
</application>
</compatibility>
Without the above tags, when running on Windows 10, GetVersionEx
and VerifyVersionInfoW
will see Windows 8
Microsoft insists you should use VerifyVersionInfoW
, not GetVersionEx
There are some helper functions which are not available in earlier VS version. But you may find them on your computer in this directory:
"C:\Program Files (x86)\Windows Kits\8.1\Include\um\VersionHelpers.h"
Here is what they are (I modified them slightly because they were too long, and tested it for Windows 10, you should double-check for older Windows versions)
#define _WIN32_WINNT_NT4 0x0400
#define _WIN32_WINNT_WIN2K 0x0500
#define _WIN32_WINNT_WINXP 0x0501
#define _WIN32_WINNT_WS03 0x0502
#define _WIN32_WINNT_WIN6 0x0600
#define _WIN32_WINNT_VISTA 0x0600
#define _WIN32_WINNT_WS08 0x0600
#define _WIN32_WINNT_LONGHORN 0x0600
#define _WIN32_WINNT_WIN7 0x0601
#define _WIN32_WINNT_WIN8 0x0602
#define _WIN32_WINNT_WINBLUE 0x0603
#define _WIN32_WINNT_WIN10 0x0A00
BOOL IsWinVersionOrGreater(DWORD id, WORD wServicePackMajor)
{
WORD wMajorVersion = HIBYTE(id);
WORD wMinorVersion = LOBYTE(id);
OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0,{ 0 }, 0, 0 };
DWORDLONG const dwlConditionMask =
VerSetConditionMask(
VerSetConditionMask(
VerSetConditionMask(0, VER_MAJORVERSION, VER_GREATER_EQUAL),
VER_MINORVERSION, VER_GREATER_EQUAL),
VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
osvi.dwMajorVersion = wMajorVersion;
osvi.dwMinorVersion = wMinorVersion;
osvi.wServicePackMajor = wServicePackMajor;
return VerifyVersionInfoW(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR, dwlConditionMask) != FALSE;
}
BOOL IsWindowsXPOrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_WINXP, 0);}
BOOL IsWindowsXPSP1OrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_WINXP, 1);}
BOOL IsWindowsXPSP2OrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_WINXP, 2);}
BOOL IsWindowsXPSP3OrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_WINXP, 3);}
BOOL IsWindowsVistaOrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_VISTA, 0);}
BOOL IsWindowsVistaSP1OrGreater(){ return IsWinVersionOrGreater(_WIN32_WINNT_VISTA, 1);}
BOOL IsWindowsVistaSP2OrGreater(){ return IsWinVersionOrGreater(_WIN32_WINNT_VISTA, 2);}
BOOL IsWindows7OrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_WIN7, 0);}
BOOL IsWindows7SP1OrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_WIN7, 1);}
BOOL IsWindows8OrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_WIN8, 0);}
BOOL IsWindows8Point1OrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_WINBLUE, 0); }
BOOL IsWindows10OrGreater() { return IsWinVersionOrGreater(_WIN32_WINNT_WIN10, 0); }
BOOL IsWindowsServer()
{
OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0,{ 0 }, 0, 0, 0, VER_NT_WORKSTATION };
DWORDLONG const dwlConditionMask = VerSetConditionMask(0, VER_PRODUCT_TYPE, VER_EQUAL);
return !VerifyVersionInfoW(&osvi, VER_PRODUCT_TYPE, dwlConditionMask);
}
这篇关于获取Windows版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!