Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / C++
Article

LicenceProvider

Rate me:
Please Sign up or sign in to vote.
4.28/5 (14 votes)
26 Oct 2006CPOL2 min read 40.5K   1K   27   3
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:

JavaScript
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Biotecnica Instruments S.p.A.
Italy Italy




Debugging? Klingons do not debug. Our software does not coddle the weak. Bugs are good for building character in the user.
-- The Klingon programmer



Beelzebub for his friends [^].





Comments and Discussions

 
QuestionWhere is the component? Pin
nonlinearly27-Nov-20 23:42
nonlinearly27-Nov-20 23:42 
Questionplease help me soon Pin
kian_beh13-Jun-07 1:31
kian_beh13-Jun-07 1:31 
AnswerRe: please help me soon Pin
CPallini13-Jun-07 1:39
mveCPallini13-Jun-07 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.