HOWTO: Passing a License Key to ADO in Visual C++ Using MFC-OLE

Last reviewed: October 8, 1997
Article ID: Q174802
The information in this article applies to:
  • ActiveX Data Objects (ADO), version 1.0 used with: - Microsoft Visual C++, 32-bit Editions, versions 4.2, 4.2b, 5.0

SUMMARY

The 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);
   }

REFERENCES

For additional information, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q168122
   TITLE     : HOWTO: Redistributing ADO 1.0 or ADO/R 1.0 with OLE/DB 1.1


Additional query words: mfckb
Keywords : kbcode adovc
Technology : kbmfc
Version : WINNT:4.2,4.2b,5.0
Platform : winnt
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 8, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.