### Creating DirectDraw Objects by Using CoCreateInstance

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

1. Initialize COM at the start of your application by calling CoInitialize and specifying NULL.

   if (FAILED(CoInitialize(NULL)))

   return FALSE;

2. Create the DirectDraw object by using CoCreateInstance and the IDirectDraw2::Initialize method.

   ddrval = CoCreateInstance(&CLSID_DirectDraw,

   NULL, CLSCTX_ALL, &IID_IDirectDraw2, &lpdd);

   if(!FAILED(ddrval))

   ddrval = IDirectDraw2_Initialize(lpdd, NULL);

   In this call to CoCreateInstance, the first parameter, CLSID_DirectDraw, is the class identifier of the DirectDraw driver object class, the IID_IDirectDraw2 parameter identifies the particular DirectDraw interface to be created, and the lpdd parameter points to the DirectDraw object that is retrieved. If the call is successful, this function returns an uninitialized object.

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

Before you close the application, shut down COM by using the CoUninitialize function.

CoUnitialize();