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

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds