Implementation of COM component interface inheritance

xiaoxiao2021-03-20  233

Author:

Zhengkui

Download the source code explanation: This is the example of the example of "Dr. GUI and ATL" articles yourself, mainly inheriting new interfaces on the original interface, so that new features can be extended on the basis of retaining the original interface. Function introduction : 1. Create a Calculate component, add the IFUN interface (Note: This example we select APARTMENT, CUSTOM, YES in Attributes. Add a function add to the two numbers in the IFUN interface (parameter [in] long n1 , [in] long N2, [out, return] long * pval). The code is as follows:

STDMETHODIMP CCALCULATE :: Add (long N1, long N2, long * pval)

{

// Todo: Add Your Implementation Code Here

* pval = n1 n2;

Return S_OK;

} 2. Add IFUN2 interface to inherit the IFUN interface. Procedure is as follows:

(1) Modify .idl file:

in

Interface IFUN: IUNKNOWN

{

[Helpstring ("Method Add")] HRESULT ADD ([in] long N1, [in] long N2, [out, retval] long * pval

}; Add code:

[

Object,

UUID (B9D7572-1950-405B-8B6-9788A6DB1DB2), / / ​​is the number of flags of interface IFUN2, can be generated by the VS GUIDgen.exe

Helpstring ("ifun2 interface"),

Pointer_DEFAULT (UNIQUE)

]

Interface IFUN2: IFUN

{

}; At the same time, change the last CoClass Calculate function to

CoClass Calculate

{

Interface IFUN;

[default] interface ifun2;

}; (2) Modify Calculate.h file

will

Class ATL_NO_VTABLE CCALCULATE:

Public CComobjectrootex ,

Public ccomcoClass ,

Public IFUN

Change to

Class ATL_NO_VTABLE CCALCULATE:

Public CComobjectrootex ,

Public ccomcoClass ,

Public IFUN2

Begin_COM_MAP (ccalculate)

COM_Interface_entry (IFUN)

Add in END_COM_MAP ()

COM_ITERFACE_ENTRY (IFUN2) (3) Add a sub function in the IFUN2 interface, the method is no longer repetitive, only when implementing

* pval = n1 - n2; // Returns two numbers. (4) Write client code, call the Add and SUB functions of the IFUN2 interface.

Development environment: VC6 Windows 2000 / XP

Test environment: Windows 2000 WindowsXP

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

New Post(0)