home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Misc / GetDXVer / getdxver.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-31  |  10.6 KB  |  316 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-2001 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.     HINSTANCE            hDPNHPASTDLL       = NULL;
  58.     LPDIRECTDRAW         pDDraw             = NULL;
  59.     LPDIRECTDRAW2        pDDraw2            = NULL;
  60.     LPDIRECTDRAWSURFACE  pSurf              = NULL;
  61.     LPDIRECTDRAWSURFACE3 pSurf3             = NULL;
  62.     LPDIRECTDRAWSURFACE4 pSurf4             = NULL;
  63.     DWORD                dwDXVersion        = 0;
  64.     HRESULT              hr;
  65.  
  66.     // First see if DDRAW.DLL even exists.
  67.     hDDrawDLL = LoadLibrary( "DDRAW.DLL" );
  68.     if( hDDrawDLL == NULL )
  69.     {
  70.         dwDXVersion = 0;
  71.         OutputDebugString( "Couldn't LoadLibrary DDraw\r\n" );
  72.         return dwDXVersion;
  73.     }
  74.  
  75.     // See if we can create the DirectDraw object.
  76.     DirectDrawCreate = (DIRECTDRAWCREATE)GetProcAddress( hDDrawDLL, "DirectDrawCreate" );
  77.     if( DirectDrawCreate == NULL )
  78.     {
  79.         dwDXVersion = 0;
  80.         FreeLibrary( hDDrawDLL );
  81.         OutputDebugString( "Couldn't GetProcAddress DirectDrawCreate\r\n" );
  82.         return dwDXVersion;
  83.     }
  84.  
  85.     hr = DirectDrawCreate( NULL, &pDDraw, NULL );
  86.     if( FAILED(hr) )
  87.     {
  88.         dwDXVersion = 0;
  89.         FreeLibrary( hDDrawDLL );
  90.         OutputDebugString( "Couldn't create DDraw\r\n" );
  91.         return dwDXVersion;
  92.     }
  93.  
  94.     // So DirectDraw exists.  We are at least DX1.
  95.     dwDXVersion = 0x100;
  96.  
  97.     // Let's see if IID_IDirectDraw2 exists.
  98.     hr = pDDraw->QueryInterface( IID_IDirectDraw2, (VOID**)&pDDraw2 );
  99.     if( FAILED(hr) )
  100.     {
  101.         // No IDirectDraw2 exists... must be DX1
  102.         pDDraw->Release();
  103.         FreeLibrary( hDDrawDLL );
  104.         OutputDebugString( "Couldn't QI DDraw2\r\n" );
  105.         return dwDXVersion;
  106.     }
  107.  
  108.     // IDirectDraw2 exists. We must be at least DX2
  109.     pDDraw2->Release();
  110.     dwDXVersion = 0x200;
  111.  
  112.  
  113.     //-------------------------------------------------------------------------
  114.     // DirectX 3.0 Checks
  115.     //-------------------------------------------------------------------------
  116.  
  117.     // DirectInput was added for DX3
  118.     hDInputDLL = LoadLibrary( "DINPUT.DLL" );
  119.     if( hDInputDLL == NULL )
  120.     {
  121.         // No DInput... must not be DX3
  122.         pDDraw->Release();
  123.         FreeLibrary( hDDrawDLL );
  124.         OutputDebugString( "Couldn't LoadLibrary DInput\r\n" );
  125.         return dwDXVersion;
  126.     }
  127.  
  128.     DirectInputCreate = (DIRECTINPUTCREATE)GetProcAddress( hDInputDLL,
  129.                                                         "DirectInputCreateA" );
  130.     if( DirectInputCreate == NULL )
  131.     {
  132.         // No DInput... must be DX2
  133.         FreeLibrary( hDInputDLL );
  134.         pDDraw->Release();
  135.         FreeLibrary( hDDrawDLL );
  136.         OutputDebugString( "Couldn't GetProcAddress DInputCreate\r\n" );
  137.         return dwDXVersion;
  138.     }
  139.  
  140.     // DirectInputCreate exists. We are at least DX3
  141.     dwDXVersion = 0x300;
  142.     FreeLibrary( hDInputDLL );
  143.  
  144.     // Can do checks for 3a vs 3b here
  145.  
  146.  
  147.     //-------------------------------------------------------------------------
  148.     // DirectX 5.0 Checks
  149.     //-------------------------------------------------------------------------
  150.  
  151.     // We can tell if DX5 is present by checking for the existence of
  152.     // IDirectDrawSurface3. First, we need a surface to QI off of.
  153.     DDSURFACEDESC ddsd;
  154.     ZeroMemory( &ddsd, sizeof(ddsd) );
  155.     ddsd.dwSize         = sizeof(ddsd);
  156.     ddsd.dwFlags        = DDSD_CAPS;
  157.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  158.  
  159.     hr = pDDraw->SetCooperativeLevel( NULL, DDSCL_NORMAL );
  160.     if( FAILED(hr) )
  161.     {
  162.         // Failure. This means DDraw isn't properly installed.
  163.         pDDraw->Release();
  164.         FreeLibrary( hDDrawDLL );
  165.         dwDXVersion = 0;
  166.         OutputDebugString( "Couldn't Set coop level\r\n" );
  167.         return dwDXVersion;
  168.     }
  169.  
  170.     hr = pDDraw->CreateSurface( &ddsd, &pSurf, NULL );
  171.     if( FAILED(hr) )
  172.     {
  173.         // Failure. This means DDraw isn't properly installed.
  174.         pDDraw->Release();
  175.         FreeLibrary( hDDrawDLL );
  176.         dwDXVersion = 0;
  177.         OutputDebugString( "Couldn't CreateSurface\r\n" );
  178.         return dwDXVersion;
  179.     }
  180.  
  181.     // Query for the IDirectDrawSurface3 interface
  182.     if( FAILED( pSurf->QueryInterface( IID_IDirectDrawSurface3,
  183.                                        (VOID**)&pSurf3 ) ) )
  184.     {
  185.         pSurf->Release();
  186.         pDDraw->Release();
  187.         FreeLibrary( hDDrawDLL );
  188.         OutputDebugString( "Couldn't QI DDS3\r\n" );
  189.         return dwDXVersion;
  190.     }
  191.  
  192.     // QI for IDirectDrawSurface3 succeeded. We must be at least DX5
  193.     dwDXVersion = 0x500;
  194.     pSurf3->Release();
  195.  
  196.  
  197.     //-------------------------------------------------------------------------
  198.     // DirectX 6.0 Checks
  199.     //-------------------------------------------------------------------------
  200.  
  201.     // The IDirectDrawSurface4 interface was introduced with DX 6.0
  202.     if( FAILED( pSurf->QueryInterface( IID_IDirectDrawSurface4,
  203.                                        (VOID**)&pSurf4 ) ) )
  204.     {
  205.         pSurf->Release();
  206.         pDDraw->Release();
  207.         FreeLibrary( hDDrawDLL );
  208.         OutputDebugString( "Couldn't QI DDS4\r\n" );
  209.         return dwDXVersion;
  210.     }
  211.  
  212.     // IDirectDrawSurface4 was create successfully. We must be at least DX6
  213.     dwDXVersion = 0x600;
  214.     pSurf4->Release();
  215.     pSurf->Release();
  216.     pDDraw->Release();
  217.  
  218.  
  219.     //-------------------------------------------------------------------------
  220.     // DirectX 6.1 Checks
  221.     //-------------------------------------------------------------------------
  222.  
  223.     // Check for DMusic, which was introduced with DX6.1
  224.     LPDIRECTMUSIC pDMusic = NULL;
  225.     CoInitialize( NULL );
  226.     hr = CoCreateInstance( CLSID_DirectMusic, NULL, CLSCTX_INPROC_SERVER,
  227.                            IID_IDirectMusic, (VOID**)&pDMusic );
  228.     if( FAILED(hr) )
  229.     {
  230.         FreeLibrary( hDDrawDLL );
  231.         OutputDebugString( "Couldn't create CLSID_DirectMusic\r\n" );
  232.         return dwDXVersion;
  233.     }
  234.  
  235.     // DirectMusic was created successfully. We must be at least DX6.1
  236.     dwDXVersion = 0x601;
  237.     pDMusic->Release();
  238.     CoUninitialize();
  239.     
  240.  
  241.     //-------------------------------------------------------------------------
  242.     // DirectX 7.0 Checks
  243.     //-------------------------------------------------------------------------
  244.  
  245.     // Check for DirectX 7 by creating a DDraw7 object
  246.     LPDIRECTDRAW7 pDD7;
  247.     DirectDrawCreateEx = (DIRECTDRAWCREATEEX)GetProcAddress( hDDrawDLL,
  248.                                                        "DirectDrawCreateEx" );
  249.     if( NULL == DirectDrawCreateEx )
  250.     {
  251.         FreeLibrary( hDDrawDLL );
  252.         OutputDebugString( "Couldn't GetProcAddress DirectDrawCreateEx\r\n" );
  253.         return dwDXVersion;
  254.     }
  255.  
  256.     if( FAILED( DirectDrawCreateEx( NULL, (VOID**)&pDD7, IID_IDirectDraw7,
  257.                                     NULL ) ) )
  258.     {
  259.         FreeLibrary( hDDrawDLL );
  260.         OutputDebugString( "Couldn't DirectDrawCreateEx\r\n" );
  261.         return dwDXVersion;
  262.     }
  263.  
  264.     // DDraw7 was created successfully. We must be at least DX7.0
  265.     dwDXVersion = 0x700;
  266.     pDD7->Release();
  267.  
  268.  
  269.     //-------------------------------------------------------------------------
  270.     // DirectX 8.0 Checks
  271.     //-------------------------------------------------------------------------
  272.  
  273.     // Simply see if D3D8.dll exists.
  274.     hD3D8DLL = LoadLibrary( "D3D8.DLL" );
  275.     if( hD3D8DLL == NULL )
  276.     {
  277.         FreeLibrary( hDDrawDLL );
  278.         OutputDebugString( "Couldn't LoadLibrary D3D8.DLL\r\n" );
  279.         return dwDXVersion;
  280.     }
  281.  
  282.     // D3D8.dll exists. We must be at least DX8.0
  283.     dwDXVersion = 0x800;
  284.  
  285.  
  286.     //-------------------------------------------------------------------------
  287.     // DirectX 8.1 Checks
  288.     //-------------------------------------------------------------------------
  289.  
  290.     // Simply see if dpnhpast.dll exists.
  291.     hDPNHPASTDLL = LoadLibrary( "dpnhpast.dll" );
  292.     if( hDPNHPASTDLL == NULL )
  293.     {
  294.         FreeLibrary( hDPNHPASTDLL );
  295.         OutputDebugString( "Couldn't LoadLibrary dpnhpast.dll\r\n" );
  296.         return dwDXVersion;
  297.     }
  298.  
  299.     // dpnhpast.dll exists. We must be at least DX8.1
  300.     dwDXVersion = 0x801;
  301.  
  302.  
  303.     //-------------------------------------------------------------------------
  304.     // End of checking for versions of DirectX 
  305.     //-------------------------------------------------------------------------
  306.  
  307.     // Close open libraries and return
  308.     FreeLibrary( hDDrawDLL );
  309.     FreeLibrary( hD3D8DLL );
  310.     
  311.     return dwDXVersion;
  312. }
  313.  
  314.  
  315.  
  316.