Microsoft DirectX 8.1 (C++)

Using ATL with DirectX 8.x COM Interfaces

To use the Active Template Library (ATL) with Microsoft® DirectX® 8.x, you must redeclare the interfaces for ATL compatibility. This will allow you to properly use the CComQIPtr class to obtain a pointer to an interface.

If you do not redeclare the interfaces for ATL, you will get the following error message.

C:\Program Files\Microsoft Visual Studio\VC98\ATL\INCLUDE\atlbase.h(566) :
  error C2787: 'IDirectXFileData' : no GUID has been associated with this object

The following code sample shows two ways to redeclare the IDirectXFileData interface.

// Method 1: Explicit declaration
struct __declspec(uuid("{3D82AB44-62DA-11CF-AB39-0020AF71E433}")) IDirectXFileData;

// Method 2: Macro method
#define DECLARE_IID(iid_, name_) struct __declspec(uuid(iid_)) name_
DECLARE_IID("{1DD9E8DA-1C77-4D40-B0CF-98FEFDFF9512}", IDirectXFileData);

After redeclaring the interface, you must use the Attach method to attach the interface to the interface pointer returned by ::Direct3DCreate8. If you do not, the IDirect3D8 interface will not be properly released by the smart pointer class.

The CComPtr class internally calls AddRef on the interface pointer when the object is created and when an interface is assigned to the CComPtr class. To avoid leaking the interface pointer, do not call AddRef on the interface returned from ::Direct3DCreate8.

The following code properly releases the interface without doing AddRef.

CComPtr<IDirect3D8> d3d;
d3d.Attach(::Direct3DCreate8(D3D_SDK_VERSION));

Use the previous code. Do not use the following code, which calls AddRef followed by Release and does not release the reference added by ::Direct3DCreate8.

CComPtr<IDirect3D8> d3d = ::Direct3DCreate8(D3D_SDK_VERSION);

Note that this is the only place in Microsoft Direct3D® where you will have to use the Attach method in this manner.

For more information about the CComPTR and CComQIPtr classes, see their definitions in the Atlbase.h header file.