Object Creation

For applications built with C/C++, use COM functions to create an object instance. Choose the method most suited to the application to create an instance or instances. Use CoCreateInstance when a single object instance is required. For example:

HRESULT             hr;

LPSQLDMOSERVER      pSQLServer;

hr = CoCreateInstance(CLSID_SQLDMOServer, NULL,

        CLSCTX_INPROC_SERVER, IID_ISQLDMOServer, (void**) &pSQLServer);

  

    // Do something with the object, then release the reference.

  

pSQLServer->Release();

For applications requiring multiple instances of the same object, consider using a class factory interface on the SQL-DMO object library to optimize object creation. For example:

HRESULT CDlgColumns::MakeColumns(UINT nCols, LPSQLDMOCOLUMN** ppColumns)

    {

    LPSQLDMOCOLUMN* apColumns;

    HRESULT         hr = NOERROR;

    LPCLASSFACTORY  pIClassFactory;

    UINT            nCol;

  

    *ppColumns = NULL;

  

    apColumns = new LPSQLDMOCOLUMN[nCols];

    if (apColumns == NULL)

        return (E_OUTOFMEMORY);

  

    memset(apColumns, 0, nCols * sizeof(LPSQLDMOCOLUMN));

  

    hr = CoGetClassObject(CLSID_SQLDMOColumn, CLSCTX_INPROC_SERVER,

        NULL, IID_IClassFactory, (void**) &pIClassFactory);

  

    if (FAILED(hr))

        {

        // Handle error....

        return (hr);

        }

  

    for (nCol = 0; nCol < nCols && !FAILED(hr); nCol++)

        {

        hr = pIClassFactory->CreateInstance(NULL, IID_IUnknown,

            (void**) &(apColumns[nCol]));

        }

  

    if (FAILED(hr))

        {

        // Handle error, and clean any bad items.

  

        for (nCol = 0; nCol < nCols && apColumns[nCol] != NULL; nCol++)

            (apColumns[nCol])->Release();

  

        delete [] apColumns;

        apColumns = NULL;

        }

  

    pIClassFactory->Release();

  

    *ppColumns = apColumns;

    return (hr);

    }

Remember, creating an instance of an object increases the reference count on the object. You must release this initial reference regardless of the use of the object. For example, adding an array of created Column objects to the Columns collection of a new Table object does nothing to the reference your application maintains on each Column object. For example:

LPSQLDMOTABLE   pTable;

  

const UINT      NCOLS = 5;

LPSQLDMOCOLUMN* apColumns;

UINT            nCol;

HRESULT         hr = NOERROR;

  

if (SUCCEEDED(MakeColumns(NCOLS, &apColumns)))

    {

    hr = CoCreateInstance(CLSID_SQLDMOTable, NULL,

        CLSCTX_INPROC_SERVER, IID_ISQLDMOTable, (void**) &pTable);

  

    // Defining columns using the array of Column objects not shown.

  

    // Use the array of Column objects to define the new table.

    for (nCol = 0; nCol < NCOLS && SUCCEEDED(hr); nCol++)

        hr = pTable->AddColumn(apColumns[nCol]);

  

    // Release references on each Column object.

    for (nCol = 0; nCol < NCOLS; nCol++)

        (apColumns[nCol])->Release();

  

    delete [] apColumns;

  

    // Release the reference on the Table object.

    pTable->Release();

    }

See Also

Object Class Identifiers and Type Definitions

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.