INFO: Windows Version CheckingID: Q225013
|
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.
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);
}
For additional information about checking system versions, please see the following article(s) in the Microsoft Knowledge Base:
Q92395 Determining System Version from a Win32-based Application
Additional query words: compatguidesetup
Keywords : kbAppSetup kbWinOS2000 kbSDKWin32 kbWinOS kbGrpKernBase
Version : winnt:
Platform : winnt
Issue type : kbinfo
Last Reviewed: May 25, 1999