常规使用windows api 查询系统版本号的接口有 GetVersion 和GetVersionEx,但是这两个接口有时无法获取准确的版本号信息,具体原因参考
https://blog.youkuaiyun.com/rankun1/article/details/55224442
因此使用了NetWkstaGetInfo方法去获取系统版本号信息
借鉴了下https://blog.youkuaiyun.com/xiaomucgwlmx/article/details/82656167的代码
修改后代码如下:
#include "stdafx.h"
#include <Windows.h>
#include <string>
#include <iostream>
#include <lm.h>
#pragma comment(lib, "netapi32.lib")
using namespace std;
DWORD PASCAL GetSystemVersion(void)
{
DWORD dwVersion = 0;
WKSTA_INFO_100 *wkstaInfo = NULL;
NET_API_STATUS netStatus = NetWkstaGetInfo(NULL, 100, (BYTE **)&wkstaInfo);
if (netStatus == NERR_Success)
{
DWORD dwMajVer = wkstaInfo->wki100_ver_major;
DWORD dwMinVer = wkstaInfo->wki100_ver_minor;
dwVersion = (DWORD)MAKELONG(dwMinVer, dwMajVer);
NetApiBufferFree(wkstaInfo);
}
return dwVersion;
}
string GetUserSystemVersion()
{
SYSTEM_INFO info; //用SYSTEM_INFO结构判断64位AMD处理器
GetSystemInfo(&info); //调用GetSystemInfo函数填充结构
// 调用window api获取版本信息(只是使用其中的非版本号信息部分,因为版本号可能不准确)
OSVERSIONINFOEX OsVersionInfortion;
OsVersionInfortion.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
GetVersionEx((OSVERSIONINFO*)&OsVersionInfortion);
// 调用 NetWkstaGetInfo 方法获取操作系统版本号
DWORD systemVersion = GetSystemVersion();
char buf[128];
sprintf(buf, "%d.%d", (systemVersion >> 16) & 0xffff, (systemVersion >> 0) & 0xffff);
string strVersion = buf;
string OSystemVersionName = "";
switch (OsVersionInfortion.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
{
if (strVersion == "5.0")
{
OSystemVersionName = "Windows 2000";
}
else if (strVersion == "5.1")
{
OSystemVersionName = "Windows XP";
}
else if (strVersion == "5.2" && (OsVersionInfortion.wProductType == VER_NT_WORKSTATION) && (info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64))
{
OSystemVersionName = "Windows XP Professional x64 Edition";
}
else if (strVersion == "6.0" && OsVersionInfortion.wProductType == VER_NT_WORKSTATION)
{
OSystemVersionName = "Windows Vista";
}
else if (strVersion == "6.1" && OsVersionInfortion.wProductType == VER_NT_WORKSTATION)
{
OSystemVersionName = "Windows 7";
}
else if (strVersion == "6.2" && OsVersionInfortion.wProductType == VER_NT_WORKSTATION)
{
OSystemVersionName = "Windows 8";
}
else if (strVersion == "6.3" && OsVersionInfortion.wProductType == VER_NT_WORKSTATION)
{
OSystemVersionName = "Windows 8.1";
}
else if (strVersion == "10.0" && OsVersionInfortion.wProductType == VER_NT_WORKSTATION)
{
OSystemVersionName = "Windows 10";
}
else
{
OSystemVersionName = "Unknow WindowsVersion,version = " + strVersion;
}
}
break;
default:
{
OSystemVersionName = "dwPlatFormId = " + to_string(OsVersionInfortion.dwPlatformId) + "Version = " + strVersion;
}
break;
}
return OSystemVersionName;
}
int _tmain(int argc, _TCHAR* argv[])
{
string strSystemVersion = GetUserSystemVersion();
cout << "Operate System Version is " << strSystemVersion << endl;
system("pause");
return 0;
}
可以比较准确获取到windows的版本号信息(这里获取的都是非服务端的版本号信息,如需获取全面的,请参考官方文档修改 https://docs.microsoft.com/zh-cn/windows/win32/api/winnt/ns-winnt-osversioninfoexw#requirements)