Creating DirectDraw Objects Using CoCreateInstance

You can create a DirectDraw object using CoCreateInstance and the IDirectDraw::Initialize method rather than the DirectDrawCreate function. The following steps describe how to create the DirectDraw object:

1Initialize COM at the start of your application using CoInitialize(NULL).

if (FAILED(CoInitialize(NULL)))

return FALSE;

2Create your DirectDraw object using CoCreateInstance and the IDirectDraw::Initialize method.

ddrval = CoCreateInstance(&CLSID_DirectDraw,

NULL,

CLSCTX_ALL,

&IID_IDirectDraw,

&lpdd);

if( !FAILED(ddrval) )

ddrval = IDirectDraw_Initialize( lpdd, NULL);

CLSID_DirectDraw is the class identifier of the DirectDraw driver object class and IID_IDirectDraw is the particular DirectDraw interface you want. lpdd is the DirectDraw object returned. CoCreateInstance returns an uninitialized object.

3Before you use the DirectDraw object, you must call IDirectDraw::Initialize. This method takes the driver GUID parameter that the DirectDrawCreate function typically uses (NULL in this case). Once the DirectDraw object is initialized, you can use and release the DirectDraw object as if it had been created using the DirectDrawCreate function. If you do not call the IDirectDraw::Initialize method before using one of the methods associated with the DirectDraw object, a DDERR_NOTINITIALIZED error will occur.

Before closing the application, shut down COM using CoUninitialize, as shown below.

CoUnitialize();