experiment: 得Windows系统版本号

看到msdn中有一段得Windows系统版本号的代码片段,很不错. 整理了一个类复用.

效果图:

todo列表:

 * Win8手头没有, 等有机会用的时候, 加入Win8系统版本的判断.


测试程序:

// srcOsVersion.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Helper/OsVersion/OsVersionHelper.h"

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

	_tprintf(L"OsVersion = %s\r\n", OsVersion.GetDispString().c_str());

	getchar();
	return 0;
}


COsVersion类封装实现

/// @file			OsVersionHelper.h
/// @brief			得Windows系统版本号, 
///					e.g. Microsoft Windows7 Ultimate Edition x64 Service Pack 1 (build 7601)
/// @reference		msdn keyword = GetVersionEx

#ifndef __OS_VERSION_HELPER_H__
#define __OS_VERSION_HELPER_H__

#include <string>
#include "OsVersionConst.h"

class COsVersion
{
public:
	typedef void (WINAPI *pfnGetNativeSystemInfo)(LPSYSTEM_INFO);
	typedef BOOL (WINAPI *pfnGetProductInfo)(DWORD, DWORD, DWORD, DWORD, PDWORD);

	COsVersion();
	virtual ~COsVersion();

	std::wstring GetDispString();

private:
	bool isKnownOsVersion(OSVERSIONINFOEX & osvi);
	void GetDispString(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si);
	void GetDispString_6x(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si);
	void GetDispString_5x(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si);
	void GetDispString_5_0(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si);
	void GetDispString_5_1(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si);
	void GetDispString_5_2(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si);
	void GetDispString_5_x(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si);

private:
	std::wstring	m_strDispStringW;
};

#endif

/// @file			OsVersionConst.h
/// @brief			...

#ifndef __OS_VERSION_CONST_H__
#define __OS_VERSION_CONST_H__

#endif

/// @file			OsVersionHelper.cpp
/// @brief			...

#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <string>
#include <strsafe.h>
#include "OsVersionHelper.h"

COsVersion::COsVersion()
{
	m_strDispStringW = L"";
}

COsVersion::~COsVersion()
{

}

std::wstring COsVersion::GetDispString()
{
	OSVERSIONINFOEX	osvi;
	SYSTEM_INFO		si;
	FARPROC			lpProc			=	NULL;

	pfnGetNativeSystemInfo	GetNativeSystemInfo	=	NULL;
	pfnGetProductInfo		GetProductInfo		=	NULL;

	::ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
	m_strDispStringW = L"";
	if (!GetVersionEx(reinterpret_cast<OSVERSIONINFO *>(&osvi)))
		return m_strDispStringW;

	/// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
	::ZeroMemory(&si, sizeof(SYSTEM_INFO));
	lpProc = GetProcAddress(GetModuleHandle(L"kernel32.dll"), "GetNativeSystemInfo");
	GetNativeSystemInfo = reinterpret_cast<pfnGetNativeSystemInfo>(lpProc);
	if (NULL != GetNativeSystemInfo)
		GetNativeSystemInfo(&si);
	else
		GetSystemInfo(&si);

	GetDispString(m_strDispStringW, osvi, si);
	return m_strDispStringW;
}

void COsVersion::GetDispString(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si)
{
	wchar_t cBufW[_MAX_PATH];

	if (!isKnownOsVersion(osvi))
		return;

	strDispStringW += L"Microsoft ";
	switch (osvi.dwMajorVersion)
	{
	case 5:
		GetDispString_5x(strDispStringW, osvi, si);
		break;
	case 6:
		GetDispString_6x(strDispStringW, osvi, si);
		break;
	default:
		StringCchPrintf( cBufW, sizeof(cBufW) / sizeof(wchar_t), TEXT(" %d.%d (build %d)"),
			osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber);
		strDispStringW += cBufW;
		break;
	}

	/// Include service pack (if any) and build number.
	if (_tcslen(osvi.szCSDVersion) > 0)
	{
		strDispStringW += L" ";
		strDispStringW += osvi.szCSDVersion;
	}

	StringCchPrintf( cBufW, sizeof(cBufW) / sizeof(wchar_t), TEXT(" (build %d)"), osvi.dwBuildNumber);
	strDispStringW += cBufW;
}

bool COsVersion::isKnownOsVersion(OSVERSIONINFOEX & osvi)
{
	return ((VER_PLATFORM_WIN32_NT == osvi.dwPlatformId) && (osvi.dwMajorVersion > 4));
}

void COsVersion::GetDispString_6x(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si)
{
	DWORD				dwType			=	0;
	wchar_t				cBufW[_MAX_PATH];
	pfnGetProductInfo	GetProductInfo	=	NULL;

	if (osvi.wProductType == VER_NT_WORKSTATION)
	{
		if (1 == osvi.dwMinorVersion)
			strDispStringW += L"Windows7 ";
		else
			strDispStringW += L"Windows Vista ";
	}
	else
		strDispStringW += L"Windows Server 2008 ";
	/// @todo win8 ?

	GetProductInfo = reinterpret_cast<pfnGetProductInfo>(GetProcAddress(GetModuleHandle(L"kernel32.dll"), "GetProductInfo"));
	GetProductInfo( 6, 0, 0, 0, &dwType);
	switch ( dwType )
	{
	case PRODUCT_ULTIMATE:
		strDispStringW += L"Ultimate Edition";
		break;
	case PRODUCT_HOME_PREMIUM:
		strDispStringW += L"Home Premium Edition";
		break;
	case PRODUCT_HOME_BASIC:
		strDispStringW += L"Home Basic Edition";
		break;
	case PRODUCT_ENTERPRISE:
		strDispStringW += L"Enterprise Edition";
		break;
	case PRODUCT_BUSINESS:
		strDispStringW += L"Business Edition";
		break;
	case PRODUCT_STARTER:
		strDispStringW += L"Starter Edition";
		break;
	case PRODUCT_CLUSTER_SERVER:
		strDispStringW += L"Cluster Server Edition";
		break;
	case PRODUCT_DATACENTER_SERVER:
		strDispStringW += L"Datacenter Edition";
		break;
	case PRODUCT_DATACENTER_SERVER_CORE:
		strDispStringW += L"Datacenter Edition (core installation)";
		break;
	case PRODUCT_ENTERPRISE_SERVER:
		strDispStringW += L"Enterprise Edition";
		break;
	case PRODUCT_ENTERPRISE_SERVER_CORE:
		strDispStringW += L"Enterprise Edition (core installation)";
		break;
	case PRODUCT_ENTERPRISE_SERVER_IA64:
		strDispStringW += L"Enterprise Edition for Itanium-based Systems";
		break;
	case PRODUCT_SMALLBUSINESS_SERVER:
		strDispStringW += L"Small Business Server";
		break;
	case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
		strDispStringW += L"Small Business Server Premium Edition";
		break;
	case PRODUCT_STANDARD_SERVER:
		strDispStringW += L"Standard Edition";
		break;
	case PRODUCT_STANDARD_SERVER_CORE:
		strDispStringW += L"Standard Edition (core installation)";
		break;
	case PRODUCT_WEB_SERVER:
		strDispStringW += L"Web Server Edition";
		break;
	default:
		StringCchPrintf( cBufW, sizeof(cBufW) / sizeof(wchar_t), L", ProductType = %d", dwType);
		strDispStringW += cBufW;
		break;
	}

	if (PROCESSOR_ARCHITECTURE_AMD64 == si.wProcessorArchitecture)
		strDispStringW += L" x64";
	else if (PROCESSOR_ARCHITECTURE_INTEL == si.wProcessorArchitecture)
		strDispStringW += L" x86";
	else
	{
		StringCchPrintf( cBufW, sizeof(cBufW) / sizeof(wchar_t), L" ProcessorArchitecture = %d", si.wProcessorArchitecture);
		strDispStringW += cBufW;
	}
}

void COsVersion::GetDispString_5_0(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si)
{
	strDispStringW += L"Windows 2000 ";
	if (VER_NT_WORKSTATION == osvi.wProductType)
		strDispStringW += L"Professional";
	else 
	{
		if ( osvi.wSuiteMask & VER_SUITE_DATACENTER )
			strDispStringW += L"Datacenter Server";
		else if ( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
			strDispStringW += L"Advanced Server";
		else
			strDispStringW += L"Server";
	}
}

void COsVersion::GetDispString_5_1(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si)
{
	strDispStringW += L"Windows XP ";
	if (VER_SUITE_PERSONAL & osvi.wSuiteMask)
		strDispStringW += L"Home Edition";
	else
		strDispStringW += L"Professional";
}

void COsVersion::GetDispString_5_2(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si)
{
	if (GetSystemMetrics(SM_SERVERR2))
		strDispStringW += L"Windows Server 2003 R2, ";
	else if (VER_SUITE_STORAGE_SERVER == osvi.wSuiteMask)
		strDispStringW += L"Windows Storage Server 2003";
	else if ((VER_NT_WORKSTATION == osvi.wProductType) && (PROCESSOR_ARCHITECTURE_AMD64 == si.wProcessorArchitecture))
		strDispStringW += L"Windows XP Professional x64 Edition";
	else
		strDispStringW += L"Windows Server 2003, ";

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

void COsVersion::GetDispString_5_x(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si)
{
	wchar_t				cBufW[_MAX_PATH];

	StringCchPrintf( cBufW, sizeof(cBufW) / sizeof(wchar_t), L"5.%d ", osvi.dwMinorVersion);
	strDispStringW += cBufW;
}

void COsVersion::GetDispString_5x(std::wstring & strDispStringW, OSVERSIONINFOEX & osvi, SYSTEM_INFO & si)
{
	switch (osvi.dwMinorVersion)
	{
	case 0:
		GetDispString_5_0(strDispStringW, osvi, si);
		break;
	case 1:
		GetDispString_5_1(strDispStringW, osvi, si);
		break;
	case 2:
		GetDispString_5_2(strDispStringW, osvi, si);
		break;
	default:
		GetDispString_5_x(strDispStringW, osvi, si);
		break;
	}
}




你可能感兴趣的:(experiment: 得Windows系统版本号)