OS version detection for 32 and 64 bit OS.

shabbir's Avatar author of OS version detection for 32 and 64 bit OS.
This is an article on OS version detection for 32 and 64 bit OS. in Win32.
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.

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;
Now lets write a general routine to detect the OS. Here is the function to detect the OS.
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;
}
I have skipped the WINCE but that can be easily added with the help of the above table.
Attached Files
File Type: zip OsVersion.zip (804 Bytes, 174 views)
Contributor
5Mar2008,18:47   #2
aisha.ansari84's Avatar
wow something like this is also possible
Ambitious contributor
6Mar2008,13:19   #3
rahul.mca2001's Avatar
i tried it works
Newbie Member
10Aug2010,12:29   #4
Farrukh's Avatar
I'm trying to download OsVersion.zip, but it is downloading as "attachment.php" and when I rename it to OsVersion.zip, it is found corrupted.
Newbie Member
10Aug2010,12:30   #5
Farrukh's Avatar
But on the 2nd try, I downloaded and renamed to zip and it worked fine

Going to try that
Go4Expert Member
27May2011,20:14   #6
msdnguide's Avatar
On solaris there is a command called isainfo that describe instruction set architectures. It will tell if OS is in 32 bit or 64 bit