Detecting the Windows version running on your machine

The following code retrieves the current Windows Operating System name and version (with Service Packs) into a CString.

OSVERSIONINFO osv;
osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
CString WindowsPlatform;
if (GetVersionEx(&osv))
{
 // note: szCSDVersion =  service pack  release  
 CString ServiceRelease = osv.szCSDVersion;
 switch(osv.dwPlatformId)
 {
  case VER_PLATFORM_WIN32s: //Win32s on Windows 3.1.
   WindowsPlatform = "Microsoft® Windows 3.1(TM)";
  break;

  case VER_PLATFORM_WIN32_WINDOWS: //WIN32 on 95 or 98
   //determine if Win95 or Win98
   if (osv.dwMinorVersion == 0)
   {
    WindowsPlatform = "Microsoft® Windows 95(TM) " + ServiceRelease;
   }
   else
   {
    WindowsPlatform = "Microsoft® Windows 98(TM) " + ServiceRelease;
   }
  break;

  case VER_PLATFORM_WIN32_NT: //Win32 on Windows NT.
   WindowsPlatform = "Microsoft® Windows NT(TM) "  + ServiceRelease;
  break;

  default:
   AfxMessageBox("Failed to get correct Operating System.", MB_OK);  
 } //end switch
} //end if

Date Last Updated: March 1, 1999



Comments

  • New for VS.NET

    Posted by Legacy on 01/20/2004 12:00am

    Originally posted by: Patrick

    New Macros built in (and GetVersionEx disappeared) in VS.NET (2k3)

    /* From MSDN
    _osver - Current Build Number
    _winmajor - Major Version Number
    _winminor - Minor Version Number
    -winver - Holds value of _winmajor in high byte and the value of _winminor in low byte
    */

    So, just use it with the case/switch statements shown earlier and you should be fine.

    Patrick

    Reply
  • How to get Windows registration number in Windows 2000?

    Posted by Legacy on 11/27/2002 12:00am

    Originally posted by: Cham

    How to get Windows registration number in Windows 2000?

    Reply
  • Update From Microsoft

    Posted by Legacy on 10/16/2002 12:00am

    Originally posted by: Stephen Love

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/getting_the_system_version.asp

    Here is an update streight from microsoft

    Reply
  • Remote...

    Posted by Legacy on 09/13/2002 12:00am

    Originally posted by: Rob

    Hi all,
    is there a possibility to get the platform info via remote ?
    I think, there is one, but I don�t find it..:-)

    THX!
    Rob.

    Reply
  • HELP!!! HOW TO DO THIS!!

    Posted by Legacy on 08/15/2002 12:00am

    Originally posted by: James

    may i know how to get the codes going? please help!!

    Reply
  • Found another way of determining the current Operating system..

    Posted by Legacy on 07/03/2001 12:00am

    Originally posted by: Ian Montgomery

    void GetOS(CString &sSystem)
    
    {
    CString sKey;
    sSystem="<Unknown>";
    sKey.Format("SOFTWARE\\Microsoft\\%s\\CurrentVersion",
    (getenv("OS")? "Windows NT":"Windows"));
    HKEY hKey=0;
    RegOpenKeyEx(HKEY_LOCAL_MACHINE,sKey,0,KEY_ALL_ACCESS,
    &hKey);
    if (hKey) {
    CString sTitle("ProductName");
    DWORD dwType=REG_SZ,nD=250;
    char sV[250]; BYTE *pD=(BYTE*)sV;
    RegQueryValueEx(hKey,sTitle,0,&dwType,pD,&nD);
    if (strlen(sV))
    sSystem=sV;
    else sSystem="Windows NT";
    RegCloseKey(hKey);
    }
    }

    Example Results:

    "Microsoft Windows 2000"
    "Microsoft Windows 95"
    "Microsoft Windows 98"
    "Microsoft NT"

    Reply
  • NT server detection is added

    Posted by Legacy on 11/30/2000 12:00am

    Originally posted by: Alexander

    typedef DWORD (WINAPI *PRtlGetNtProductType)
    
    ( PDWORD pVersion );
    PRtlGetNtProductType func = (PRtlGetNtProductType) GetProcAddress( GetModuleHandle( "ntdll.dll" ), "RtlGetNtProductType" );
    if ( func )
    {
    func( &dwVersion );
    if ( dwVersion != 1 )
    return WINNT_SERVER;
    }
    return WINNT;

    Reply
  • Sample Project

    Posted by Legacy on 04/08/2000 12:00am

    Originally posted by: Ron

    Do you have sample Projects????

    Reply
  • Addon for Windows version check.

    Posted by Legacy on 12/22/1999 12:00am

    Originally posted by: Joakim Fredlund

    I have added support for both Windows 2000 and Windows Millenium.
    
    


    OSVERSIONINFO osv;
    osv.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

    if(GetVersionEx(&osv))
    {
    //note: szCSDVersion = service pack release
    CString ServiceRelease = osv.szCSDVersion;

    switch(osv.dwPlatformId)
    {
    case VER_PLATFORM_WIN32s: //Win32s on Windows 3.1.
    str = "Microsoft� Windows 3.1(TM)";
    break;

    case VER_PLATFORM_WIN32_WINDOWS: //WIN32 on 95 or 98
    //determine if Win95 or Win98
    if(osv.dwMinorVersion == 0)
    {
    str = "Microsoft� Windows 95(TM) "
    + ServiceRelease;
    }
    else if(osv.dwMinorVersion == 10)
    {
    str = "Microsoft� Windows 98(TM) "
    + ServiceRelease;
    }
    if(osv.dwMinorVersion == 90)
    {
    str = "Microsoft� Windows Millenium(TM) "
    + ServiceRelease;
    }
    break;

    case VER_PLATFORM_WIN32_NT: //Win32 on Windows NT.

    if(osv.dwMajorVersion == 4)
    {
    str = "Microsoft� Windows NT(TM) "
    + ServiceRelease;
    }
    else if(osv.dwMajorVersion == 5)
    {
    str = "Microsoft� Windows 2000(TM)"
    + ServiceRelease;
    }
    break;

    default:
    str = _T("");
    }//end switch
    }//end if

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds