解説
Visual C++の標準ヘッダーファイルの1つ「stdlib.h」を見ると以下のような宣言がある。/* Windows major/minor and O.S. version numbers */ _CRTIMP extern unsigned int _osplatform; _CRTIMP extern unsigned int _osver; _CRTIMP extern unsigned int _winver; _CRTIMP extern unsigned int _winmajor; _CRTIMP extern unsigned int _winminor;
コメントや変数名から分かるようにOSのバージョンなどが格納される変数らしい。さらに標準ライブ ラリのソースコードを調べると「crt0.c」や「dllcrt0.c」、「crtlib.c」に以下のような代入ルーチンが見つかる。
posvi = (OSVERSIONINFOA *)_alloca(sizeof(OSVERSIONINFOA));
/*
* Get the full Win32 version
*/
posvi->dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
(void)GetVersionExA(posvi);
_osplatform = posvi->dwPlatformId;
_winmajor = posvi->dwMajorVersion;
_winminor = posvi->dwMinorVersion;
/*
* The somewhat bizarre calculations of _osver and _winver are
* required for backward compatibility (used to use GetVersion)
*/
_osver = (posvi->dwBuildNumber) & 0x07fff;
if ( _osplatform != VER_PLATFORM_WIN32_NT )
_osver |= 0x08000;
_winver = (_winmajor << 8) + _winminor;
つまり_osplatform、_winmajor、_winminorはGetVersionEx()での取得値そのままが代入され、 _osverはビルド番号の下位15ビット、_winverはdwMajorVersionとdwMinorVersionの合成値が入っ ている。
使用方法
使い方はいたって簡単。通常使っているコードの中でいきなり_osplatformなどを参照してしまえばいい。
if(_osplatform == VER_PLATFORM_WIN32_NT)
{
//NT系OS(Windows NT/2000/XP/2003など)での処理
}
else
{
//9x系もしくはWin3.1での処理
}
if(_osplatform == VER_PLATFORM_WIN32_WINDOWS && _winmajor == 4 && _winminor == 0)
{
//Windows95での処理
}







