Determining System Version from a Win32-based Application

ID: Q92395

1.20 1.25a 1.30 4.00 | 3.10 3.50 3.51

WINDOWS              | WINDOWS NT
kbprg

The information in this article applies to:

SUMMARY

In order to create a Win32-based application that takes advantage of the features of each platform, it is necessary to determine the operating system on which the application is currently running.

You can use GetVersion() or GetVersionEx() to determine what operating system and version your application is running under. NOTE: GetVersion() is supported on Windows 3.1, but GetVersionEx() is new to the Win32 API. A Win32-based application might be running under MS-DOS/Windows using the Win32s extension, Windows NT Workstation, Windows NT Server, or Windows 95.

MORE INFORMATION

According to the documentation, the return value of GetVersion() is a DWORD that specifies the major and minor version numbers. GetVersionEx() uses members of the OSVERSIONINFO structure (dwMajorVersion and dwMinorVersion).

The following table shows the return values from GetVersion() under various environments:

+------------------------------------------------------------------+
|  Environment    |  LOWORD                | HIWORD                |
+==================================================================+
|  Win32s on      |  Windows version 3.1   | RESERVED *            |
|  Windows 3.1    |                        |                       |
+------------------------------------------------------------------+
|  Windows NT     |  Windows version       | RESERVED **           |
|                 |                        |                       |
+------------------------------------------------------------------+
|  Windows 95     |  Windows version 4.0   | RESERVED ***          |
|                 |                        |                       |
+------------------------------------------------------------------+

  * The highest bit is 1. The remaining bits specify build number.
    Note that the version of MS-DOS cannot be determined as it can under
    Windows 3.x.
 ** The highest bit is 0. The remaining bits specify build number.
*** The highest bit is 1. The remaining bits are reserved.

The following sample code can be used to test the values returned by GetVersion().

Sample Code 1

#include <windows.h>

void main()
{

   DWORD dwVersion;
   char szVersion[80];

   dwVersion = GetVersion();

   if (dwVersion < 0x80000000) {
   // Windows NT
       wsprintf (szVersion, "Microsoft Windows NT %u.%u (Build: %u)",
                (DWORD)(LOBYTE(LOWORD(dwVersion))),
                (DWORD)(HIBYTE(LOWORD(dwVersion))),
                (DWORD)(HIWORD(dwVersion)));
   }
   else if (LOBYTE(LOWORD(dwVersion))<4) {
   // Win32s
       wsprintf (szVersion, "Microsoft Win32s %u.%u (Build: %u)",
                (DWORD)(LOBYTE(LOWORD(dwVersion))),
                (DWORD)(HIBYTE(LOWORD(dwVersion))),
                (DWORD)(HIWORD(dwVersion) & ~0x8000));
   } else {
   // Windows 95
       wsprintf (szVersion, "Microsoft Windows 95 %u.%u (Build: %u)",
                (DWORD)(LOBYTE(LOWORD(dwVersion))),
                (DWORD)(HIBYTE(LOWORD(dwVersion))),
                (DWORD)(HIWORD(dwVersion) & ~0x8000));
   }

   MessageBox( NULL, szVersion, "Version Check", MB_OK );
}

The following sample code can be used to test the values returned by GetVersionEx(). NOTE: The actual build number is derived by masking dwBuildNumber with 0xFFFF.

Sample Code 2

{

   OSVERSIONINFO osvi;
   char  szVersion [80];

   memset(&osvi, 0, sizeof(OSVERSIONINFO));
   osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
   GetVersionEx (&osvi);

   if (osvi.dwPlatformId == VER_PLATFORM_WIN32s)
      wsprintf (szVersion, "Microsoft Win32s %d.%d (Build %d)",
                osvi.dwMajorVersion,
                osvi.dwMinorVersion,
                osvi.dwBuildNumber & 0xFFFF);

   else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
      wsprintf (szVersion, "Microsoft Windows 95 %d.%d (Build %d)",
                osvi.dwMajorVersion,
                osvi.dwMinorVersion,
                osvi.dwBuildNumber & 0xFFFF);

   else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
      wsprintf (szVersion, "Microsoft Windows NT %d.%d (Build %d)",
                osvi.dwMajorVersion,
                osvi.dwMinorVersion,
                osvi.dwBuildNumber & 0xFFFF);

   MessageBox( NULL, szVersion, "Version Check", MB_OK );
}

In order to distinguish between Windows NT Workstation and Windows NT Server, use the registry API to query the following:

   \HKEY_LOCAL_MACHINE\SYSTEM
   \CurrentControlSet
   \Control
   \ProductOptions

The result will be one of the following:

   WINNT      Windows NT Workstation is running.
   SERVERNT   Windows NT Server (3.5 or later) is running.
   LANMANNT   Windows NT Advanced Server (3.1) is running.

Additional reference words: 1.20 1.30 3.10 3.50 4.00 detect KBCategory: kbprg KBSubcategory: BseMisc
Keywords          : kbnokeyword kbKernBase kbGrpKernBase 
Version           : 1.20 1.25a 1.30 4.00 | 3.10 3.50
Platform          : NT WINDOWS

Last Reviewed: May 4, 1996