Licensing in the Visual Designer

At design-time, license verification proceeds as shown in the following figure:

When a programmer loads the ActiveX designer, the host calls GetLicInfo to verify that the appropriate license is present. If the license is not present, the method returns E_NOTLICENSED and the visual designer is not created. If a license is present or if no license is required, the method creates an instance and returns S_OK.

The following code sample shows how GetLicInfo might be implemented:

STDMETHODIMP CClassFactory::GetLicInfo
(
    LICINFO *pLicInfo
)
{
    CHECK_POINTER(pLicInfo);

    pLicInfo->cbLicInfo = sizeof(LICINFO);

    // If true, RequestLicKey will work.
    pLicInfo->fRuntimeKeyAvail = g_fMachineHasLicense;

    // If true, the standard CreateInstance will work.
    pLicInfo->fLicVerified = g_fMachineHasLicense;

    return S_OK;
}

After construction of the application is complete, the programmer selects Build or Run.

During this process, the host calls RequestLicKey to get a key to embed into the resulting application. The following sample shows one approach to implementing this method:

STDMETHODIMP CClassFactory::RequestLicKey
(
    DWORD  dwReserved,
    BSTR  *pbstr
)
{
    // If the machine isn't licensed, don't give this.
    if (!g_fMachineHasLicense)
        return CLASS_E_NOTLICENSED;

    *pbstr = SysAllocString(g_wszLicenseKey);
    return (*pbstr) ? S_OK : E_OUTOFMEMORY;
}

As with GetLicInfo, RequestLicKey returns CLASS_E_NOTLICENSED if the license key is not valid.