DirectX SDK |
In order to scale their functionality to the version of DirectX currently running on the user's system, applications may need to know what version of DirectX is currently installed.
The DirectX software development kit includes a sample application with source code at (SDK root) \Samples\Multimedia\DXMisc\Src\GetDXVer that determines whether DirectX is on the system and, if so, what version it is. Each new version of DirectX supports all the functionality that was in older versions, so be sure to test for the desired version or later, so that your application will continue to run on future versions.
You can also test for particular functionality by using QueryInterface. For example, suppose your application is designed to run on DirectX 3.0 or later, but has optional force-feedback effects supported only by DirectX 5.0 and later. You can determine whether support for force feedback is available by obtaining the IDirectInputDevice interface and then querying for IDirectInputDevice2.
For a variety of reasons applications may also want to determine what version of the operating system is present. To provide for easier migration to future versions of components or versions of the Windows operating system, applications should check for the earliest compliant version, and allow for installation and execution on later versions. Knowing that Windows 2000 will contain DirectX 7.0 functionality or greater means developers should write code that detects the presence of Windows 2000 or greater and allows the application to run if it is found. Conversely, if Windows NT 4.0 is the version detected, DirectX 7.0 functionality is not available.
The following sample function returns TRUE if the operating system is the same as or later than the one indicated in the parameters, which indicate the major version, minor version, and service pack.
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; }