?/TD>
Microsoft DirectX 9.0

Version Checking


Applications sometimes need to know which version of Microsoft?DirectX?is currently available on the system. For example, if an older version of DirectX is on the system, your application may need to scale itself to the capabilities of that version or install the most recent version.

DirectX 9.0 supports the following operating systems. The table includes which version of DirectX shipped with each operating system.

Operating SystemDirectX Version
Microsoft Windows?98 GoldDirectX 5.2
Windows 98 SEDirectX 6.1a
Windows 2000DirectX 7.0
Windows Millennium Edition (Windows Me)DirectX 7.1
Windows XPDirectX 8.1

There is no direct way to obtain the DirectX version number. However, each version has a characteristic set of objects and interfaces. Because any version of DirectX supports all previous versions, this set of interfaces and objects will be supported by the version in which they are introduced and all subsequent versions. Thus, the preferred way to determine whether your desired version is available is to test for its characteristic objects or interfaces. As long as those are present, your application will work normally even though you might be using a more recent version of DirectX.

For example, suppose you need DirectX 6.1 support. The Microsoft DirectMusic?object (CLSID_DirectMusic) was introduced in DirectX 6.1. You can test for the presence of the DirectMusic object by attempting to create it with CoCreateInstance. If you are successful, you have version 6.1 or later, and you will be able to use all the DirectX 6.1 capabilities.

Rather than provide a detailed list here of each version's characteristic interfaces and objects, you should refer to the DirectX software development kits (SDKs) sample section. One of the samples is a function, GetDXVersion, that includes tests for all DirectX versions. GetDXVersion returns an integer that corresponds to the DirectX version that is present on the system. As long as this integer is greater than or equal to your desired version number, your application will run normally. You can find the sample code under your SDK root folder at \Samples\Multimedia\DXMisc\GetDXVer.

Checking the Operating System Version

In addition to determining the DirectX version, applications may also need to know what operating system they are running on, and perhaps which Service Pack has been installed. The preferred way to check the operating system version is to use the Windows function, GetVersionEx. This function returns an OSVERSIONINFO structure that contains a variety of information including:

The general procedure is to determine the earliest version of the operating system that your application is compliant with. If that version or later is installed, you can safely install and run your application.

Note  The operating system information returned by GetVersionEx should not be used to test for the presence or version number of DirectX. In particular, the fact that a system is Windows NT-based does not necessarily mean that DirectX is absent. The Windows 2000 operating system, for example, includes DirectX 7.0 or later. New versions of Windows NT-based operating systems can also be expected to include a version of DirectX. Use the procedures described in the previous section to determine whether DirectX is present and, if so, the version number.

The following sample function illustrates how to use GetVersionEx to test the operating system version. If the installed version is identical to or more recent than the version specified in the parameter list, the function returns TRUE. You can then safely install and run your application. Otherwise the function returns FALSE, indicating that your application will not run properly, if at all.

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

BOOL bIsWindowsVersionOK(DWORD dwWin9xMajor,
                         DWORD dwWin9xMinor,
                         DWORD dwWinNTMajor,
                         DWORD dwWinNTMinor,
                         WORD wWinNTSPMajor )

{
    OSVERSIONINFO           osvi;
	
    // Initialize the OSVERSIONINFO structure.

    ZeroMemory( &osvi, sizeof( osvi ) );
    osvi.dwOSVersionInfoSize = sizeof( osvi );

    GetVersionEx( &osvi );  // Assume this function succeeds.

    // Split code paths for NT and Windows 9x    
    if( osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS )
    {
        // Check the major version.
        if( osvi.dwMajorVersion > dwWin9xMajor )
            return TRUE;

        else if( osvi.dwMajorVersion == dwWin9xMajor )
        {
            // Check the minor version.
            if( osvi.dwMinorVersion >= dwWin9xMinor )
                return TRUE;
        }
    }

    else if( osvi.dwPlatformId == VER_PLATFORM_WIN32_NT )
    {
        // Check the major version.
        if( osvi.dwMajorVersion > dwWinNTMajor )
            return TRUE;

        else if( osvi.dwMajorVersion == dwWinNTMajor )
        {
            // Check the minor version.
            if( osvi.dwMinorVersion > dwWinNTMinor )
                return TRUE;

            else if( osvi.dwMinorVersion == dwWinNTMinor )
            {
                // Check the service pack.

                DWORD dwServicePack = 0;

                if( osvi.szCSDVersion )
                {
                    _stscanf(osvi.szCSDVersion,
                             _T("Service Pack %d"),
                             &dwServicePack );
                }
                return ( dwServicePack >= wWinNTSPMajor );
            }
        }
    }
    return FALSE;
}


© 2002 Microsoft Corporation. All rights reserved.