I needed to check the OS and on different OS the different path needed to be followed and so the host operating system version was needed for an application I was developing.
First I would like to make a table where we know what are the platformId, MajorVersion, MinorVersion of each of the Window'sOS. Here are the PlatformId, Major and Minor Version Numbers of the 32-bit Windows operating systems Table
Now lets begin the OS detection code and for that we need some constants. Lets define the constants first
Now lets write a general routine to detect the OS. Here is the function to detect the OS.
I have skipped the WINCE but that can be easily added with the help of the above table.
Background
First I would like to make a table where we know what are the platformId, MajorVersion, MinorVersion of each of the Window'sOS. Here are the PlatformId, Major and Minor Version Numbers of the 32-bit Windows operating systems Table
Code:
+----------+------------+-------+-------+ | Version | PlatformId | Major | Minor | +----------+------------+-------+-------+ | Win95 | 1 | 4 | 0 | | Win98 | 1 | 4 | 10 | | WinME | 1 | 4 | 90 | | WinNT351 | 2 | 3 | 51 | | WinNT4 | 2 | 4 | 0 | | Win2000 | 2 | 5 | 0 | | WinXP | 2 | 5 | 1 | | Win2003 | 2 | 5 | 2 | | WinCE | 3 | ? | ? | | WinLH | 2 | 6 | 0 | +----------+------------+-------+-------+
Coding
Now lets begin the OS detection code and for that we need some constants. Lets define the constants first
Code: C++
typedef enum OSVersion
{
WIN95 = 1,
WIN98 = 2,
WINME = 3,
WINNT351 = 4,
WINNT4 = 5,
WIN2000 = 6,
WINXP = 7,
WIN2003 = 8,
WINCE = 9,
WINLH = 10,
WIN64BIT = 11
} OS_VERSION;
Code: C++
OS_VERSION GetHostOS()
{
BOOL bIs64BitOS = FALSE;
BOOL bIsWin95 = FALSE;
BOOL bIsWin98 = FALSE;
BOOL bIsWinME = FALSE;
BOOL bIsWinNT351 = FALSE;
BOOL bIsWinNT4 = FALSE;
BOOL bIsWin2000 = FALSE;
BOOL bIsWinXP = FALSE;
BOOL bIsWin2003 = FALSE;
BOOL bIsWinLH = FALSE;
// We check if the OS is 64 Bit
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(
GetModuleHandle("kernel32"),"IsWow64Process");
if (NULL != fnIsWow64Process)
{
if (!fnIsWow64Process(GetCurrentProcess(),&bIs64BitOS))
{
// handle error
}
}
OSVERSIONINFO osvi;
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
::GetVersionEx( &osvi );
bIsWin95 =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
(osvi.dwMajorVersion == 4) &&
(osvi.dwMinorVersion == 0);
bIsWin98 =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
(osvi.dwMajorVersion == 4) &&
(osvi.dwMinorVersion == 10);
bIsWinME =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &&
(osvi.dwMajorVersion == 4) &&
(osvi.dwMinorVersion == 90);
bIsWinNT351 =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(osvi.dwMajorVersion == 3) &&
(osvi.dwMinorVersion == 51);
bIsWinNT4 =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(osvi.dwMajorVersion == 4) &&
(osvi.dwMinorVersion == 0);
bIsWin2000 =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(osvi.dwMajorVersion == 5) &&
(osvi.dwMinorVersion == 0);
bIsWinXP =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(osvi.dwMajorVersion == 5) &&
(osvi.dwMinorVersion == 1);
bIsWin2003 =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(osvi.dwMajorVersion == 5) &&
(osvi.dwMinorVersion == 2);
bIsWinLH =
(osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &&
(osvi.dwMajorVersion == 6);
if( bIs64BitOS ) return WIN64BIT;
if( bIsWin95 ) return WIN95;
if( bIsWin98 ) return WIN98;
if( bIsWinME ) return WINME;
if( bIsWinNT351 ) return WINNT351;
if( bIsWinNT4 ) return WINNT4;
if( bIsWin2000 ) return WIN2000;
if( bIsWinXP ) return WINXP;
if( bIsWin2003 ) return WIN2003;
if( bIsWinLH ) return WINLH;
return WINXP;
}

