Create Barcode ActiveX at the background using Visual C++

Morovia Barcode ActiveX is a licensed COM component. Here licensed means that you need to pass the license key when creating the object. There are two methods creating an object in the COM world: using IClassFactory or IClassFactory2 interfaces. On a machine with design-time license installed, you can use IClassFactory to create the object. On the other side, you must use IClassFactory2 interface to create a license object on a machine with no design time license available.

Installing design-time license on computers with no furnished license violates the license agreement.

If your code creates the object using IClassFactory interface, it works fine on the developer machine. However, when running on the customer machine, the creating function returns an error: You are not licensed to use the class.

Note that this article does not apply to GUI programs created in an IDE environment by placing the ActiveX object into a form or dialog. The IDE environments automatically retrieves the key for you and embed into your application.

Solution

The IClassFactory2 interface provides methods to retrieve the license key and creating an object with license.

IClassFactory2::RequestLicKey returns the license key.

IClassFactory2::CreateInstanceLic creates the object with license key passed as a parameter.

Morovia Barcode ActiveX uses your license name as the license key. There is no need to call the first function to retrieve the key. Suppose that your license name is XYZ corporation, simply pass that string as the license key.

The following code snippet demonstrates creating the Barcode ActiveX object using IClassFactory2.

// assume the DLL is under the root folder of drive C. 
#import "c:\\MrvBarCd.dll"
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{ 
  int nRetCode = 0; 
  ::CoInitialize(NULL);
  CComPtr<MrvBarCdLib::IBarcode> pBarCode; 
  IClassFactory2* pCF=NULL;  
  CComBSTR bstrLicenseKey = L"Your license key"; 
  HRESULT hr = CoGetClassObject(__uuidof(MrvBarCdLib::MrvBarcode), 
      CLSCTX_ALL, 
      NULL, 
      IID_IClassFactory2, 
      reinterpret_cast<void**>(&pCF));  
  ASSERT(SUCCEEDED(hr)); 
  hr = pCF->CreateInstanceLic(NULL, NULL, 
       __uuidof(MrvBarCdLib::IBarcode), 
       bstrLicenseKey,      
       reinterpret_cast<void**>(&pBarCode)); 
       ASSERT(SUCCEEDED(hr)); 
  return nRetCode;
}