语言:C/C++
编程软件:VS2015
字符集:UNICODE编码
【1】说明
1、由于GetVersionEx函数在Windows 8.1版本之后,返回的结果全部是6.2,这函数被微软动了手脚的,但是实际上可以利用额外添加一个manifest文件指示兼容新版本.
(参看文章:http://blog.csdn.net/k1988/article/details/47614529)
2、GetVersionEx函数还有一个问题,就是当使用兼容模式打开程序时,它返回的是兼容模式下的系统版本号.
由于以上这两个问题,建议用Version Helper系列的函数或使用直接内核的函数获取系统版本信息.
(文章:https://msdn.microsoft.com/en-us/library/windows/desktop/dn424972(v=vs.85).aspx)
这里直接使用ntdll.dll里的函数,获取系统版本信息.
【2】实现:
//获取系统版本号
#define SYSTEM_VERSION_UNKNOW _T("未知的操作系统")
BOOL GetSystemVersion(LPWSTR version, DWORD lenth)
{
BOOL Wow64;
SYSTEM_INFO info;
//判断是否是在64位系统模拟层上运行
if (!IsWow64Process(GetCurrentProcess(), &Wow64))
return FALSE;
//获取CPU信息
if (Wow64)
GetNativeSystemInfo(&info);
else
GetSystemInfo(&info);
//定义变量
typedef LONG(__stdcall *fnRtlGetVersion)(PRTL_OSVERSIONINFOW lpVersionInformation);
fnRtlGetVersion pRtlGetVersion;
HMODULE hNtdll;
LONG ntStatus;
RTL_OSVERSIONINFOEXW os;
//获取函数
hNtdll = GetModuleHandle(_T("ntdll.dll"));
if (hNtdll == NULL)
return FALSE;
pRtlGetVersion = (fnRtlGetVersion)GetProcAddress(hNtdll, "RtlGetVersion");
if (pRtlGetVersion == NULL)
return FALSE;
//获取系统相关信息
os.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
ntStatus = pRtlGetVersion((PRTL_OSVERSIONINFOW)&os);
if (ntStatus != 0)
return FALSE;
//判断是什么系统
switch (os.dwMajorVersion){
case 4:
{
switch (os.dwMinorVersion) { //Minor
case 0:
{
if (os.dwPlatformId == VER_PLATFORM_WIN32_NT)
wcscpy_s(version, lenth, _T("Microsoft Windows NT 4.0"));
else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
wcscpy_s(version, lenth, _T("Microsoft Windows 95"));
break;
}
case 10:
{
wcscpy_s(version, lenth, _T("Microsoft Windows 98"));
break;
}
case 90:
{
wcscpy_s(version, lenth, _T("Microsoft Windows Me"));
break;
}
default:
{
wcscpy_s(version, lenth, SYSTEM_VERSION_UNKNOW);
break;
}
}
break;
}
case 5:
{
switch (os.dwMinorVersion) {
case 0:
{
wcscpy_s(version, lenth, _T("Microsoft Windows 2000"));
break;
}
case 1:
{
wcscpy_s(version, lenth, _T("Microsoft Windows XP"));
break;
}
case 2:
{
if (os.wProductType == VER_NT_WORKSTATION &&
info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
wcscpy_s(version, lenth, _T("Microsoft Windows XP Professional x64 Edition"));
else if (os.wSuiteMask == VER_SUITE_WH_SERVER)
wcscpy_s(version, lenth, _T("Microsoft Windows Home Server"));
else if (GetSystemMetrics(SM_SERVERR2) == 0)
wcscpy_s(version, lenth, _T("Microsoft Windows Server 2003"));
else if (GetSystemMetrics(SM_SERVERR2) != 0)
wcscpy_s(version, lenth, _T("Microsoft Windows Server 2003 R2"));
break;
}
default:
{
wcscpy_s(version, lenth, SYSTEM_VERSION_UNKNOW);
break;
}
}
break;
}
case 6:
{
switch (os.dwMinorVersion) {
case 0:
{
if (os.wProductType == VER_NT_WORKSTATION)
wcscpy_s(version, lenth, _T("Microsoft Windows Vista"));
else
wcscpy_s(version, lenth, _T("Microsoft Windows Server 2008"));
break;
}
case 1:
{
if (os.wProductType == VER_NT_WORKSTATION)
wcscpy_s(version, lenth, _T("Microsoft Windows 7"));
else
wcscpy_s(version, lenth, _T("Microsoft Windows Server 2008 R2"));
break;
}
case 2:
{
if (os.wProductType == VER_NT_WORKSTATION)
wcscpy_s(version, lenth, _T("Microsoft Windows 8"));
else
wcscpy_s(version, lenth, _T("Microsoft Windows Server 2012"));
break;
}
case 3:
{
if (os.wProductType == VER_NT_WORKSTATION)
wcscpy_s(version, lenth, _T("Microsoft Windows 8.1"));
else
wcscpy_s(version, lenth, _T("Microsoft Windows Server 2012 R2"));
break;
}
}
break;
}
case 10:
{
if (os.dwMinorVersion == 0)
{
if (os.wProductType == VER_NT_WORKSTATION)
wcscpy_s(version, lenth, _T("Microsoft Windows 10"));
else
wcscpy_s(version, lenth, _T("Microsoft Windows Server 2016"));
}
else
{
wcscpy_s(version, lenth, SYSTEM_VERSION_UNKNOW);
}
break;
}
default:
{
wcscpy_s(version, lenth, SYSTEM_VERSION_UNKNOW);
break;
}
}
if ((wcscmp(version, SYSTEM_VERSION_UNKNOW) != 0))
{
if(os.dwBuildNumber != 0)
wsprintf(version, _T("%s.%u"), version, os.dwBuildNumber);
if (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 || info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
wcscat_s(version, lenth, _T(" 64位<基于x64的处理器>"));
else
wcscat_s(version, lenth, _T(" 32位<基于x86的处理器>"));
}
return TRUE;
}
【3】效果: