C++ 实现获取系统名称

项目中需要用到操作系统名及版本信息:下面是用两种方法实现的

一种是通过查询注册表:


 

#include "stdafx.h"
#include <Windows.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include <strsafe.h>
#include "RegistryControl.h"
using namespace std;
 #pragma warning(disable:4996)

#define  SYS_INFO_PATH1 (L"Software\\Microsoft\\Windows NT\\CurrentVersion")


BOOL SystemNameInfo(wchar_t *wzSystemName,int size)
{
	

	GA::CRegistryControl Reg;
    //打开注册表函数若是64位系统则需要关闭注册表重定向
	if (!Reg.OpenKey(HKEY_LOCAL_MACHINE,SYS_INFO_PATH1,false,IsWow64()))
		return false;

	wchar_t wzProduct[256] = {0};
	DWORD dwBuffSize = sizeof(wzProduct);

	if (!Reg.GetStringValue(L"ProductName", wzProduct, &dwBuffSize))
	{
		Reg.CloseKey();
		return false;
	}
	Reg.CloseKey();

	StringCchCopy(wzSystemName,size,L"Microsoft ");

	StringCchCat(wzSystemName,size,wzProduct);

	StringCchCat(wzSystemName,size,L" ");
	if (IsWow64())
	{
		StringCchCat(wzSystemName,size,L"x64");
	}
	else
	{
		StringCchCat(wzSystemName,size,L"x86");
	}

}

int _tmain(int argc, _TCHAR* argv[])
{

 	wchar_t productname[MAX_PATH];
	SystemNameInfo(productname,MAX_PATH);
	wprintf(L"%s\n",productname);


	getchar();
	getchar();
	return 0;
}

方法二:

通过GetVersionEx和RtlGetNtVersionNumbers 查询系统主次版本号,根据系统版本号返回相应的系统名称:

#include "stdafx.h"
#include <Windows.h>
#include <string>
#include <stdio.h>
#include <iostream>
#include <strsafe.h>
#include "RegistryControl.h"
using namespace std;
 #pragma warning(disable:4996)

#define SM_SERVERR2             89

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

typedef void (WINAPI *PGNSI) (LPSYSTEM_INFO);
typedef BOOL (WINAPI *PGPI)(DWORD,DWORD,DWORD,DWORD,PDWORD);
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS)(HANDLE, PBOOL);
#define BUFSIZE 256

void GetVersionType(UINT uType,wstring & versionType)
{
	wchar_t pszOS[BUFSIZE] = {0};
	switch( uType )
	{
	case PRODUCT_ULTIMATE:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Ultimate "));
		break;
		//case PRODUCT_PROFESSIONAL:
	case 0x00000030:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Professional "));
		break;
	case PRODUCT_HOME_PREMIUM:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Home Premium "));
		break;
	case PRODUCT_HOME_BASIC:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Home Basic "));
		break;
	case PRODUCT_ENTERPRISE:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Enterprise "));
		break;
	case PRODUCT_BUSINESS:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Business "));
		break;
	case PRODUCT_STARTER:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Starter "));
		break;
	case PRODUCT_CLUSTER_SERVER:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Cluster Server "));
		break;
	case PRODUCT_DATACENTER_SERVER:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Datacenter "));
		break;
	case PRODUCT_DATACENTER_SERVER_CORE:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation) " ));
		break;
	case PRODUCT_ENTERPRISE_SERVER:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Enterprise " ));
		break;
	case PRODUCT_ENTERPRISE_SERVER_CORE:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation) " ));
		break;
	case PRODUCT_ENTERPRISE_SERVER_IA64:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems " ));
		break;
	case PRODUCT_SMALLBUSINESS_SERVER:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Small Business Server " ));
		break;
	case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition " ));
		break;
	case PRODUCT_STANDARD_SERVER:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Standard " ));
		break;
	case PRODUCT_STANDARD_SERVER_CORE:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Standard Edition (core installation) " ));
		break;
	case PRODUCT_WEB_SERVER:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Web Server " ));
		break;
	case 0x00000079:
		StringCchCopy(pszOS, BUFSIZE, TEXT("Education " ));
		break;
	default:
		StringCchCopy(pszOS, BUFSIZE,TEXT(" "));
		break;
	}
	versionType = pszOS;
}

BOOL IsWow64()   //判断是否是64位系统
{
	LPFN_ISWOW64PROCESS fnIsWow64Process;
	BOOL bIsWow64 = FALSE;
	fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
	if (NULL != fnIsWow64Process)
	{
		fnIsWow64Process(GetCurrentProcess(), &bIsWow64);
	}
	return bIsWow64;
}

BOOL  GetSystemName(wchar_t *buf, int  bufsize)
{
	static std::wstring 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); 



	memset(buf, 0, bufsize*sizeof(wchar_t));

	PGNSI pGNSI;
	PGPI pGPI;
	BOOL bOsVersionInfoEx;
	DWORD dwType;
	OSVERSIONINFOEX osvi;
	SYSTEM_INFO si;
	wstring wstrVersionType;

	ZeroMemory(&si, sizeof(SYSTEM_INFO));
	ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));

	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);


	if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) )
	{
		return FALSE;
	}
		

  	pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),  "GetNativeSystemInfo");
  	if(NULL != pGNSI)
  		pGNSI(&si);
  	else 
  		GetSystemInfo(&si);
  

	StringCchCopy(buf, bufsize, TEXT("Microsoft "));


	if (dwMajor >= 6)
	{

		pGPI = (PGPI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),  "GetProductInfo");

		pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);

		if (dwMajor == 6 && dwMinor == 3)	//win 8.1
		{
			if (dwBuildNumber == WINVERSION_2012)
			{
				//vname = L"Microsoft Windows Server 2012 R2";
				StringCchCat(buf, bufsize, TEXT("Windows Server 2012 R2 "));
			}
			else
			{
				//vname = L"Microsoft Windows 8.1";
				StringCchCat(buf, bufsize, TEXT("Windows 8.1 "));
			}

			GetVersionType(dwType, wstrVersionType);

			StringCchCat(buf,bufsize,wstrVersionType.c_str());

			if (IsWow64())
				StringCchCat(buf, bufsize,TEXT("x64"));
			else
				StringCchCat(buf, bufsize,TEXT("x86"));
			//return vname.c_str();
			return TRUE;
		}

		if (dwMajor == 10 && dwMinor == 0)	//win 10
		{
			    if(osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(buf,bufsize,TEXT("Windows 10 "));
				else
					StringCchCat(buf,bufsize,TEXT("Windows Server 2016 "));;//服务器版本 
	


			GetVersionType(dwType, wstrVersionType);

			StringCchCat(buf,bufsize,wstrVersionType.c_str());

			if (IsWow64())
				StringCchCat(buf, bufsize,TEXT("x64"));
			else
				StringCchCat(buf, bufsize,TEXT("x86"));
			//return vname.c_str();
			return TRUE;
		}

		if (osvi.dwMajorVersion == 6)
		{
			switch (osvi.dwMinorVersion)
			{
			case 0:
				{
					if (osvi.wProductType == VER_NT_WORKSTATION)
						StringCchCat(buf, bufsize, TEXT("Windows Vista "));
					else
						StringCchCat(buf, bufsize, TEXT("Windows Server 2008 "));

				}
				break;
			case 1:
				{
					if (osvi.wProductType == VER_NT_WORKSTATION)
						StringCchCat(buf, bufsize, TEXT("Windows 7 "));
					else
						StringCchCat(buf, bufsize, TEXT("Windows Server 2008 R2 "));
				}
				break;
			case 2:
				{
					if (osvi.wProductType == VER_NT_WORKSTATION)
						StringCchCat(buf, bufsize, TEXT("Windows 8 "));
					else
						StringCchCat(buf, bufsize, TEXT("Windows Server 2012 "));

				}
				break;
			}

			GetVersionType(dwType, wstrVersionType);
			StringCchCat(buf,bufsize,wstrVersionType.c_str()); 
			if (IsWow64())
				StringCchCat(buf, bufsize,TEXT("x64"));
			else
				StringCchCat(buf, bufsize,TEXT("x86"));

			return TRUE;

		}
	}
	else
	{

		//下面根据版本信息判断操作系统名称  
		switch (osvi.dwMajorVersion)  //判断主版本号  
		{                       	
		case 5:
			{
				switch (osvi.dwMinorVersion)
				{               //再比较dwMinorVersion的值  
				case 0:
					{
						StringCchCat(buf, bufsize, TEXT("Windows 2000")); //1999年12月发布  
						if ( osvi.wProductType == VER_NT_WORKSTATION )
						{
							StringCchCat(buf, bufsize, TEXT( "Professional" ));
						}
						else 
						{
							if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
							{
								StringCchCat(buf, bufsize, TEXT( "Datacenter Server" ));
							}     
							else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
							{
								StringCchCat(buf, bufsize, TEXT( "Advanced Server" ));
							}    
							else
							{
								StringCchCat(buf, bufsize, TEXT( "Server" ));
							} 

						}

						if (IsWow64())
							StringCchCat(buf, bufsize,TEXT("x64"));
						else
							StringCchCat(buf, bufsize,TEXT("x86"));
					}
					break;
				case 1:
					{
						StringCchCat(buf, bufsize, TEXT("Windows XP"));  //2001年8月发布    
						if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
							StringCchCat(buf, bufsize, TEXT( "Home Edition" ));
						else 
							StringCchCat(buf, bufsize, TEXT( "Professional" ));

			           if (IsWow64())
				          StringCchCat(buf, bufsize,TEXT("x64"));
			           else
				          StringCchCat(buf, bufsize,TEXT("x86"));
			        }
					break;
				case 2:
			       {

					   if( GetSystemMetrics(SM_SERVERR2))
						   StringCchCat(buf, bufsize, TEXT( "Windows Server 2003 R2 "));
					   else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER )
						   StringCchCat(buf, bufsize, TEXT( "Windows Storage Server 2003 "));

					   else if( osvi.wProductType == VER_NT_WORKSTATION && si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
					   {
						   StringCchCat(buf, bufsize, TEXT( "Windows XP Professional x64 Edition "));
					   }
					   else 
						   StringCchCat(buf, bufsize, TEXT("Windows Server 2003 "));

					   // Test for the server type.
					   if ( osvi.wProductType != VER_NT_WORKSTATION )
					   {
						   if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
						   {
							   if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
								   StringCchCat(buf, bufsize, TEXT( "Datacenter Edition for Itanium-based Systems " ));
							   else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
								   StringCchCat(buf, bufsize, TEXT( "Enterprise Edition for Itanium-based Systems " ));
						   }
						   else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
						   {
							   if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
								   StringCchCat(buf, bufsize, TEXT( "Datacenter x64 Edition " ));
							   else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
								   StringCchCat(buf, bufsize, TEXT( "Enterprise x64 Edition " ));
							   else 
								   StringCchCat(buf, bufsize, TEXT( "Standard x64 Edition " ));
						   }
						   else
						   {
							   if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
								   StringCchCat(buf, bufsize, TEXT( "Compute Cluster Edition " ));
							   else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
								   StringCchCat(buf, bufsize, TEXT( "Datacenter Edition " ));
							   else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
								   StringCchCat(buf, bufsize, TEXT( "Enterprise Edition " ));
							   else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
								   StringCchCat(buf, bufsize, TEXT( "Web Edition " ));
							   else 
								   StringCchCat(buf, bufsize, TEXT( "Standard Edition " ));
						   }			
					   }

					   break;
			       }
				}

			break;
		case 4:
			switch (osvi.dwMinorVersion)
			{                //判断次版本号  
			case 0:
				if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
					StringCchCat(buf, bufsize, TEXT("Windows NT 4.0"));//1996年7月发布  
				else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
					StringCchCat(buf, bufsize, TEXT("Windows 95"));
				break;
			case 10:
				StringCchCat(buf, bufsize, TEXT("Windows 98"));
				break;
			case 90:
				StringCchCat(buf, bufsize, TEXT("Windows Me"));
				break;
			}
			break;

		default:
			StringCchCat(buf, bufsize, TEXT("Unknown OS"));
			break;
		}
	  }
	}
  
	return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
	wchar_t systename[MAX_PATH] ;

	GetSystemName(systename, MAX_PATH);
 
	wprintf(L"%s",systename);

	getchar();
	getchar();
	return 0;
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值