windows 下c++获取操作系统版本名称笔记

windows通过c++获取操作系统主要分两种:
1、windows是8.1版本以下版本:获取操作系统可以通过windows提供的api中GetVersionEx函数来获取
2、windows是8.1版本及以上:获取操作系统主要通过RtlGetNtVersionNumbers法来获取


代码如下:
#include  
#include
#include

#define WINVERSION_2012 4026541440 //Microsoft Windows Server 2012 R2 的BuildNumber号
#define WINVERSION_10 4026546233 //Microsoft Windows 10 的BuildNumber号

using namespace std;
/*
**函数:GetSystemName()
**功能:获取8.1以下版本操作系统名称
*/  
void GetSystemName(string& osname)  
{  
SYSTEM_INFO info;  
GetSystemInfo(&info); 
OSVERSIONINFOEX os;   
os.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);     
 
osname = _T("Unknown Microsoft Windows Version");  
 
if(GetVersionEx((OSVERSIONINFO *)&os))  
{   
//下面根据版本信息判断操作系统名称
cout<<"mainVersion:"< switch(os.dwMajorVersion)//判断主版本号  
{  
case 4:  
switch(os.dwMinorVersion)//判断次版本号   
{   
case 0:  
if(os.dwPlatformId==VER_PLATFORM_WIN32_NT)  
osname =_T("Microsoft Windows NT 4.0"); 
else if(os.dwPlatformId==VER_PLATFORM_WIN32_WINDOWS)  
osname =_T("Microsoft Windows 95");  
break;  
case 10:  
osname =_T("Microsoft Windows 98");  
break;  
case 90:  
osname =_T("Microsoft Windows Me");  
break;  
}  
break;  
 
case 5:  
switch(os.dwMinorVersion)
{   
case 0:  
osname =_T("Microsoft Windows 2000"); 
break;  
 
case 1:  
osname =_T("Microsoft Windows XP");
break;  
 
case 2:  
if(os.wProductType==VER_NT_WORKSTATION   
&& info.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)  
{  
osname =_T("Microsoft Windows XP Professional x64 Edition");  
}  
else if(GetSystemMetrics(SM_SERVERR2)==0)  
osname =_T("Microsoft Windows Server 2003"); 
else if(GetSystemMetrics(SM_SERVERR2)!=0)  
osname =_T("Microsoft Windows Server 2003 R2");  
break;  
}  
break;  
 
case 6:  
switch(os.dwMinorVersion)  
{  
case 0:  
if(os.wProductType == VER_NT_WORKSTATION)  
osname =_T("Microsoft Windows Vista");  
else  
osname =_T("Microsoft Windows Server 2008");   
break;  
case 1:  
if(os.wProductType == VER_NT_WORKSTATION)  
osname =_T("Microsoft Windows 7");  
else  
osname =_T("Microsoft Windows Server 2008 R2");  
break;
case 2:
if(os.wProductType == VER_NT_WORKSTATION)
osname = _T("Microsoft Windows 8");
else  
osname =_T("Microsoft Windows Server 2012");  
break;
}  
break;  
}  
}
}  
/*
**函数:GetSystemNameUp()
**功能:获取8.1以上版本操作系统名称
*/ 
void GetSystemNameUp(string& vname)  
{  
//先判断是否为win8.1或win10  
typedef void(__stdcall*NTPROC)(DWORD*, DWORD*, DWORD*);  
HINSTANCE hinst = LoadLibrary("ntdll.dll");  
DWORD dwMajor, dwMinor, dwBuildNumber;  
NTPROC proc = (NTPROC)GetProcAddress(hinst, "RtlGetNtVersionNumbers");   
proc(&dwMajor, &dwMinor, &dwBuildNumber); 
cout<<"mainVersion:"< if (dwMajor == 6 && dwMinor == 3)   //win 8.1  
{
if(dwBuildNumber == WINVERSION_2012)
{
vname = "Microsoft Windows Server 2012 R2";
}
else
{
vname = "Microsoft Windows 8.1";
}
return;  
}  
else if (dwMajor == 10 && dwMinor == 0)  //win 10  
{
if(dwBuildNumber == WINVERSION_10)
{
vname = "Microsoft Windows 10";
}
else
{
vname = "Microsoft Windows Server 2016";
}
return;

}
int main()
{
string sysName;
GetSystemNameUp(sysName);
cout< if(sysName=="")
{
GetSystemName(sysName);
}
cout< }

你可能感兴趣的:(c++)