Example #1
0
HRESULT
STDMETHODCALLTYPE
CClassFactory::CreateInstance(
    _In_opt_ IUnknown * /* OuterObject */,
    _In_ REFIID InterfaceId,
    _Out_ PVOID *Object
    )
/*++
 
  Routine Description:

    This COM method is the factory routine - it creates instances of the driver
    callback class and returns the specified interface on them.

  Arguments:

    OuterObject - only used for aggregation, which our driver callback class
                  does not support.

    InterfaceId - the interface ID the caller would like to get from our 
                  new object.

    Object - a location to store the referenced interface pointer to the new
             object.

  Return Value:

    Status.

--*/
{
    HRESULT hr;

    PCMyDriver driver;

    *Object = NULL;

    hr = CMyDriver::CreateInstance(&driver);

    if (SUCCEEDED(hr)) 
    {
        hr = driver->QueryInterface(InterfaceId, Object);
        driver->Release();
    }

    return hr;
}
HRESULT
CMyDriver::CreateInstance(
    _Out_ PCMyDriver *Driver
    )
/*++
 
  Routine Description:

    This static method is invoked in order to create and initialize a new 
    instance of the driver class.  The caller should arrange for the object
    to be released when it is no longer in use.

  Arguments:

    Driver - a location to store a referenced pointer to the new instance

  Return Value:

    S_OK if successful, or error otherwise.

--*/
{
    PCMyDriver driver;
    HRESULT hr;

    //
    // Allocate the callback object.
    //

    driver = new CMyDriver();

    if (NULL == driver)
    {
        return E_OUTOFMEMORY;
    }
        
    //
    // Initialize the callback object.
    //

    hr = driver->Initialize();

    if (SUCCEEDED(hr)) 
    {
        //
        // Store a pointer to the new, initialized object in the output 
        // parameter.
        //

        *Driver = driver;
    }
    else 
    {

        //
        // Release the reference on the driver object to get it to delete 
        // itself.
        //

        driver->Release();
    }

    return hr;
}