Microsoft KB Archive/92395

From BetaArchive Wiki

HOWTO: Determining System Version from a Win32-based Application

ID: Q92395



The information in this article applies to:

  • Microsoft Win32 Application Programming Interface (API), used with:
    • Microsoft Windows NT
    • Microsoft Windows versions 95, 98
    • Microsoft Win32s versions 1.25, 1.3
    • Microsoft Windows 2000




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, Windows 2000, Windows 95, or Windows 98.


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 3.1 Windows version 3.1 RESERVED *
Windows NT Windows Version RESERVED **
Windows 2000 Windows Version RESERVED **
Windows 95 Windows version 4.0 RESERVED ***
Windows 98 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 / 2000
       wsprintf (szVersion, "Microsoft Windows NT/2000 %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/98
       wsprintf (szVersion, "Microsoft Windows 95/98 %u.%u",
                (DWORD)(LOBYTE(LOWORD(dwVersion))),
                (DWORD)(HIBYTE(LOWORD(dwVersion))));
   }

   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

#include <windows.h>

void main()
{
   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/98 %d.%d (Build %d)",
                osvi.dwMajorVersion,
                osvi.dwMinorVersion,
                osvi.dwBuildNumber & 0xFFFF);
   else if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT)
      wsprintf (szVersion, "Microsoft Windows NT/2000 %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 query words: 1.20 3.10 3.50 4.00 detect

Keywords          : kbnokeyword kbKernBase kbWin32s kbWinOS95 kbWinOS98 kbGrpKernBase 
Version           : winnt:
Platform          : winnt 
Issue type        : kbhowto 

Last Reviewed: October 14, 1999
© 1999 Microsoft Corporation. All rights reserved. Terms of Use.