DWORD GetNtVersionNumbers()
{
DWORD dwVersion = 0;
HMODULE hModNtdll = NULL;
TCHAR szBuffer[100];
DWORD dwMajorVer, dwMinorVer, dwBuildNumber;
if (hModNtdll = LoadLibrary(TEXT("ntdll.dll")))
{
typedef void (WINAPI* RtlGetNtVersionNumbers)(DWORD*, DWORD*, DWORD*);
RtlGetNtVersionNumbers pfRtlGetNtVersionNumbers;
pfRtlGetNtVersionNumbers = (RtlGetNtVersionNumbers)GetProcAddress(hModNtdll, "RtlGetNtVersionNumbers");
if (pfRtlGetNtVersionNumbers)
{
pfRtlGetNtVersionNumbers(&dwMajorVer, &dwMinorVer, &dwBuildNumber);
dwBuildNumber &= 0xffff;
}
printf("NT VERSION:%ld.%ld.%ld\n", dwMajorVer, dwMinorVer, dwBuildNumber);
FreeLibrary(hModNtdll);
}
return dwVersion;
}
NTSTATUS RtlGetVersion(OSVERSIONINFOW& VersionInformation)
{
UINT_PTR pfn = CalcFunctionAddress(phWCP, pfb_RtlGetVersion);
typedef signed int(__stdcall* fnRtlGetVersion)(OSVERSIONINFOW&);
fnRtlGetVersion g_fnRtlGetVersion = (fnRtlGetVersion) * (UINT_PTR*)pfn;
VersionInformation.dwOSVersionInfoSize = sizeof(OSVERSIONINFOW);
memset(&VersionInformation.dwMajorVersion, 0,
sizeof(VersionInformation.szCSDVersion));
NTSTATUS result = g_fnRtlGetVersion(VersionInformation);
return result;
}
VOID GetWindowsVersion()
{
RTL_OSVERSIONINFOEXW versionInfo;
ULONG majorVersion;
ULONG minorVersion;
ULONG buildVersion;
memset(&versionInfo, 0, sizeof(RTL_OSVERSIONINFOEXW));
versionInfo.dwOSVersionInfoSize = sizeof(RTL_OSVERSIONINFOEXW);
if (!NT_SUCCESS(RtlGetVersion(&versionInfo)))
{
WindowsVersion = WINDOWS_NEW;
return;
}
memcpy(&PhOsVersion, &versionInfo, sizeof(RTL_OSVERSIONINFOEXW));
majorVersion = versionInfo.dwMajorVersion;
minorVersion = versionInfo.dwMinorVersion;
buildVersion = versionInfo.dwBuildNumber;
if (majorVersion == 6 && minorVersion < 1 || majorVersion < 6)
{
WindowsVersion = WINDOWS_ANCIENT;
}
// Windows 7, Windows Server 2008 R2
else if (majorVersion == 6 && minorVersion == 1)
{
WindowsVersion = WINDOWS_7;
}
// Windows 8, Windows Server 2012
else if (majorVersion == 6 && minorVersion == 2)
{
WindowsVersion = WINDOWS_8;
}
// Windows 8.1, Windows Server 2012 R2
else if (majorVersion == 6 && minorVersion == 3)
{
WindowsVersion = WINDOWS_8_1;
}
// Windows 10, Windows Server 2016
else if (majorVersion == 10 && minorVersion == 0)
{
if (buildVersion >= 22500)
{
WindowsVersion = WINDOWS_11_22H1;
}
else if (buildVersion >= 22000)
{
WindowsVersion = WINDOWS_11_21H2;
}
else if (buildVersion >= 19044)
{
WindowsVersion = WINDOWS_10_21H2;
}
else if (buildVersion >= 19043)
{
WindowsVersion = WINDOWS_10_21H1;
}
else if (buildVersion >= 19042)
{
WindowsVersion = WINDOWS_10_20H2;
}
else if (buildVersion >= 19041)
{
WindowsVersion = WINDOWS_10_20H1;
}
else if (buildVersion >= 18363)
{
WindowsVersion = WINDOWS_10_19H2;
}
else if (buildVersion >= 18362)
{
WindowsVersion = WINDOWS_10_19H1;
}
else if (buildVersion >= 17763)
{
WindowsVersion = WINDOWS_10_RS5;
}
else if (buildVersion >= 17134)
{
WindowsVersion = WINDOWS_10_RS4;
}
else if (buildVersion >= 16299)
{
WindowsVersion = WINDOWS_10_RS3;
}
else if (buildVersion >= 15063)
{
WindowsVersion = WINDOWS_10_RS2;
}
else if (buildVersion >= 14393)
{
WindowsVersion = WINDOWS_10_RS1;
}
else if (buildVersion >= 10586)
{
WindowsVersion = WINDOWS_10_TH2;
}
else if (buildVersion >= 10240)
{
WindowsVersion = WINDOWS_10;
}
else
{
WindowsVersion = WINDOWS_10;
}
}
else
{
WindowsVersion = WINDOWS_NEW;
}
}
#include <Windows.h>
#include <string>
#include <stdio.h>
void getSystemName()
{
std::string vname;
//先判断是否为win8.1或win10
typedef void(__stdcall* NTPROC)(DWORD*, DWORD*, DWORD*);
HINSTANCE hinst = LoadLibrary(L"ntdll.dll");
DWORD dwMajor, dwMinor, dwBuildNumber;
NTPROC proc = (NTPROC)GetProcAddress(hinst, "RtlGetNtVersionNumbers");
proc(&dwMajor, &dwMinor, &dwBuildNumber);
if (dwMajor == 6 && dwMinor == 3) //win 8.1
{
vname = "Microsoft Windows 8.1";
printf_s("此电脑的版本为:%s\n", vname.c_str());
return;
}
if (dwMajor == 10 && dwMinor == 0) //win 10
{
vname = "Microsoft Windows 10";
printf_s("此电脑的版本为:%s\n", vname.c_str());
return;
}
//下面判断不能Win Server,因为本人还未有这种系统的机子,暂时不给出
//判断win8.1以下的版本
SYSTEM_INFO info; //用SYSTEM_INFO结构判断64位AMD处理器
GetSystemInfo(&info); //调用GetSystemInfo函数填充结构
OSVERSIONINFOEX os;
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
#pragma warning(disable:4996)
if (GetVersionEx((OSVERSIONINFO*)&os))
{
//下面根据版本信息判断操作系统名称
switch (os.dwMajorVersion)
{ //判断主版本号
case 4:
switch (os.dwMinorVersion)
{ //判断次版本号
case 0:
if (os.dwPlatformId == VER_PLATFORM_WIN32_NT)
vname = "Microsoft Windows NT 4.0"; //1996年7月发布
else if (os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
vname = "Microsoft Windows 95";
break;
case 10:
vname = "Microsoft Windows 98";
break;
case 90:
vname = "Microsoft Windows Me";
break;
}
break;
case 5:
switch (os.dwMinorVersion)
{ //再比较dwMinorVersion的值
case 0:
vname = "Microsoft Windows 2000"; //1999年12月发布
break;
case 1:
vname = "Microsoft Windows XP"; //2001年8月发布
break;
case 2:
if (os.wProductType == VER_NT_WORKSTATION &&
info.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
vname = "Microsoft Windows XP Professional x64 Edition";
else if (GetSystemMetrics(SM_SERVERR2) == 0)
vname = "Microsoft Windows Server 2003"; //2003年3月发布
else if (GetSystemMetrics(SM_SERVERR2) != 0)
vname = "Microsoft Windows Server 2003 R2";
break;
}
break;
case 6:
switch (os.dwMinorVersion)
{
case 0:
if (os.wProductType == VER_NT_WORKSTATION)
vname = "Microsoft Windows Vista";
else
vname = "Microsoft Windows Server 2008"; //服务器版本
break;
case 1:
if (os.wProductType == VER_NT_WORKSTATION)
vname = "Microsoft Windows 7";
else
vname = "Microsoft Windows Server 2008 R2";
break;
case 2:
if (os.wProductType == VER_NT_WORKSTATION)
vname = "Microsoft Windows 8";
else
vname = "Microsoft Windows Server 2012";
break;
}
break;
default:
vname = "未知操作系统";
}
printf_s("此电脑的版本为:%s\n", vname.c_str());
}
else
printf_s("版本获取失败\n");
}
void main()
{
getSystemName();
system("pause");
}
文章评论