获取Windows版本

要利用API获取Windows 8.1的版本号,必须自定义manifest文件,指定程序支持Windows8.1,manifest文件如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level="asInvoker" uiAccess="false"></requestedExecutionLevel>
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="x86" publicKeyToken="6595b64144ccf1df" language="*"></assemblyIdentity>
    </dependentAssembly>
  </dependency>
    <compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
        <application>
            <!-- Windows 8.1 -->
            <supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
            <!-- Windows Vista -->
            <supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
            <!-- Windows 7 -->
            <supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
            <!-- Windows 8 -->
            <supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
        </application>
    </compatibility>
</assembly>

将manifest文件应用到项目:

“项目"——"属性"——"清单工具"——"输入和输出"——将manifest文件的名称填入到"附加清单文件”里面,编译。


获取Windows版本的代码如下:

typedef VOID (WINAPI *GETNATIVESYSTEMINFO)(LPSYSTEM_INFO);
typedef BOOL (WINAPI *GETPRODUCTINFO)(DWORD,DWORD,DWORD,DWORD,PDWORD);

BOOL GetWindowsVersion(LPTSTR pszOS, size_t cchLen)
{
	SYSTEM_INFO si;
	OSVERSIONINFOEX osvi;
	GETNATIVESYSTEMINFO pGNSI;
	GETPRODUCTINFO pGPI;
	BOOL bOsVersionInfoEx;
	DWORD dwType;
	
	ZeroMemory(&si,sizeof(SYSTEM_INFO));
	ZeroMemory(&osvi,sizeof(OSVERSIONINFOEX));

	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

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

	pGNSI = (GETNATIVESYSTEMINFO)GetProcAddress(GetModuleHandle(_T("kernel32.dll")),"GetNativeSystemInfo");

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

	if(VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
	{
		StringCchCat(pszOS,cchLen,_T("Microsoft "));

		if(osvi.dwMajorVersion == 6)
		{
			if(osvi.dwMinorVersion == 3)
			{
				if(osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS,cchLen,_T("Windows 8.1 "));
				else
					StringCchCat(pszOS,cchLen,_T("Windows Server 2012 R2 "));
			}
			else if(osvi.dwMinorVersion == 2)
			{
				if(osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS,cchLen,_T("Windows 8 "));
				else
					StringCchCat(pszOS,cchLen,_T("Windows Server 2012 "));
			}
			else if(osvi.dwMinorVersion == 1)
			{
				if(osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS,cchLen,_T("Windows 7 "));
				else
					StringCchCat(pszOS,cchLen,_T("Windows Server 2008 R2 "));
			}
			else if(osvi.dwMinorVersion == 0)
			{
				if(osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS,cchLen,_T("Windows Vista "));
				else
					StringCchCat(pszOS,cchLen,_T("Windows Server 2008 "));
			}
			

			pGPI = (GETPRODUCTINFO)GetProcAddress(GetModuleHandle(_T("kernel32.dll")),"GetProductInfo");
			if(pGPI)
			{
				pGPI(6,0,0,0,&dwType);
			}

			switch(dwType)
			{
			case PRODUCT_ULTIMATE:
				StringCchCat(pszOS,cchLen,_T("Ultimate Edition"));
				break;
			case PRODUCT_HOME_PREMIUM:
				StringCchCat(pszOS,cchLen,_T("Home Premium Edition"));
				break;
			case PRODUCT_HOME_BASIC:
				StringCchCat(pszOS,cchLen,_T("Home Basic Edition"));
				break;
			case PRODUCT_ENTERPRISE:
				StringCchCat(pszOS,cchLen,_T("Enterprise Edition"));
				break;
			case PRODUCT_BUSINESS:
				StringCchCat(pszOS,cchLen,_T("Business Edition"));
				break;
			case PRODUCT_STARTER:
				StringCchCat(pszOS,cchLen,_T("Starter Edition"));
				break;
			case PRODUCT_CLUSTER_SERVER:
				StringCchCat(pszOS,cchLen,_T("Cluster Server Edition"));
				break;
			case PRODUCT_DATACENTER_SERVER:
				StringCchCat(pszOS,cchLen,_T("Datacenter Edition"));
				break;
			case PRODUCT_ENTERPRISE_SERVER:  
				StringCchCat(pszOS, cchLen,_T("Enterprise Edition"));  
				break;  
			case PRODUCT_ENTERPRISE_SERVER_CORE:  
				StringCchCat(pszOS, cchLen,_T("Enterprise Edition (core installation)"));  
				break;  
			case PRODUCT_ENTERPRISE_SERVER_IA64:  
				StringCchCat(pszOS, cchLen,_T("Enterprise Edition for Itanium-based Systems"));  
				break;  
			case PRODUCT_SMALLBUSINESS_SERVER:  
				StringCchCat(pszOS, cchLen,_T("Small Business Server"));  
				break;  
			case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:  
				StringCchCat(pszOS, cchLen,_T("Small Business Server Premium Edition"));  
				break;  
			case PRODUCT_STANDARD_SERVER:  
				StringCchCat(pszOS, cchLen,_T("Standard Edition"));  
				break;  
			case PRODUCT_STANDARD_SERVER_CORE:  
				StringCchCat(pszOS, cchLen,_T("Standard Edition (core installation)"));  
				break;  
			case PRODUCT_WEB_SERVER:  
				StringCchCat(pszOS, cchLen,_T("Web Server Edition"));  
				break;  
			default:  
				StringCchCat(pszOS, cchLen,_T("Unknown"));  
				break;  
			}

			if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
				StringCchCat(pszOS,cchLen,_T(", 64-bit"));
			else if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_INTEL)
				StringCchCat(pszOS,cchLen,_T(", 32-bit"));
		}

		if(osvi.dwMajorVersion == 5)
		{
			if(osvi.dwMinorVersion == 2)
			{
				if(osvi.wSuiteMask & VER_SUITE_WH_SERVER)
					StringCchCat(pszOS,cchLen,_T("Windows Home Server"));
				else if(osvi.wProductType == VER_NT_WORKSTATION && 
					si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
					StringCchCat(pszOS,cchLen,_T("Windows XP Professional x64 Edition"));
				else
				{
					if(GetSystemMetrics(SM_SERVERR2) == 0)
						StringCchCat(pszOS,cchLen,_T("Windows Server 2003, "));
					else
						StringCchCat(pszOS,cchLen,_T("Windows Server 2003 R2, "));
				}

				if(osvi.wProductType != VER_NT_WORKSTATION)
				{
					if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
					{
						if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
							StringCchCat(pszOS,cchLen,_T("Datacenter Edition for Itanium-based Systems"));
						else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
							StringCchCat(pszOS,cchLen,_T("Enterprise Edition for Itanium-based Systems"));
					}
					else if(si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
					{
						if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
							StringCchCat(pszOS,cchLen,_T("Datacenter x64 Edition"));
						else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
							StringCchCat(pszOS,cchLen,_T("Enterprise x64 Edition"));
						else	
							StringCchCat(pszOS,cchLen,_T("Standard x64 Edition"));
					}
					else
					{
						if(osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
							StringCchCat(pszOS,cchLen,_T("Compute Cluster Edition"));
						else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
							StringCchCat(pszOS,cchLen,_T("Datacenter Edition"));
						else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) 
							StringCchCat(pszOS,cchLen,_T("Enterprise Edition"));
						else if ( osvi.wSuiteMask & VER_SUITE_BLADE ) 
							StringCchCat(pszOS,cchLen,_T("Web Edition"));
						else 
							StringCchCat(pszOS,cchLen,_T("Standard Edition"));
					}
				}
			}
			else if(osvi.dwMinorVersion == 1)
			{
				StringCchCat(pszOS,cchLen,_T("Windows XP "));
				if(osvi.wSuiteMask & VER_SUITE_PERSONAL)
					StringCchCat(pszOS,cchLen,_T("Home Edition"));
				else
					StringCchCat(pszOS,cchLen,_T("Professional"));
			}
			else if(osvi.dwMinorVersion == 0)
			{
				StringCchCat(pszOS,cchLen,_T("Windows 2000 "));
				if(osvi.wProductType == VER_NT_WORKSTATION)
					StringCchCat(pszOS,cchLen,_T("Professional"));
				else
				{
					if (osvi.wSuiteMask & VER_SUITE_DATACENTER)  
						StringCchCat(pszOS, cchLen,TEXT("Datacenter Server"));  
					else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)  
						StringCchCat(pszOS, cchLen,TEXT("Advanced Server"));  
					else  
						StringCchCat(pszOS, cchLen,TEXT("Server")); 
				}
			}
		}

		TCHAR szBuf[80];
		StringCchPrintf(szBuf,80,_T(" (build %d)"), osvi.dwBuildNumber);
		StringCchCat(pszOS,cchLen + 80, szBuf);

		return TRUE;
	}
	else
	{
		return FALSE;
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值