Use ATL to establish COM components that support iClassFactory2

zhaozj2021-02-17  49

Use ATL to establish COM components that support iClassFactory2

Keywords: COM, ICLASSFACTORY2, Object Creation Permissions, License, License Key

This article describes how to create a COM object with a creation permission function using ATL itself. Finally, how to create a COM object with fixed and temporary privileges, and how to get registration code.

1 Overview

The role of the IClassFactory2 interface is to increase the functionality of the restriction control for COM objects. With this interface, you can only create an instance of a COM object on a computer with legal use.

There are two permissions for the COM object. One is fixed permissions. There is no additional information when there is a computer before creating a COM object. The other is temporary permissions. You must provide a registration code with permissions when you create an object. Temporary permissions cannot be kept in the computer, so each time you create an instance, you need to provide a registration code. Each COM object can choose to use a fixed permission method, a temporary permission method or both.

The COM object can provide a function of obtaining a registration code. With this feature, you can get registration code on your computer with fixed permissions.

When COM object is created, you should check the registration code or fixed permissions. If the COM object determines that the user does not use permissions, it can return class_e_notlicensed.

2 ATL support for IclassFactory2

ATL fully supports iClassFactory2. A macro Declare_ClassFactory2 declaration COM object supports the iClassFactory2 interface. Declare_classfactory2 Macro requires a license class to implement specific permission limit verification algorithms and registration code authentication algorithms. The License class can use any name, which must be defined as several member functions as follows. The form of the license class is as follows:

Class ClicenseCls

{

protected:

Static Bool VerifyLicenseKey (BSTR BSTR);

Static Bool GetLicenseKey (DWORD DWRESERVED, BSTR * PBSTR);

Static bool islicensevalid ();

}

The VerifyLicenseKey function is used to verify the legality of the registration code. GetLicenseKey is used to get the registration code. The Islicensevalid function is used to verify that there is a fixed usage permission.

The user can provide the implementation of these three functions as needed. As follows:

Desired function

VerifyLicenseKey

GetLicenseKey

Islicensevalid

Do not check permission False False True fixed permissions FALSE FALSE Check fixed permission Temporary Permissions Detection Registration Code FALSE FALSE Fixed Permissions and Temporary Permissions Registration Code False Check fixed permissions fixed permissions and temporary privileges and get registration code detection registration code Return registration code Check fixed permissions

3 implementation

3.1 License class

Realize the members of the license class:

Class Cmylicense

{

protected:

Static Bool VerifyLicenseKey (BSTR BSTRLICENSEKEY)

{

IF (WCSCMP (BSTRLICENSEKEY, OLESTR ("Template License Key")) == 0)

{

Return True;

}

Return False;

}

Static Bool getLicenseKey (DWORD DWRESERVED, BSTR * PBSTRLICENSEKEY)

{

IF (PBStrlicenseKey)

{

* pbstrlicensekey = sysallocstring (Olestr ("Template License Key");}

Return True;

}

Static Bool Islicensevalid () {Return True;}

}

In the above example, the IslicenseValid function will always return true. In actual use, you want to verify that there is fixed permissions in your computer. If there is a fixed permissions, it returns true, otherwise it returns false. This example is used to secure the registration code, or the pseudo-random registration code can be used as needed.

3.2 Implementing the ICLASSFActory2 interface

In the COM object class in the ATL, use the IclassFactory2 interface in the class object in the class object in the COM object class in the ATL. The definition of the DECLARE_CLASSFACTORY2 macro is as follows:

Declare_classfactory2 (LIC)

Where the LIC is the name of the License class, in this example, CMylicense.

4 Create a COM object that requires license

4.1 COM object with fixed permissions

Creating a COM object with fixed permissions and creating a normal COM object is the same. No additional information is required.

Use CoCreateInstance to easily create COM objects for fixed permissions. The definition of COCREATEINSTANCE is as follows:

Stdapi CocreateInstance (

Refclsid rclsid,

LPunknown Punkouter,

DWORD DWCLSCONTEXT,

Refiid RIID,

LPVOID * PPV

);

RCLSID is the CLSID to create an object.

Punkouter is used to polymerize, and NULL is used without using polymerization.

DwclsContext Specifies the runtime environment of the COM object, and the general situation uses CLSCTX_all representation to run in any environment.

RIID is the IID of the interface that needs to be returned.

PPV is the address of the returned interface pointer.

The specific code is as follows:

IYYY * POBJ;

HRESULT HR;

HR = CocreateInstance (CLSID_XXX, NULL, CLSCTX_ALL, IID_YYY, & POBJ);

IF (Failed (HR))

{

...

}

4.2 Using Temporary Permissions

Creating a COM object using temporary privileges, requiring a registration code. The registration code can be obtained from the COM object itself, or it can be obtained in other ways (such as purchase, etc.). Creating a COM object using Temporary Permissions requires two steps: Get the COM class object and create a COM object.

4.2.1 Get the COM class object

With the CogetherClassObject function, you can get the class object. The definition of COGETCLASSObject is as follows:

StDAPI COGETCLASSOBJECT

Refclsid rclsid,

DWORD DWCLSCONTEXT,

CoserverInfo * PServerInfo,

Refiid RIID,

LPVOID * PPV

);

Rclsid is the CLSID to create a COM object.

DWClsContext is a way of operating of COM objects, in general, using CLSCTX_ALL indicates that you can run any environment.

PServerInfo is used to create a COM object on another computer, and null is created locally.

RIID is the IID of the interface required, using IID_ICLASSFACTORY2.

PPV is the address that returns the interface pointer.

It can be seen that the first two parameters and the definitions in CocreateInstance are the same.

Specific code:

IclassFactory2 * PCF2;

HRESULT HR;

HR = COGETCLASSOBJECT (CLSID_XXX, CLSCTX_ALL, NULL, IID_ICLASSFACTORY2, (Void **) & PCF2);

IF (Failed (HR))

{

...

}

...

4.2.2 Creating an object

You can create objects using temporary permissions through the CreateInstanceLIC method of IclassFactory2. After obtaining the COM class object, call the CREATEINSTANCELIC method, and use the registration code as a parameter, you can get the interface pointer of the new COM object.

CreateInstanceLic is defined as follows:

HRESULT CREATEINSTANCELIC (

IUNKNOWN * PUNKOUTER,

IUNKNOWN * Punkreserved,

Refiid RIID,

BSTR BSTRKEY,

void ** ppvobject

);

Punkouter is a polymeric external object pointer that uses NULL when not using aggregation.

PunkReServed is the parameters reserved and NULL.

RIID is the interface IID that needs to return.

BSTRKEY is a registration code.

PPVObject is the address of saving the returned interface pointer.

Specific code:

IXXX * POBJ;

HR = PCF2-> CreateInstancelic (null, null, IID_IXXX,

BSTRLICENSEKEY, (VOID **) & POBJ);

IF (Failed (HR))

{

...

}

After creating, you can use a new COM object like using a normal COM object.

4.3 Get a registration code

First get the pointer to the class object, see Section 4.2.1.

After obtaining the pointer of the iClassFactory2, the registration code can be obtained through the RequestlicKey method.

The definition of RequestlicKey is as follows:

HRESULT REQUESTLICKEY

DWORD DWRESERVED,

BSTR * PBSTRKEY

);

DWRESERVED is the reserved parameter and use 0.

PBSTRKEY is the address of the registration code that holds the returned registration code. The caller is responsible for releasing the address of the return value.

Specific code:

BSTR BSTRLICKEY;

HR = PCF2-> Requestlickey (0, & BSTRLICKEY);

IF (Failed (HR))

{

...

}

...

Sysfreeestring (BSTRLICKEY);

转载请注明原文地址:https://www.9cbs.com/read-30635.html

New Post(0)