Article ID: 151771
Article Last Modified on 4/10/2006
Download the Licreqst.exe package now.119591 How to obtain Microsoft support files from online services
/*
Copyright (c) 1994
*/
WCHAR pwchLicenseKey[] =
{
0x0043, 0x006F, 0x0070, 0x0079, 0x0072, 0x0069,
0x0067, 0x0068, 0x0074, 0x0020, 0x0028, 0x0063,
0x0029, 0x0020, 0x0031, 0x0039, 0x0039, 0x0034,
0x0020
};
You can use the previous block of text in code that creates an instance
of the Licensed object, and you can use the pwchLicenseKey variable to specify
the object's License key.
BSTR bstrLicense = ::SysAllocStringLen(pwchLicenseKey,
sizeof(pwchLicenseKey)/sizeof(WCHAR));
m_MyControl.Create(NULL, WS_VISIBLE, CRect(10,10,10,10), this,
2, NULL, FALSE, bstrLicense);
::SysFreeString(bstrLicense);
Note how a bstrLicense is created from the pwchLicenseKey variable and how the bstrLicense is then used for the License key parameter in the Create method call.
// Compile options needed: none
///////////////////////////////////////////////////////////////////////
// SAMPLE CODE - Implementation of the RequestLicenseKey function
//
// The RequestLicenseKey function uses the IClassFactory2 interface
// to request a License key from an object specified by its ProgID.
//
// Parameters:
//
// [out] BSTR& bstrLicenseKey
// Upon return, this BSTR will contain either a valid
// License key or an error message. It is the caller's
// responsibility to call ::SysFreeString on this BSTR.
//
// [in] CString strProgID
// Specifies the ProgID of the object from which to request the
// License key.
//
//
// Return Value:
//
// A BOOL specifying success or failure. If TRUE is returned,
// the License key was retrieved successfully and the
// bstrLicenseKey parameter contains a valid License key. If
// FALSE is returned, the License key was not retrieved
// successfully and the bstrLicenseKey parameter contains a
// descriptive error string.
//
// Regardless of the return value, it is the responsibility of
// the caller to call ::SysFreeString on the returned bstrLicenseKey
// parameter.
//
BOOL RequestLicenseKey(BSTR& bstrLicenseKey, CString strProgID)
{
USES_CONVERSION;
LPCLASSFACTORY2 pClassFactory;
CLSID clsid;
BOOL bValidKeyReturned = FALSE;
// Get the CLSID of the specified ProgID.
if (SUCCEEDED(CLSIDFromProgID(T2OLE(strProgID), &clsid)))
{
// Create an instance of the object and query it for
// the IClassFactory2 interface.
if (SUCCEEDED(CoGetClassObject(clsid, CLSCTX_INPROC_SERVER, NULL,
IID_IClassFactory2, (LPVOID *)(&pClassFactory))))
{
LICINFO licinfo;
// Check to see if this object has a runtime License key.
if (SUCCEEDED(pClassFactory->GetLicInfo(&licinfo)))
{
if (licinfo.fRuntimeKeyAvail)
{
HRESULT hr;
// The object has a runtime License key, so request it.
hr = pClassFactory->RequestLicKey(0, &bstrLicenseKey);
if (SUCCEEDED(hr))
{
if(bstrLicenseKey == NULL)
bstrLicenseKey = ::SysAllocString(
L"<Object returned a NULL license key>");
else
// You have the license key.
bValidKeyReturned = TRUE;
}
else
// Requesting the License key failed.
switch(hr)
{
case E_NOTIMPL:
bstrLicenseKey = ::SysAllocString(
L"<The object's class factory does not support"
L" run-time license keys>");
break;
case E_UNEXPECTED:
bstrLicenseKey = ::SysAllocString(
L"<An unexpected error occurred when requesting the"
L" run-time license key>");
break;
case E_OUTOFMEMORY:
bstrLicenseKey = ::SysAllocString(
L"<The object's class factory was unable to allocate"
L" the license key>");
break;
case CLASS_E_NOTLICENSED:
bstrLicenseKey = ::SysAllocString(
L"<The object's class factory supports run-time"
L" licensing, but the current machine\r\nitself is"
L" not licensed. Thus, a run-time key is not"
L" available on this machine>");
break;
default:
bstrLicenseKey = ::SysAllocString(
L"<An unknown error occurred when requesting the"
L" license key>");
}
}
else
bstrLicenseKey = ::SysAllocString(
L"<The object has no runtime license key>");
}
else
bstrLicenseKey = ::SysAllocString(
L"<Unable to get the licensing capabilities of the object's"
L" class factory>");
// Make sure you release the reference to the class factory.
pClassFactory->Release();
}
else
bstrLicenseKey = ::SysAllocString(
L"<Unable to get the IClassFactory2 interface from the"
L" specified object>");
}
else
bstrLicenseKey = ::SysAllocString(
L"<Unable to get the CLSID of the specified object>");
// Return a BOOL specifying whether or not you were able to get a
// valid license key.
return bValidKeyReturned;
}
OLE Programmer's Reference; search on IClassFactory2 interface; CWnd::CreateControl method.
Keywords: kbhowto kbinfo kblicensing kbsample KB151771