Platform SDK: DirectX

Creating DirectDraw Objects by Using CoCreateInstance

[Visual Basic]

The information in this topic pertains only to applications written in C++.

[C++]

You can create a DirectDraw object by using the CoCreateInstance function and the IDirectDraw7::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 IDirectDraw7::Initialize method.
    ddrval = CoCreateInstance(&CLSID_DirectDraw,
        NULL, CLSCTX_ALL, &IID_IDirectDraw7, &lpdd);
    if(!FAILED(ddrval))
        ddrval = IDirectDraw7_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_IDirectDraw7 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 IDirectDraw7::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 Initialize method before using one of the methods associated with the DirectDraw object, a DDERR_NOTINITIALIZED error will occur.

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

CoUninitialize();