OSのバージョン取得

使用方法

GetOS();を実行すれば整数形式でOSの種類が返る。

 95   -> Windows 95 (OSR2.0/2.1/2.5)
 98   -> Windows 98 (Second Edition)
 99   -> Windows Millennium Edition
 2000 -> Windows 2000
 3    -> Windows NT 3.5x
 4    -> Windows NT 4.0
 31   -> Windows 3.1
 0    -> 不明なOS


ソースコード

int	GetOS(CString* lpstrVersion/*=NULL*/)
{
	int             nOS;

	OSVERSIONINFO osVersionInfo;

	osVersionInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
	GetVersionEx(&osVersionInfo);

	if(lpstrVersion != NULL)
	{
		lpstrVersion->Format(" Version %d.%d Build %d",
			osVersionInfo.dwMajorVersion,
			osVersionInfo.dwMinorVersion,
			osVersionInfo.dwBuildNumber & 0xffff);
        }



	nOS = 0;
	switch(osVersionInfo.dwPlatformId)
	{
	case    VER_PLATFORM_WIN32s:
		nOS = 31;                       // Windows 3.1
		break;

	case VER_PLATFORM_WIN32_WINDOWS:
		if(osVersionInfo.dwMajorVersion == 4)
		{
			if(osVersionInfo.dwMinorVersion == 0)
				nOS = 95;               // Windows 95
			if(osVersionInfo.dwMinorVersion > 0)
				nOS = 98;               // Windows 98
			if(osVersionInfo.dwMinorVersion > 9)
				nOS = 99;               // Windows Me
		}

		if(osVersionInfo.dwMajorVersion > 4)
			nOS = 99;               // Windows Me
		break;

		case VER_PLATFORM_WIN32_NT:
			if(osVersionInfo.dwMajorVersion == 3)
				nOS = 3;                // Windows NT 3.5
			if(osVersionInfo.dwMajorVersion == 4)
				nOS = 4;                // Windows NT 4.0
			if(osVersionInfo.dwMajorVersion >= 5)
				nOS = 2000;             // Windows 2000
			break;

		default:
			nOS = 0;

	}


	if(lpstrVersion != NULL)
	{
		switch(nOS)
		{
		case    31:
			*lpstrVersion = "Windows 3.1" + *lpstrVersion;
			break;
		case    95:
			*lpstrVersion = "Windows 95" + *lpstrVersion;
			break;
		case    98:
			*lpstrVersion = "Windows 98" + *lpstrVersion;
			break;
		case    99:
			*lpstrVersion = "Windows Me" + *lpstrVersion;
			break;
		case    3:
			*lpstrVersion = "Windows NT 3.5x" + *lpstrVersion;
			break;
		case    4:
			*lpstrVersion = "Windows NT 4.0" + *lpstrVersion;
			break;
		case    2000:
			*lpstrVersion = "Windows 2000" + *lpstrVersion;
			break;
		default:
			*lpstrVersion = "Unknown OS" + *lpstrVersion;
			break;
		}
	}

	return	nOS;
}

カテゴリー「VC++ TIPS」 のエントリー