Microsoft KB Archive/174802

-

{| The information in this article applies to:
 * width="100%"|
 * ActiveX Data Objects (ADO), version 1.0 used with: - Microsoft Visual C++, 32-bit Editions, versions 4.2, 4.2b, 5.0

SUMMARY
The Microsoft Foundation Classes (MFC) ClassWizard provides a powerful mechanism for manipulation OLE Automation servers by building MFC-based classes off a given typelib. The classes built wrap each object in the typelib by generating classes derived from COleDispatchDriver. However, COleDispatchDriver has no means for passing a license key. This article explains how to override the COleDispatchDriver::CreateDispatch methods to provide a license key.

MORE INFORMATION
Below is sample code for overriding the two versions of CreateDispatch. In this case, it is from sample code for passing a license key via MFC-OLE to the ActiveX Data Objects (ADO) Connection object; however, this is incidental to the code.

Unless noted, this code is identical to the Visual C++ 5.0 version of COleDIspatchDriver::CreateDispatch. Some macros available internally to MFC had to be replaced. Original code has been left below, but commented out to demonstrate the modifications:

BOOL _Connection::CreateDispatch(REFCLSID clsid,                                   COleException* pError, BSTR bstrLicKey) {     ASSERT(m_lpDispatch == NULL);

m_bAutoRelease = TRUE; // auto-release is a good default

// create an instance of the object LPUNKNOWN     lpUnknown  = NULL; IClassFactory2 *pIUnknown = NULL; IUnknown     *pOuter   = NULL;

//  SCODE sc = CoCreateInstance(clsid, NULL,   //                               CLSCTX_ALL | CLSCTX_REMOTE_SERVER,   //                               IID_IUnknown, (LPLP)&lpUnknown); SCODE sc = CoGetClassObject( clsid,                           CLSCTX_ALL | CLSCTX_REMOTE_SERVER,                            NULL,                            IID_IClassFactory2,                            (void**) &pIUnknown );

if (sc == E_INVALIDARG) {        // may not support CLSCTX_REMOTE_SERVER, so try without SCODE sc = CoGetClassObject( clsid,                              CLSCTX_ALL & ~CLSCTX_REMOTE_SERVER,                               NULL,                               IID_IClassFactory2,                               (void**) &pIUnknown );

//     sc = CoCreateInstance(clsid, NULL,   //                            CLSCTX_ALL & ~CLSCTX_REMOTE_SERVER,   //                            IID_IUnknown, (LPLP)&lpUnknown);

}

if( !FAILED( sc ) ) {        sc = pIUnknown->CreateInstanceLic( pOuter,                                    NULL,                                    IID_IUnknown,                                    bstrLicKey,                                    (void**)&lpUnknown ); }

if (FAILED(sc)) goto Failed;

// make sure it is running sc = OleRun(lpUnknown); if (FAILED(sc)) goto Failed;

// query for IDispatch interface // m_lpDispatch = QUERYINTERFACE(lpUnknown, IDispatch); if (lpUnknown->QueryInterface(IID_IDispatch, (void**)&m_lpDispatch) != S_OK)

if (m_lpDispatch == NULL) goto Failed;

lpUnknown->Release; ASSERT(m_lpDispatch != NULL); return TRUE;

Failed: // RELEASE(lpUnknown); if( lpUnknown != NULL ) {        lpUnknown->Release; lpUnknown = NULL; }     if (pError != NULL) pError->m_sc = sc; TRACE1("Warning: CreateDispatch returning scode = %s.\n",        AfxGetFullScodeString(sc)); return FALSE;

}

BOOL _Connection::CreateDispatch(LPCTSTR lpszProgID,                                   COleException* pError,                                    BSTR bstrLicKey) {     ASSERT(m_lpDispatch == NULL);

// map prog id to CLSID CLSID clsid; SCODE sc = AfxGetClassIDFromString(lpszProgID, &clsid); if (FAILED(sc)) {        if (pError != NULL) pError->m_sc = sc; return FALSE; }

// create with CLSID return CreateDispatch(clsid, pError, bstrLicKey); }