OS version detection for 32 and 64 bit OS.

Discussion in 'Win32' started by shabbir, Sep 26, 2006.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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:
    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:
    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:

  2. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
    wow something like this is also possible
     
  3. rahul.mca2001

    rahul.mca2001 New Member

    Joined:
    Feb 13, 2008
    Messages:
    103
    Likes Received:
    0
    Trophy Points:
    0
    i tried it works
     
  4. Farrukh

    Farrukh New Member

    Joined:
    Mar 23, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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. :(
     
  5. Farrukh

    Farrukh New Member

    Joined:
    Mar 23, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    But on the 2nd try, I downloaded and renamed to zip and it worked fine :)

    Going to try that
     
  6. msdnguide

    msdnguide New Member

    Joined:
    May 14, 2011
    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    msdnguide
    Location:
    Delhi
    Home Page:
    http://www.msdnguide.info/
    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
     
  7. xiaochunjie

    xiaochunjie New Member

    Joined:
    Feb 5, 2015
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    thank you .

    this is a nice code.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice