template< class license >
class CComClassFactory2 : public CComClassFactory2Base, license
Parameters
license A class that implements the following static functions:
CComClassFactory2 implements IClassFactory2 interface, which is an extension of IClassFactory. IClassFactory2 controls object creation through a license. A class factory executing on a licensed machine can provide a run-time license key. This license key allows an application to instantiate objects when a full machine license does not exist.
ATL objects normally acquire a class factory by deriving from CComCoClass. This class includes the macro DECLARE_CLASSFACTORY, which declares CComClassFactory as the default class factory. To use CComClassFactory2, specify the DECLARE_CLASSFACTORY2 macro in your objects class definition. For example:
class CMyClass : ..., public CComCoClass< ... > { public: DECLARE_CLASSFACTORY2(CMyLicense) ... };
CMyLicense, the template parameter to CComClassFactory2, must implement the static functions VerifyLicenseKey, GetLicenseKey, and IsLicenseValid. The following is an example of a simple license class:
class CMyLicense { protected: static BOOL VerifyLicenseKey(BSTR bstr) { USES_CONVERSION; return !lstrcmp(OLE2T(bstr), _T("My run-time license key")); }
static BOOL GetLicenseKey(DWORD dwReserved, BSTR* pBstr) { USES_CONVERSION; *pBstr = SysAllocString( T2OLE(_T("My run-time license key"))); return TRUE; }
static BOOL IsLicenseValid() { return TRUE; } };
CComClassFactory2 derives from both CComClassFactory2Base and license. CComClassFactory2Base, in turn, derives from IClassFactory2 and CComObjectRootEx< CComGlobalsThreadModel >.
#include <atlcom.h>
See Also CComClassFactoryAutoThread, CComClassFactorySingleton, CComObjectRootEx, CComGlobalsThreadModel, IClassFactory2 in the Win32 SDK
IClassFactory Methods | |
CreateInstance | Creates an object of the specified CLSID. |
LockServer | Locks the class factory in memory. |
IClassFactory2 Methods | |
CreateInstanceLic | Given a license key, creates an object of the specified CLSID. |
GetLicInfo | Retrieves information describing the licensing capabilities of the class factory. |
RequestLicKey | Creates and returns a license key. |
HRESULT CreateInstance( LPUNKNOWN pUnkOuter, REFIID riid, void** ppvObj );
Return Value
A standard HRESULT value.
Parameters
pUnkOuter [in] If the object is being created as part of an aggregate, then pUnkOuter must be the outer unknown. Otherwise, pUnkOuter must be NULL.
riid [in] The IID of the requested interface. If pUnkOuter is non-NULL, riid must be IID_IUnknown.
ppvObj [out] A pointer to the interface pointer identified by riid. If the object does not support this interface, ppvObj is set to NULL.
Remarks
Creates an object of the specified CLSID and retrieves an interface pointer to this object. Requires the machine to be fully licensed. If a full machine license does not exist, call CreateInstanceLic.
CComClassFactory2 Overview | Class Members
See Also CoCreateInstance and CoGetClassObject in the Win32 SDK
HRESULT CreateInstanceLic( IUnknown* pUnkOuter, IUnknown* pUnkReserved, REFIID riid, BSTR bstrKey, void** ppvObject );
Return Value
A standard HRESULT value.
Parameters
pUnkOuter [in] If the object is being created as part of an aggregate, then pUnkOuter must be the outer unknown. Otherwise, pUnkOuter must be NULL.
pUnkReserved [in] Not used. Must be NULL.
riid [in] The IID of the requested interface. If pUnkOuter is non-NULL, riid must be IID_IUnknown.
bstrKey [in] The run-time license key previously obtained from a call to RequestLicKey. This key is required to create the object.
ppvObject [out] A pointer to the interface pointer specified by riid. If the object does not support this interface, ppvObject is set to NULL.
Remarks
Similar to CreateInstance, except that CreateInstanceLic requires a license key. You can obtain a license key using RequestLicKey. In order to create an object on an unlicensed machine, you must call CreateInstanceLic.
CComClassFactory2 Overview | Class Members
See Also CoCreateInstance and CoGetClassObject in the Win32 SDK
HRESULT GetLicInfo( LICINFO* pLicInfo )
Return Value
A standard HRESULT value.
Parameters
pLicInfo [out] Pointer to a LICINFO structure.
Remarks
Fills a LICINFO structure with information that describes the class factory's licensing capabilities. The fRuntimeKeyAvail member of this structure indicates whether, given a license key, the class factory allows objects to be created on an unlicensed machine. The fLicVerified member indicates whether a full machine license exists.
CComClassFactory2 Overview | Class Members
See Also LICINFO in the Win32 SDK, CComClassFactory2::RequestLicKey, CComClassFactory2::CreateInstanceLic
HRESULT LockServer( BOOL fLock );
Return Value
A standard HRESULT value.
Parameters
fLock [in] If TRUE, the lock count is incremented; otherwise, the lock count is decremented.
Remarks
Increments and decrements the module lock count by calling _Module::Lock and _Module::Unlock, respectively. Here, _Module refers to the global instance of CComModule or a class derived from it.
Calling LockServer allows a client to hold onto a class factory object so that multiple objects can be created quickly.
CComClassFactory2 Overview | Class Members
See Also CComModule::Lock, CComModule::Unlock
HRESULT RequestLicKey( DWORD dwReserved, BSTR* pbstrKey );
Return Value
A standard HRESULT value.
Parameters
dwReserved [in] Not used. Must be zero.
pbstrKey [out] Pointer to the license key.
Remarks
Creates and returns a license key, provided that the fRuntimeKeyAvail member of the LICINFO structure is TRUE. A license key is required for calling CreateInstanceLic to create an object on an unlicensed machine. If fRuntimeKeyAvail is FALSE, then objects can only be created on a fully licensed machine.
Call GetLicInfo to retrieve the value of fRuntimeKeyAvail.
CComClassFactory2 Overview | Class Members
See Also LICINFO in the Win32 SDK