获取windows操作系统名称和版本

// GetSystemName.cpp : 定义控制台应用程序的入口点。
//


#include "stdafx.h"
#include <string>
#include <Windows.h>
#include <LM.h>


#pragma comment(lib, "netapi32.lib")
#define REG_K_OS_W L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"


void GetSystemNameBy4X(const OSVERSIONINFOEX& os, std::string& strSystemName)
{
switch(os.dwMinorVersion)
{
case 0:
if (os.dwPlatformId == VER_PLATFORM_WIN32_NT)
{
strSystemName = "Microsoft Windows NT 4.0"; // 1996.7发布
}
else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
{
strSystemName = "Microsoft Windows 95";
}
break;
case 10:
strSystemName = "Microsoft Windows 98";
break;
case 90:
strSystemName = "Microsoft Windows Me";
break;
}// switch
}


void GetSystemNameBy5X(const OSVERSIONINFOEX& os, const SYSTEM_INFO& info, std::string& strSystemName)
{
switch(os.dwMinorVersion)
{
case 0:
strSystemName = "Microsoft Windows 2000"; // 1999.12发布
break;
case 1:
strSystemName = "Microsoft Windows XP"; // 2001.8发布
break;
case 2:
if (os.wProductType == VER_NT_WORKSTATION && info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
{
strSystemName = "Microsoft Windows XP Professional x64 Edition";
}
else if (GetSystemMetrics(SM_SERVERR2) == 0)
{
strSystemName = "Microsoft Windows Server 2003"; // 2003.3发布
}
else if (GetSystemMetrics(SM_SERVERR2) != 0)
{
strSystemName = "Microsoft Windows Server 2003 R2";
}
else
{
strSystemName = "Microsoft Windows Home Server";
}
break;
} // switch
}


bool GetWin8dot1Name(std::string& strSystemName)
{
bool bRet = true;
do 
{
HKEY hKey = NULL;
LONG lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
REG_K_OS_W,
0,
KEY_ALL_ACCESS,
&hKey);
if (ERROR_SUCCESS != lResult)
{
if (hKey)
{
lResult = RegCloseKey(hKey);
hKey = NULL;
}
bRet = false;
break;
}


DWORD dwType = 0;
char wszSystemName[MAX_PATH] = {0};
DWORD dwSize = sizeof(wszSystemName) / sizeof(wszSystemName[0]);
lResult = RegQueryValueExA(hKey,
"ProductName",
NULL,
&dwType,
(LPBYTE)wszSystemName,
(DWORD*)&dwSize);
if (ERROR_SUCCESS != lResult)
{
lResult = RegCloseKey(hKey);
hKey = NULL;
bRet = false;
break;
}
strSystemName = wszSystemName;
lResult = RegCloseKey(hKey);
} while (0);


return bRet;
}


void GetSystemNameBy6X(const OSVERSIONINFOEX& os, std::string& strSystemName)
{
switch(os.dwMinorVersion)
{
case 0:
strSystemName = "Microsoft Windows Server 2008";
if (os.wProductType == VER_NT_WORKSTATION)
{
strSystemName = "Microsoft Windows Vista";
}
break;
case 1:
strSystemName = "Microsoft Windows Server 2008 R2";
if (os.wProductType == VER_NT_WORKSTATION)
{
strSystemName = "Microsoft Windows 7";
}
break;
case 2:
{
// 从注册列表中读取系统名
GetWin8dot1Name(strSystemName);
if (strSystemName.empty())
{
strSystemName = "Microsoft Windows Server 2012";
if (VER_NT_WORKSTATION == os.wProductType)
{
strSystemName = "Microsoft Windows 8";
}
}
}
break;
case 3:
strSystemName = "Microsoft Windows Server 2012 R2";
if (VER_NT_WORKSTATION == os.wProductType)
{
strSystemName = "Microsoft Windows 8.1";
}
break;
}// switch
};


void GetSystemNameBy10X(const OSVERSIONINFOEX& os, std::string& strSystemName)
{
switch(os.dwMinorVersion)
{
case 0:
strSystemName = "Microsoft Windows Server 2016";
if (VER_NT_WORKSTATION == os.wProductType)
{
strSystemName = "Microsoft Windows 10";
}
break;
}
}


static bool GetSystemName(std::string& strSystemName)
{
bool bRet = true;


strSystemName.clear();
SYSTEM_INFO info;
ZeroMemory(&info, sizeof(SYSTEM_INFO));
GetSystemInfo(&info);
OSVERSIONINFOEX os;
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);


bool bTrue = GetVersionEx((OSVERSIONINFO*)&os);
HINSTANCE hinst = LoadLibraryA("ntdll.dll");
if (hinst)
{
typedef void (__stdcall *NTPROC)(DWORD*, DWORD*, DWORD*);
NTPROC PRtlGetNtVersionNumbers = (NTPROC)GetProcAddress(hinst, "RtlGetNtVersionNumbers");
if (PRtlGetNtVersionNumbers)
{
PRtlGetNtVersionNumbers(&os.dwMajorVersion, &os.dwMinorVersion, &os.dwBuildNumber);
os.dwBuildNumber &= 0xFFFF;
}
::FreeLibrary(hinst);
hinst = NULL;
}
else
{
WKSTA_INFO_100* pInfo = NULL;
if (NERR_Success == NetWkstaGetInfo(NULL, 100, (LPBYTE*)&pInfo))
{
os.dwMajorVersion = pInfo->wki100_ver_major;
os.dwMinorVersion = pInfo->wki100_ver_minor;
}
}


if (bTrue)
{
switch(os.dwMajorVersion)
{
case 4:
GetSystemNameBy4X(os, strSystemName);
break; // case 4
case 5:
GetSystemNameBy5X(os, info, strSystemName);
break; // case 5
case 6:
GetSystemNameBy6X(os, strSystemName);
break;// case 6
case 10:
GetSystemNameBy10X(os, strSystemName);
break; // case 10
default:
break;
}
}


return bRet;
}


#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
std::string strName;
GetSystemName(strName);
std::cout << strName << std::endl;
return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值