Getting the System Version

The following example uses the GetVersionEx, GetSystemMetrics, GetProductInfo, and GetNativeSystemInfo functions to determine the version information of the currently running operating system. If compatibility mode is in effect, the example displays the operating system selected for application compatibility. The example displays the information to the console.

To obtain the full version number for the operating system, call the GetFileVersionInfo function on one of the system DLLs, such as Kernel32.dll, then call VerQueryValue to obtain the \\StringFileInfo\\<lang><codepage>\\ProductVersion subblock of the file version information.

Relying on version information is not the best way to test for a feature. Instead, refer to the documentation for the feature of interest. For more information on common techniques for feature detection, see Operating System Version.

If you must require a particular operating system, be sure to use it as a minimum supported version, rather than design the test for the one operating system. This way, your detection code will continue to work on future versions of Windows.


#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <strsafe.h>

#pragma comment(lib, "User32.lib")

#define BUFSIZE 256

typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);

BOOL GetOSDisplayString( LPTSTR pszOS)
{
   OSVERSIONINFOEX osvi;
   SYSTEM_INFO si;
   PGNSI pGNSI;
   PGPI pGPI;
   BOOL bOsVersionInfoEx;
   DWORD dwType;

   ZeroMemory(&si, sizeof(SYSTEM_INFO));
   ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));

   osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
   bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi);

   if(bOsVersionInfoEx != NULL ) return 1;

   // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.

   pGNSI = (PGNSI) GetProcAddress(
      GetModuleHandle(TEXT("kernel32.dll")), 
      "GetNativeSystemInfo");
   if(NULL != pGNSI)
      pGNSI(&si);
   else GetSystemInfo(&si);

   if ( VER_PLATFORM_WIN32_NT==osvi.dwPlatformId && 
        osvi.dwMajorVersion > 4 )
   {
      StringCchCopy(pszOS, BUFSIZE, TEXT("Microsoft "));

      // Test for the specific product.

      if ( osvi.dwMajorVersion == 6 )
      {
         if( osvi.dwMinorVersion == 0 )
         {
            if( osvi.wProductType == VER_NT_WORKSTATION )
                StringCchCat(pszOS, BUFSIZE, TEXT("Windows Vista "));
            else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 " ));
         }

         if ( osvi.dwMinorVersion == 1 )
         {
            if( osvi.wProductType == VER_NT_WORKSTATION )
                StringCchCat(pszOS, BUFSIZE, TEXT("Windows 7 "));
            else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2008 R2 " ));
         }
         
         pGPI = (PGPI) GetProcAddress(
            GetModuleHandle(TEXT("kernel32.dll")), 
            "GetProductInfo");

         pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);

         switch( dwType )
         {
            case PRODUCT_ULTIMATE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Ultimate Edition" ));
               break;
            case PRODUCT_PROFESSIONAL:
               StringCchCat(pszOS, BUFSIZE, TEXT("Professional" ));
               break;
            case PRODUCT_HOME_PREMIUM:
               StringCchCat(pszOS, BUFSIZE, TEXT("Home Premium Edition" ));
               break;
            case PRODUCT_HOME_BASIC:
               StringCchCat(pszOS, BUFSIZE, TEXT("Home Basic Edition" ));
               break;
            case PRODUCT_ENTERPRISE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
               break;
            case PRODUCT_BUSINESS:
               StringCchCat(pszOS, BUFSIZE, TEXT("Business Edition" ));
               break;
            case PRODUCT_STARTER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Starter Edition" ));
               break;
            case PRODUCT_CLUSTER_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Cluster Server Edition" ));
               break;
            case PRODUCT_DATACENTER_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition" ));
               break;
            case PRODUCT_DATACENTER_SERVER_CORE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Datacenter Edition (core installation)" ));
               break;
            case PRODUCT_ENTERPRISE_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition" ));
               break;
            case PRODUCT_ENTERPRISE_SERVER_CORE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition (core installation)" ));
               break;
            case PRODUCT_ENTERPRISE_SERVER_IA64:
               StringCchCat(pszOS, BUFSIZE, TEXT("Enterprise Edition for Itanium-based Systems" ));
               break;
            case PRODUCT_SMALLBUSINESS_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server" ));
               break;
            case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
               StringCchCat(pszOS, BUFSIZE, TEXT("Small Business Server Premium Edition" ));
               break;
            case PRODUCT_STANDARD_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition" ));
               break;
            case PRODUCT_STANDARD_SERVER_CORE:
               StringCchCat(pszOS, BUFSIZE, TEXT("Standard Edition (core installation)" ));
               break;
            case PRODUCT_WEB_SERVER:
               StringCchCat(pszOS, BUFSIZE, TEXT("Web Server Edition" ));
               break;
         }
      }

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
      {
         if( GetSystemMetrics(SM_SERVERR2) )
            StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Server 2003 R2, "));
         else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER )
            StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Storage Server 2003"));
         else if ( osvi.wSuiteMask & VER_SUITE_WH_SERVER )
            StringCchCat(pszOS, BUFSIZE, TEXT( "Windows Home Server"));
         else if( osvi.wProductType == VER_NT_WORKSTATION &&
                  si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
         {
            StringCchCat(pszOS, BUFSIZE, TEXT( "Windows XP Professional x64 Edition"));
         }
         else StringCchCat(pszOS, BUFSIZE, TEXT("Windows Server 2003, "));

         // Test for the server type.
         if ( osvi.wProductType != VER_NT_WORKSTATION )
         {
            if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
            {
                if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition for Itanium-based Systems" ));
                else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition for Itanium-based Systems" ));
            }

            else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
            {
                if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter x64 Edition" ));
                else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise x64 Edition" ));
                else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard x64 Edition" ));
            }

            else
            {
                if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Compute Cluster Edition" ));
                else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Edition" ));
                else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Enterprise Edition" ));
                else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
                   StringCchCat(pszOS, BUFSIZE, TEXT( "Web Edition" ));
                else StringCchCat(pszOS, BUFSIZE, TEXT( "Standard Edition" ));
            }
         }
      }

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
      {
         StringCchCat(pszOS, BUFSIZE, TEXT("Windows XP "));
         if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
            StringCchCat(pszOS, BUFSIZE, TEXT( "Home Edition" ));
         else StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
      }

      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
      {
         StringCchCat(pszOS, BUFSIZE, TEXT("Windows 2000 "));

         if ( osvi.wProductType == VER_NT_WORKSTATION )
         {
            StringCchCat(pszOS, BUFSIZE, TEXT( "Professional" ));
         }
         else 
         {
            if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
               StringCchCat(pszOS, BUFSIZE, TEXT( "Datacenter Server" ));
            else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
               StringCchCat(pszOS, BUFSIZE, TEXT( "Advanced Server" ));
            else StringCchCat(pszOS, BUFSIZE, TEXT( "Server" ));
         }
      }

       // Include service pack (if any) and build number.

      if( _tcslen(osvi.szCSDVersion) > 0 )
      {
          StringCchCat(pszOS, BUFSIZE, TEXT(" ") );
          StringCchCat(pszOS, BUFSIZE, osvi.szCSDVersion);
      }

      TCHAR buf[80];

      StringCchPrintf( buf, 80, TEXT(" (build %d)"), osvi.dwBuildNumber);
      StringCchCat(pszOS, BUFSIZE, buf);

      if ( osvi.dwMajorVersion >= 6 )
      {
         if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
            StringCchCat(pszOS, BUFSIZE, TEXT( ", 64-bit" ));
         else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL )
            StringCchCat(pszOS, BUFSIZE, TEXT(", 32-bit"));
      }
      
      return TRUE; 
   }

   else
   {  
      printf( "This sample does not support this version of Windows.\n");
      return FALSE;
   }
}

int __cdecl _tmain()
{
    TCHAR szOS[BUFSIZE];

    if( GetOSDisplayString( szOS ) )
        _tprintf( TEXT("\n%s\n"), szOS );
}


 

 

Send comments about this topic to Microsoft

Build date: 8/11/2011

Community Content Add
Annotations FAQ
Error in code

if(bOsVersionInfoEx != NULL ) return 1;

must be

if(bOsVersionInfoEx != TRUE) return FALSE;

"Updated version" pure C note
This "Updated version" will not work for pure C mode compile. $0(Original also does not compile, but all you need is just move line TCHAR buf[80]; to the top of function body)$0 $0$0 $0 $0You do not need to use C++ *streams to get it work on Windows XP < SP2 and Windows 2000. $0$0 $0 $0Just replace StringCchCat with _tcscat_s and StringCchPrintf with _stprintf_s. Keep tchar.h included and remove include to Strsafe.h.$0 $0 $0$0 $0 $0Thats all.$0
Updated version

I've included fixes for all the bugs found here in the comments. Also removed the use of the antiquated function tcslen and the Strsafe function StringCchCat which is only supported in XP SP2 and later. This function should work for Windows 2000 through Windows 7.

#include <windows.h>
#include <string>
#include <sstream>
typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
#define PRODUCT_PROFESSIONAL 0x00000030
#define VER_SUITE_WH_SERVER 0x00008000
bool windowsVersionName(wchar_t* str, int bufferSize){
 OSVERSIONINFOEX osvi;
 SYSTEM_INFO si;
 BOOL bOsVersionInfoEx;
 DWORD dwType; ZeroMemory(&si, sizeof(SYSTEM_INFO));
 ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
 bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*) &osvi); if(bOsVersionInfoEx == 0)
  return false; // Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
 PGNSI pGNSI = (PGNSI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
 if(NULL != pGNSI)
  pGNSI(&si);
 else GetSystemInfo(&si); // Check for unsupported OS
 if (VER_PLATFORM_WIN32_NT != osvi.dwPlatformId || osvi.dwMajorVersion <= 4 ) {
  return false;
 } std::wstringstream os;
 os << L"Microsoft "; // Test for the specific product. if ( osvi.dwMajorVersion == 6 )
 {
  if( osvi.dwMinorVersion == 0 )
  {
   if( osvi.wProductType == VER_NT_WORKSTATION )
    os << "Windows Vista ";
   else os << "Windows Server 2008 ";
  }  if ( osvi.dwMinorVersion == 1 )
  {
   if( osvi.wProductType == VER_NT_WORKSTATION )
    os << "Windows 7 ";
   else os << "Windows Server 2008 R2 ";
  }  PGPI pGPI = (PGPI) GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo");
  pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);  switch( dwType )
  {
  case PRODUCT_ULTIMATE:
   os << "Ultimate Edition";
   break;
  case PRODUCT_PROFESSIONAL:
   os << "Professional";
   break;
  case PRODUCT_HOME_PREMIUM:
   os << "Home Premium Edition";
   break;
  case PRODUCT_HOME_BASIC:
   os << "Home Basic Edition";
   break;
  case PRODUCT_ENTERPRISE:
   os << "Enterprise Edition";
   break;
  case PRODUCT_BUSINESS:
   os << "Business Edition";
   break;
  case PRODUCT_STARTER:
   os << "Starter Edition";
   break;
  case PRODUCT_CLUSTER_SERVER:
   os << "Cluster Server Edition";
   break;
  case PRODUCT_DATACENTER_SERVER:
   os << "Datacenter Edition";
   break;
  case PRODUCT_DATACENTER_SERVER_CORE:
   os << "Datacenter Edition (core installation)";
   break;
  case PRODUCT_ENTERPRISE_SERVER:
   os << "Enterprise Edition";
   break;
  case PRODUCT_ENTERPRISE_SERVER_CORE:
   os << "Enterprise Edition (core installation)";
   break;
  case PRODUCT_ENTERPRISE_SERVER_IA64:
   os << "Enterprise Edition for Itanium-based Systems";
   break;
  case PRODUCT_SMALLBUSINESS_SERVER:
   os << "Small Business Server";
   break;
  case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
   os << "Small Business Server Premium Edition";
   break;
  case PRODUCT_STANDARD_SERVER:
   os << "Standard Edition";
   break;
  case PRODUCT_STANDARD_SERVER_CORE:
   os << "Standard Edition (core installation)";
   break;
  case PRODUCT_WEB_SERVER:
   os << "Web Server Edition";
   break;
  }
 } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 )
 {
  if( GetSystemMetrics(SM_SERVERR2) )
   os <<  "Windows Server 2003 R2, ";
  else if ( osvi.wSuiteMask & VER_SUITE_STORAGE_SERVER )
   os <<  "Windows Storage Server 2003";
  else if ( osvi.wSuiteMask & VER_SUITE_WH_SERVER )
   os <<  "Windows Home Server";
  else if( osvi.wProductType == VER_NT_WORKSTATION &&
   si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
  {
   os <<  "Windows XP Professional x64 Edition";
  }
  else os << "Windows Server 2003, ";  // Test for the server type.
  if ( osvi.wProductType != VER_NT_WORKSTATION )
  {
   if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64 )
   {
    if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
     os <<  "Datacenter Edition for Itanium-based Systems";
    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
     os <<  "Enterprise Edition for Itanium-based Systems";
   }   else if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
   {
    if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
     os <<  "Datacenter x64 Edition";
    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
     os <<  "Enterprise x64 Edition";
    else os <<  "Standard x64 Edition";
   }   else
   {
    if ( osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER )
     os <<  "Compute Cluster Edition";
    else if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
     os <<  "Datacenter Edition";
    else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
     os <<  "Enterprise Edition";
    else if ( osvi.wSuiteMask & VER_SUITE_BLADE )
     os <<  "Web Edition";
    else os <<  "Standard Edition";
   }
  }
 } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 )
 {
  os << "Windows XP ";
  if( osvi.wSuiteMask & VER_SUITE_PERSONAL )
   os <<  "Home Edition";
  else os <<  "Professional";
 } if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 )
 {
  os << "Windows 2000 ";  if ( osvi.wProductType == VER_NT_WORKSTATION )
  {
   os <<  "Professional";
  }
  else
  {
   if( osvi.wSuiteMask & VER_SUITE_DATACENTER )
    os <<  "Datacenter Server";
   else if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE )
    os <<  "Advanced Server";
   else os <<  "Server";
  }
 } // Include service pack (if any) and build number. if(wcslen(osvi.szCSDVersion) > 0) {
  os << " " << osvi.szCSDVersion;
 } os << L" (build " << osvi.dwBuildNumber << L")"; if ( osvi.dwMajorVersion >= 6 ) {
  if ( si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64 )
   os <<  ", 64-bit";
  else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL )
   os << ", 32-bit";
 } wcscpy_s(str, bufferSize, os.str().c_str());
 return true;
}
replace != with ==msdn.microsoft.com
  1. the line
if(bOsVersionInfoEx != NULL ) return 1;


is (and obviously so) wrong.

see GetVersionEx: http:///en-us/library/ms724451%28v=vs.85%29.aspx


If the function succeeds, the return value is a nonzero value.
If the function fails, the return value is zero.


This is correct (though bad).
bOsVersionInfoEx is defined as bool, so use it as bool:

if( ! bOsVersionInfoEx ) return 1;

This is more readable, and thus better.

Buggy code.
and of course this code will execute only under 2k8, vista, 2k8r2 and 7
wrong arguments
when this call is made: pGPI( osvi.dwMajorVersion, osvi.dwMinorVersion, 0, 0, &dwType);

the third and 4th parameters should be osvi.wServicePackMajor, osvi.wServicePackMinor instead of 0.
does not support windows server 2008 r2??

of course, 2k8r2 has the version 6.1.... and wProductType != 1
error C2065: 'PRODUCT_ULTIMATE' : undeclared identifier
This should be compiled under VC9.
These identifier is not defined under VC7. (in WinNT.h)



Q33NY:
inserting this line into your code would do
#define PRODUCT_PROFESSIONAL 0x00000030
Buggy code.
When you say:
"This code was posted untested and will not work properly on systems older than Windows Vista.
Windows XP does not have GetProductInfo function implemented. GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "GetProductInfo") sets pGPI to NULL and when pGPI is invoked, unhandled exception occurs."

Where is the bug? this code won't be executed on xp... this code is protected by the "if (osvi.dwMajorVersion == 6 )" clause...
precompiled ?
Will this source code be available precompiled and digitally signed by Microsoft ?
Having it digitally signed really would be great since avira and other programs start crying around if it finds a upx packed portable exe or even not packed self- programmed stuff.
In the c++ - compiler Dev c++ the already mentioned errors are popping up as well.

I am looking forward to a working /precompiled version ;-)

Should be changed to osvi.wSuiteMask&amp; from osvi.wSuiteMask==
In the above example, because the OSVERSIONINFOEX member .wSuiteMask is always set to a value of VER_SUITE_TERMINAL (0x00000010), the two instances of

osvi.wSuiteMask==

should be changed to

osvi.wSuiteMask&
Windows version from VB .Net

To use this as a VB .Net callable .dll, remove the _tmain() code, compile as a .dll, add the appropriate .def source, and, in your VB program, add the following reference:

Public Declare Function GetOSDisplayString _
Lib "WindowsVersion.dll" (<MarshalAs(UnmanagedType.LPWStr, SafeArraySubType:=Runtime.InteropServices.VarEnum.VT_BYREF)> ByVal oString As String) As Boolean
Dim oString As String = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" & _
" " & _
" " & _
" "
Dim iRet As Boolean = GetOSDisplayString(oString)

Since BUFSIZE is 256, oString should contain at least that many characters.