Windows 获取系统版本号接口

常规使用windows api 查询系统版本号的接口有 GetVersion 和GetVersionEx,但是这两个接口有时无法获取准确的版本号信息,具体原因参考

 https://blog.csdn.net/rankun1/article/details/55224442

因此使用了NetWkstaGetInfo方法去获取系统版本号信息

借鉴了下https://blog.csdn.net/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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值