获取操作系统的详细信息--代码实现

1、GetVersionExA的使用

OSVERSIONINFO 结构体获取

头文件:#include <windows.h>

OSVERSIONINFO osvi;
ZeroMemory(osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx((OSVERSIONINFO *)&osvi);

然后根据获取的主版本,次版本

最后给出获取OS的代码,没有单独试过是不是可以获取win10版本信息,修改后的确可以

BOOL ClassName::SetOperateSystemTypeAndCoreVersion()
{	
	TCHAR				pszOS[256+1] = {0};
	TCHAR				pszOSCore[64] = {0};
	UINT				uBufSize = 256;
	OSVERSIONINFOEX		osvi;
	SYSTEM_INFO			si;
	PGNSI				pGNSI;
	PGPI				pGPI;
	BOOL				bOsVersionInfoEx;
	DWORD				dwType;

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

	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
	bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi);

	if (! bOsVersionInfoEx)
	{
		return FALSE;
	}

	pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")),"GetNativeSystemInfo");

	if (NULL != pGNSI)
	{
		pGNSI(&si);
	}
	else 
	{
		GetSystemInfo(&si);
	}

	if (VER_PLATFORM_WIN32_NT==osvi.dwPlatformId && osvi.dwMajorVersion > 4)
	{
		StringCchCopy(pszOS, uBufSize, TEXT("Microsoft "));
		if (osvi.dwMajorVersion >= 6)
		{   // after Windows 8.1 or later, GetVersionEx function cannot get correct version...
			OSVERSIONINFOEXW ovi;
			ZeroMemory(&ovi, sizeof(OSVERSIONINFOEXW));
			if (!GetVersionEx2((LPOSVERSIONINFOW)&ovi)) return FALSE;
			// GetVersionEx2/RtlGetVersion cannot get correct wProductType, so copy it to osvi
			osvi.dwMajorVersion = ovi.dwMajorVersion;
			osvi.dwMinorVersion = ovi.dwMinorVersion;
			osvi.dwBuildNumber  = ovi.dwBuildNumber;
			//_tprintf(TEXT("RtlGetVersion=%d.%d.%d\n"), osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber); //output version info directly
		}
		
		LogPrint::PrintLogA("osvi.dwMajorVersion === %d",osvi.dwMajorVersion);
		LogPrint::PrintLogA("osvi.dwMinorVersion ===  :%d",osvi.dwMinorVersion);
		
		if (osvi.dwMajorVersion > 6)
		{
			if ( osvi.dwMajorVersion > 10 || osvi.dwMinorVersion > 0 )
			{
				//Reserve  for new OS
				StringCchCat(pszOS, uBufSize, TEXT("Windows 10 Later ")); 
			}
			else
			{
				if( osvi.wProductType == VER_NT_WORKSTATION )
					StringCchCat(pszOS, uBufSize, TEXT("Windows 10 "));
				else
					StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2016 " ));
			}

		}
		else if (osvi.dwMajorVersion == 6)
		{
			StringCchCopy(pszOSCore,64,TEXT("6"));

			if (osvi.dwMinorVersion == 0)
			{
				StringCchCat(pszOSCore,64,TEXT(".0"));
				if(osvi.wProductType == VER_NT_WORKSTATION)
				{
					StringCchCat(pszOS, uBufSize, TEXT("Windows Vista "));
				}
				else 
				{
					StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2008 " ));
				}
			}
			else if (osvi.dwMinorVersion == 1 )
			{
				StringCchCat(pszOSCore,64,TEXT(".1"));
				if (osvi.wProductType == VER_NT_WORKSTATION)
				{
					StringCchCat(pszOS, uBufSize, TEXT("Windows 7 "));
				}
				else
				{
					StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2008 R2 " ));
				}
			} 
			else if(osvi.dwMinorVersion == 2)
			{
				StringCchCat(pszOSCore,64,TEXT(".2"));
				 if (osvi.wProductType == VER_NT_WORKSTATION)
				{
					StringCchCat(pszOS, uBufSize, TEXT("Windows 8 "));
				}
				 else
				 {
					 StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2012"));
				 }
			}	
			else if (osvi.dwMinorVersion == 3)
			{
				StringCchCat(pszOSCore,64,TEXT(".3"));
				if (osvi.wProductType == VER_NT_WORKSTATION )
				{
					StringCchCat(pszOS, uBufSize, TEXT("Windows 8.1"));
				}
				else
				{
					StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2012 R2"));
				}
			}
			pGPI = (PGPI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");

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

			switch(dwType)
			{
			case PRODUCT_ULTIMATE:
				StringCchCat(pszOS, uBufSize, TEXT("Ultimate Edition"));
				break;
			case PRODUCT_PROFESSIONAL:
				StringCchCat(pszOS, uBufSize, TEXT("Professional"));
				break;
			case PRODUCT_HOME_PREMIUM:
				StringCchCat(pszOS, uBufSize, TEXT("Home Premium Edition"));
				break;
			case PRODUCT_HOME_BASIC:
				StringCchCat(pszOS, uBufSize, TEXT("Home Basic Edition"));
				break;
			case PRODUCT_ENTERPRISE:
				StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition"));
				break;
			case PRODUCT_BUSINESS:
				StringCchCat(pszOS, uBufSize, TEXT("Business Edition"));
				break;
			case PRODUCT_STARTER:
				StringCchCat(pszOS, uBufSize, TEXT("Starter Edition"));
				break;
			case PRODUCT_CLUSTER_SERVER:
				StringCchCat(pszOS, uBufSize, TEXT("Cluster Server Edition"));
				break;
			case PRODUCT_DATACENTER_SERVER:
				StringCchCat(pszOS, uBufSize, TEXT("Datacenter Edition"));
				break;
			case PRODUCT_DATACENTER_SERVER_CORE:
				StringCchCat(pszOS, uBufSize, TEXT("Datacenter Edition (core installation)"));
				break;
			case PRODUCT_ENTERPRISE_SERVER:
				StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition" ));
				break;
			case PRODUCT_ENTERPRISE_SERVER_CORE:
				StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition (core installation)"));
				break;
			case PRODUCT_ENTERPRISE_SERVER_IA64:
				StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition for Itanium-based Systems"));
				break;
			case PRODUCT_SMALLBUSINESS_SERVER:
				StringCchCat(pszOS, uBufSize, TEXT("Small Business Server"));
				break;
			case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
				StringCchCat(pszOS, uBufSize, TEXT("Small Business Server Premium Edition"));
				break;
			case PRODUCT_STANDARD_SERVER:
				StringCchCat(pszOS, uBufSize, TEXT("Standard Edition"));
				break;
			case PRODUCT_STANDARD_SERVER_CORE:
				StringCchCat(pszOS, uBufSize, TEXT("Standard Edition (core installation)"));
				break;
			case PRODUCT_WEB_SERVER:
				StringCchCat(pszOS, uBufSize, TEXT("Web Server Edition"));
				break;
			}
		}
		else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
		{
			StringCchCopy(pszOSCore,64,TEXT("5"));
			StringCchCat(pszOSCore,64,TEXT(".2"));

			if(GetSystemMetrics(SM_SERVERR2))
			{
				StringCchCat(pszOS, uBufSize, TEXT( "Windows Server 2003 R2, "));
			}
			else if (osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER)
			{
				StringCchCat(pszOS, uBufSize, TEXT( "Windows Storage Server 2003"));
			}
			else if (osvi.wSuiteMask & VER_SUITE_WH_SERVER)
			{
				StringCchCat(pszOS, uBufSize, TEXT( "Windows Home Server"));
			}
			else if(osvi.wProductType == VER_NT_WORKSTATION &&
				si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
			{
				StringCchCat(pszOS, uBufSize, TEXT("Windows XP Professional x64 Edition"));
			}
			else 
			{
				StringCchCat(pszOS, uBufSize, TEXT("Windows Server 2003, "));
			}

			if (osvi.wProductType != VER_NT_WORKSTATION)
			{
				if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64)
				{
					if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
					{
						StringCchCat(pszOS, uBufSize, TEXT("Datacenter Edition for Itanium-based Systems"));
					}
					else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
					{
						StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition for Itanium-based Systems"));
					}
				}
				else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
				{
					if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
					{
						StringCchCat(pszOS, uBufSize, TEXT("Datacenter x64 Edition"));
					}
					else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
					{
						StringCchCat(pszOS, uBufSize, TEXT("Enterprise x64 Edition"));
					}
					else 
					{
						StringCchCat(pszOS, uBufSize, TEXT("Standard x64 Edition"));
					}
				}
				else
				{
					if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
					{
						StringCchCat(pszOS, uBufSize, TEXT("Compute Cluster Edition"));
					}
					else if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
					{
						StringCchCat(pszOS, uBufSize, TEXT("Datacenter Edition"));
					}
					else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
					{
						StringCchCat(pszOS, uBufSize, TEXT("Enterprise Edition"));
					}
					else if (osvi.wSuiteMask & VER_SUITE_BLADE)
					{
						StringCchCat(pszOS, uBufSize, TEXT("Web Edition"));
					}
					else 
					{
						StringCchCat(pszOS, uBufSize, TEXT("Standard Edition"));
					}
				}
			}
		}
		else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
		{
			StringCchCopy(pszOSCore, 64, TEXT("5.1"));

			StringCchCat(pszOS, uBufSize, TEXT("Windows XP "));
			if(osvi.wSuiteMask & VER_SUITE_PERSONAL)
			{
				StringCchCat(pszOS, uBufSize, TEXT("Home Edition"));
			}
			else 
			{
				StringCchCat(pszOS, uBufSize, TEXT( "Professional"));
			}
		}
		else if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
		{
			StringCchCat(pszOS, uBufSize, TEXT("Windows 2000 "));

			StringCchCopy(pszOSCore, 64, TEXT("5.0"));

			if ( osvi.wProductType == VER_NT_WORKSTATION )
			{
				StringCchCat(pszOS, uBufSize, TEXT("Professional"));
			}
			else 
			{
				if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
				{
					StringCchCat(pszOS, uBufSize, TEXT("Datacenter Server"));
				}
				else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
				{
					StringCchCat(pszOS, uBufSize, TEXT("Advanced Server"));
				}
				else 
				{
					StringCchCat(pszOS, uBufSize, TEXT("Server"));
				}
			}
		}


		if(_tcslen(osvi.szCSDVersion) > 0)
		{
			StringCchCat(pszOS, uBufSize, TEXT(" "));
			StringCchCat(pszOS, uBufSize, osvi.szCSDVersion);
		}

		TCHAR bufCore[64];
		
		StringCchPrintf(bufCore, 64, TEXT("%d"), osvi.dwBuildNumber);

		StringCchCat(pszOSCore, 64, TEXT("."));
		StringCchCat(pszOSCore, 64, bufCore);
	
		m_strOStype =global::WCharToMultiChar(pszOS);
		m_strOSCoreType = global::WCharToMultiChar(pszOSCore);

		return TRUE; 
	}
	else
	{  
		return FALSE;
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Ma_Hong_Kai

微信 2936729162

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值