Microsoft KB Archive/225013

{|
 * width="100%"|

INFO: Windows Version Checking

 * }

Q225013

This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about obtaining support for a Beta release, please see the documentation included with the Beta product files, or check the Web location from which you downloaded the release.

-

The information in this article applies to:


 * Microsoft Win32 Application Programming Interface (API), included with:
 * the operating system: Microsoft Windows 2000

-

SUMMARY
When you check the current version of Windows in code, verify that it is the version your application requires or a later version unless the behavior of your application is version- or operating system-specific. Migrating your application to Windows 2000 will be most viable if your application runs on both Windows NT 4.0 and Windows 98.

You can use GetVersionEx in the Win32 API or the new Windows 2000 API called VerifyVersionInfo to determine the operating system version.

MORE INFORMATION
Many applications consider only existing Windows versions when making decisions based on operating system information. To provide for easier migration to future versions of Windows, design your applications to check for the earliest compliant version of Windows, and allow for installation and execution on later versions.

Unless you need to install a different version of your application on Windows NT versus Windows 95 or Windows 98, it is not necessary to determine the specific operating system. However, test your applications in both environments to insure portability.

The following sample code uses GetVersionEx to check the current version of Windows:

BOOL bIsWindowsVersionOK(DWORD dwMajor, DWORD dwMinor, WORD dwSPMajor) {   OSVERSIONINFO osvi; // Initialize the OSVERSIONINFO structure.// ZeroMemory(&osvi, sizeof(OSVERSIONINFO));

osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx((OSVERSIONINFO*)&osvi); // Check the major version. if (osvi.dwMajorVersion > dwMajor) return TRUE; else if (osvi.dwMajorVersion == dwMajor) {       // Check the minor version. if (osvi.dwMinorVersion > dwMinor) return TRUE; else if (osvi.dwMinorVersion == dwMinor) {           // Check the Service Pack. if (dwSPMajor && osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) {               HKEY    hKey; DWORD  dwCSDVersion; DWORD  dwSize; BOOL   fMeetsSPRequirement = FALSE;

if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Windows", 0,                       KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) {                   dwSize = sizeof(dwCSDVersion); if (RegQueryValueEx (hKey, "CSDVersion", NULL, NULL, (unsigned char*)&dwCSDVersion, &dwSize) == ERROR_SUCCESS) {                       fMeetsSPRequirement = (LOWORD(dwCSDVersion)  >= dwSPMajor); }                   RegCloseKey(hKey); }               return fMeetsSPRequirement; }           return TRUE; }   }    return FALSE; } If you are developing an application on Windows 2000 and your application does not need to be backwards compatible, you can use the new API called VerifyVersionInfo. The following sample code will run on Windows 2000:

 BOOL bIsWindowsVersionOK(DWORD dwMajor, DWORD dwMinor, WORD dwSPMajor) {   DWORDLONG dwlConditionMask = 0; OSVERSIONINFOEX osvi; ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));

osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); osvi.dwMajorVersion = dwMajor; osvi.dwMinorVersion = dwMinor; osvi.wServicePackMajor = dwSPMajor;// Set up the condition mask.

VER_SET_CONDITION(dwlConditionMask, VER_MAJORVERSION, VER_GREATER_EQUAL); VER_SET_CONDITION(dwlConditionMask, VER_MINORVERSION, VER_GREATER_EQUAL); VER_SET_CONDITION(dwlConditionMask, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);

// Perform the test. return VerifyVersionInfo(&osvi, VER_MAJORVERSION                                   | VER_MINORVERSION                                    | VER_SERVICEPACKMAJOR,                                    dwlConditionMask); }