65.9K
CodeProject is changing. Read more.
Home

LicenceProvider

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.28/5 (11 votes)

Oct 26, 2006

CPOL

2 min read

viewsIcon

41274

downloadIcon

1037

A small COM component (DLL) enabling scrips to provide Runtime Licence text for ActiveX controls.

Introduction

If you try to embed a Microsoft ActiveX control, such as MSComm (for serial communications) inside your script (JScript, VBScript, script component), then you have to provide its Runtime License text (or key) in order to make it work on an end-user unlicensed machine (yes, you have this kind of problem only if you plan to deploy your script). I made a very small COM component (hosted by a DLL) able to provide such a license. Of course, you need to know the Runtime License text of the intended ActiveX (I have also developed an application to discover such texts on a licensed machine, i.e., a PC having Visual Studio installed on, see my article about COMLicenceFinder).

Background

You should know how to use ActiveX controls in a script (see MSDN). A general knowledge of COM is also desirable (there are a lot of books about COM: Inside COM, Essential COM, etc...).

Using the LicenceProvider COM component

First of all, you have to register on the target machine both the LicenceProvider component and the MS ActiveX control your script uses. Then you can obtain an instance of the control by calling the Provide method of the LicenceProvider object:

var objSerial;
// Create the LicenceProvider object
var lp = new ActiveXObject("LicenceProvider.LicProvider");
// Use the LicenceProvider object to obtain an instance
// of the MsComm ActiveX, by providing a Runtime Licence text.
// Passing MsComm CLSID & Runtime
// Licence Text as argumens. 
objSerial = lp.Provide("{648A5600-2C6E-101B-82B6-000000000014}", 
                       "Copyright (c) 1994 ");

The above sample code (JScript) just shows how to obtain an instance of the MSComm, providing the Runtime License via the Provide method of the LicenceProvider object.

Points of Interest

You need to use a COM component as LicenceProvider because the only way (I know) to obtain a runtime licensed instance of any of such ActiveX controls is requesting it by the CreateInstanceLic member of the control's IClassFactory2 interface, and the latter is not directly accessible by the script. Hence the LicenceProvider is finally an adapter of the VTABLE binding to the DISPATCH one used by the automation clients (like the scripts). The steps performed are the following:
  1. Obtain a pointer to the ActiveX control Class Object IClassFactory interface.
  2. Using the obtained pointer, request another pointer, this time to the IClassFactory2 interface.
  3. Call the IClassFactory2::CreateInstanceLic to obtain an instance of the ActiveX control (in fact, a pointer to its IUnknown interface).
  4. Request the IDispatch interface pointer via the QueryInterface method of IUnknown.

The steps are shown by the sample code below:

if ( UuidFromString((unsigned char *)
       (pCLSIDBegin+1), &uuid) != RPC_S_OK )
    return E_FAIL;
HRESULT hr = CoGetClassObject(uuid, CLSCTX_ALL, 
             NULL, IID_IClassFactory, (void**)&pCF);
if ( FAILED(hr) ) return E_FAIL;
hr = pCF->QueryInterface(IID_IClassFactory2, (void **)&pCF2);
pCF->Release();
if ( FAILED(hr)) return E_FAIL;
IUnknown * pObj;
hr = pCF2->CreateInstanceLic(NULL,NULL, IID_IUnknown , 
                             lictext, (void **)&pObj); 
pCF2->Release();
if ( FAILED(hr)) return E_FAIL;
pObj->QueryInterface(IID_IDispatch, (void **) pObject);

The magic of the DISPATCH to VTABLE mapping is done by the dual interface of the component (thanks to ATL).

History

  • 26 Oct 2006 - First release.