home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / directx / dxf / samples / multimedia / misc / getdxver / getdxver.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  9.5 KB  |  287 lines

  1. //-----------------------------------------------------------------------------
  2. // File: GetDXVer.cpp
  3. //
  4. // Desc: Demonstrates how applications can detect what version of DirectX
  5. //       is installed.
  6. //
  7. // (C) Copyright 1995-2000 Microsoft Corp.  All rights reserved.
  8. //-----------------------------------------------------------------------------
  9. #include <windows.h>
  10. #include <windowsx.h>
  11. #include <basetsd.h>
  12. #include <ddraw.h>
  13. #include <dinput.h>
  14. #include <dmusici.h>
  15.  
  16.  
  17.  
  18.  
  19. typedef HRESULT(WINAPI * DIRECTDRAWCREATE)( GUID*, LPDIRECTDRAW*, IUnknown* );
  20. typedef HRESULT(WINAPI * DIRECTDRAWCREATEEX)( GUID*, VOID**, REFIID, IUnknown* );
  21. typedef HRESULT(WINAPI * DIRECTINPUTCREATE)( HINSTANCE, DWORD, LPDIRECTINPUT*,
  22.                                              IUnknown* );
  23.  
  24.  
  25.  
  26.  
  27. //-----------------------------------------------------------------------------
  28. // Name: GetDXVersion()
  29. // Desc: This function returns the DirectX version number as follows:
  30. //          0x0000 = No DirectX installed
  31. //          0x0100 = DirectX version 1 installed
  32. //          0x0200 = DirectX 2 installed
  33. //          0x0300 = DirectX 3 installed
  34. //          0x0500 = At least DirectX 5 installed.
  35. //          0x0600 = At least DirectX 6 installed.
  36. //          0x0601 = At least DirectX 6.1 installed.
  37. //          0x0700 = At least DirectX 7 installed.
  38. //          0x0800 = At least DirectX 8 installed.
  39. // 
  40. //       Please note that this code is intended as a general guideline. Your
  41. //       app will probably be able to simply query for functionality (via
  42. //       QueryInterface) for one or two components.
  43. //
  44. //       Please also note:
  45. //          "if( dwDXVersion != 0x500 ) return FALSE;" is VERY BAD. 
  46. //          "if( dwDXVersion <  0x500 ) return FALSE;" is MUCH BETTER.
  47. //       to ensure your app will run on future releases of DirectX.
  48. //-----------------------------------------------------------------------------
  49. DWORD GetDXVersion()
  50. {
  51.     DIRECTDRAWCREATE     DirectDrawCreate   = NULL;
  52.     DIRECTDRAWCREATEEX   DirectDrawCreateEx = NULL;
  53.     DIRECTINPUTCREATE    DirectInputCreate  = NULL;
  54.     HINSTANCE            hDDrawDLL          = NULL;
  55.     HINSTANCE            hDInputDLL         = NULL;
  56.     HINSTANCE            hD3D8DLL           = NULL;
  57.     LPDIRECTDRAW         pDDraw             = NULL;
  58.     LPDIRECTDRAW2        pDDraw2            = NULL;
  59.     LPDIRECTDRAWSURFACE  pSurf              = NULL;
  60.     LPDIRECTDRAWSURFACE3 pSurf3             = NULL;
  61.     LPDIRECTDRAWSURFACE4 pSurf4             = NULL;
  62.     DWORD                dwDXVersion        = 0;
  63.     HRESULT              hr;
  64.  
  65.     // First see if DDRAW.DLL even exists.
  66.     hDDrawDLL = LoadLibrary( "DDRAW.DLL" );
  67.     if( hDDrawDLL == NULL )
  68.     {
  69.         dwDXVersion = 0;
  70.         return dwDXVersion;
  71.     }
  72.  
  73.     // See if we can create the DirectDraw object.
  74.     DirectDrawCreate = (DIRECTDRAWCREATE)GetProcAddress( hDDrawDLL, "DirectDrawCreate" );
  75.     if( DirectDrawCreate == NULL )
  76.     {
  77.         dwDXVersion = 0;
  78.         FreeLibrary( hDDrawDLL );
  79.         OutputDebugString( "Couldn't LoadLibrary DDraw\r\n" );
  80.         return dwDXVersion;
  81.     }
  82.  
  83.     hr = DirectDrawCreate( NULL, &pDDraw, NULL );
  84.     if( FAILED(hr) )
  85.     {
  86.         dwDXVersion = 0;
  87.         FreeLibrary( hDDrawDLL );
  88.         OutputDebugString( "Couldn't create DDraw\r\n" );
  89.         return dwDXVersion;
  90.     }
  91.  
  92.     // So DirectDraw exists.  We are at least DX1.
  93.     dwDXVersion = 0x100;
  94.  
  95.     // Let's see if IID_IDirectDraw2 exists.
  96.     hr = pDDraw->QueryInterface( IID_IDirectDraw2, (VOID**)&pDDraw2 );
  97.     if( FAILED(hr) )
  98.     {
  99.         // No IDirectDraw2 exists... must be DX1
  100.         pDDraw->Release();
  101.         FreeLibrary( hDDrawDLL );
  102.         OutputDebugString( "Couldn't QI DDraw2\r\n" );
  103.         return dwDXVersion;
  104.     }
  105.  
  106.     // IDirectDraw2 exists. We must be at least DX2
  107.     pDDraw2->Release();
  108.     dwDXVersion = 0x200;
  109.  
  110.  
  111.     //-------------------------------------------------------------------------
  112.     // DirectX 3.0 Checks
  113.     //-------------------------------------------------------------------------
  114.  
  115.     // DirectInput was added for DX3
  116.     hDInputDLL = LoadLibrary( "DINPUT.DLL" );
  117.     if( hDInputDLL == NULL )
  118.     {
  119.         // No DInput... must not be DX3
  120.         OutputDebugString( "Couldn't LoadLibrary DInput\r\n" );
  121.         pDDraw->Release();
  122.         return dwDXVersion;
  123.     }
  124.  
  125.     DirectInputCreate = (DIRECTINPUTCREATE)GetProcAddress( hDInputDLL,
  126.                                                         "DirectInputCreateA" );
  127.     if( DirectInputCreate == NULL )
  128.     {
  129.         // No DInput... must be DX2
  130.         FreeLibrary( hDInputDLL );
  131.         FreeLibrary( hDDrawDLL );
  132.         pDDraw->Release();
  133.         OutputDebugString( "Couldn't GetProcAddress DInputCreate\r\n" );
  134.         return dwDXVersion;
  135.     }
  136.  
  137.     // DirectInputCreate exists. We are at least DX3
  138.     dwDXVersion = 0x300;
  139.     FreeLibrary( hDInputDLL );
  140.  
  141.     // Can do checks for 3a vs 3b here
  142.  
  143.  
  144.     //-------------------------------------------------------------------------
  145.     // DirectX 5.0 Checks
  146.     //-------------------------------------------------------------------------
  147.  
  148.     // We can tell if DX5 is present by checking for the existence of
  149.     // IDirectDrawSurface3. First, we need a surface to QI off of.
  150.     DDSURFACEDESC ddsd;
  151.     ZeroMemory( &ddsd, sizeof(ddsd) );
  152.     ddsd.dwSize         = sizeof(ddsd);
  153.     ddsd.dwFlags        = DDSD_CAPS;
  154.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  155.  
  156.     hr = pDDraw->SetCooperativeLevel( NULL, DDSCL_NORMAL );
  157.     if( FAILED(hr) )
  158.     {
  159.         // Failure. This means DDraw isn't properly installed.
  160.         pDDraw->Release();
  161.         FreeLibrary( hDDrawDLL );
  162.         dwDXVersion = 0;
  163.         OutputDebugString( "Couldn't Set coop level\r\n" );
  164.         return dwDXVersion;
  165.     }
  166.  
  167.     hr = pDDraw->CreateSurface( &ddsd, &pSurf, NULL );
  168.     if( FAILED(hr) )
  169.     {
  170.         // Failure. This means DDraw isn't properly installed.
  171.         pDDraw->Release();
  172.         FreeLibrary( hDDrawDLL );
  173.         dwDXVersion = 0;
  174.         OutputDebugString( "Couldn't CreateSurface\r\n" );
  175.         return dwDXVersion;
  176.     }
  177.  
  178.     // Query for the IDirectDrawSurface3 interface
  179.     if( FAILED( pSurf->QueryInterface( IID_IDirectDrawSurface3,
  180.                                        (VOID**)&pSurf3 ) ) )
  181.     {
  182.         pDDraw->Release();
  183.         FreeLibrary( hDDrawDLL );
  184.         return dwDXVersion;
  185.     }
  186.  
  187.     // QI for IDirectDrawSurface3 succeeded. We must be at least DX5
  188.     dwDXVersion = 0x500;
  189.  
  190.  
  191.     //-------------------------------------------------------------------------
  192.     // DirectX 6.0 Checks
  193.     //-------------------------------------------------------------------------
  194.  
  195.     // The IDirectDrawSurface4 interface was introduced with DX 6.0
  196.     if( FAILED( pSurf->QueryInterface( IID_IDirectDrawSurface4,
  197.                                        (VOID**)&pSurf4 ) ) )
  198.     {
  199.         pDDraw->Release();
  200.         FreeLibrary( hDDrawDLL );
  201.         return dwDXVersion;
  202.     }
  203.  
  204.     // IDirectDrawSurface4 was create successfully. We must be at least DX6
  205.     dwDXVersion = 0x600;
  206.     pSurf->Release();
  207.     pDDraw->Release();
  208.  
  209.  
  210.     //-------------------------------------------------------------------------
  211.     // DirectX 6.1 Checks
  212.     //-------------------------------------------------------------------------
  213.  
  214.     // Check for DMusic, which was introduced with DX6.1
  215.     LPDIRECTMUSIC pDMusic = NULL;
  216.     CoInitialize( NULL );
  217.     hr = CoCreateInstance( CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER,
  218.                            IID_IDirectMusic, (VOID**)&pDMusic );
  219.     if( FAILED(hr) )
  220.     {
  221.         OutputDebugString( "Couldn't create CLSID_DirectMusic\r\n" );
  222.         FreeLibrary( hDDrawDLL );
  223.         return dwDXVersion;
  224.     }
  225.  
  226.     // DirectMusic was created successfully. We must be at least DX6.1
  227.     dwDXVersion = 0x601;
  228.     pDMusic->Release();
  229.     CoUninitialize();
  230.     
  231.  
  232.     //-------------------------------------------------------------------------
  233.     // DirectX 7.0 Checks
  234.     //-------------------------------------------------------------------------
  235.  
  236.     // Check for DirectX 7 by creating a DDraw7 object
  237.     LPDIRECTDRAW7 pDD7;
  238.     DirectDrawCreateEx = (DIRECTDRAWCREATEEX)GetProcAddress( hDDrawDLL,
  239.                                                        "DirectDrawCreateEx" );
  240.     if( NULL == DirectDrawCreateEx )
  241.     {
  242.         FreeLibrary( hDDrawDLL );
  243.         return dwDXVersion;
  244.     }
  245.  
  246.     if( FAILED( DirectDrawCreateEx( NULL, (VOID**)&pDD7, IID_IDirectDraw7,
  247.                                     NULL ) ) )
  248.     {
  249.         FreeLibrary( hDDrawDLL );
  250.         return dwDXVersion;
  251.     }
  252.  
  253.     // DDraw7 was created successfully. We must be at least DX7.0
  254.     dwDXVersion = 0x700;
  255.     pDD7->Release();
  256.  
  257.  
  258.     //-------------------------------------------------------------------------
  259.     // DirectX 8.0 Checks
  260.     //-------------------------------------------------------------------------
  261.  
  262.     // Simply see if D3D8.dll exists.
  263.     hD3D8DLL = LoadLibrary( "D3D8.DLL" );
  264.     if( hD3D8DLL == NULL )
  265.     {
  266.         FreeLibrary( hDDrawDLL );
  267.         return dwDXVersion;
  268.     }
  269.  
  270.     // D3D8.dll exists. We must be at least DX8.0
  271.     dwDXVersion = 0x800;
  272.  
  273.  
  274.     //-------------------------------------------------------------------------
  275.     // End of checking for versions of DirectX 
  276.     //-------------------------------------------------------------------------
  277.  
  278.     // Close open libraries and return
  279.     FreeLibrary( hDDrawDLL );
  280.     FreeLibrary( hD3D8DLL );
  281.     
  282.     return dwDXVersion;
  283. }
  284.  
  285.  
  286.  
  287.