"Reprint" COM Component design and application (eight)--implement multi-interface

Source: Internet
Author: User
Tags mul

Original: http://vckbase.com/index.php/wv/1219.html

First, preface

From the fifth back to the seventh, we wrote a simple COM component with ATL, simply because in the component, only one custom interface Ifun was implemented. Of course, if you want to be lazy, we can add 200 functions to this interface, if so, I am afraid that no one likes to use our component. Since a component can provide multiple interfaces, then we should design, we should according to the function of the functions of classification, the function of different functions classified by multiple interfaces. This can have some of the following benefits:

1, the number of functions in an interface is limited, the function is concentrated, the user is easy to learn, remember and call. How many functions is appropriate for an interface? The answer is: If you are a chimpanzee, then an interface up to 3 functions, if you are human, then an interface is best not to more than 7 functions. (Note 1)

2, easy to maintain. At least it's handy when you're looking at the naked eye.

3, easy to upgrade. When we add a function to a component, do not modify the already published interface, but instead provide a new interface to complete the function extension. (Note 2)

This get books back landed in------how to implement a component, multiple interfaces.

Second, the interface structure

Figure one, component A has 2 custom interfaces, component B is an upgrade

One day, we designed component A, which has 2 custom interfaces. Imathe has the function add () to complete the integer addition, and ISTR has the function cat () to complete the string connection. Suddenly, we upgrade components A to B, adding a function Mul () to complete the multiplication of integers. Note that since we have already published component A, we cannot arrange this function into the old interface imathe. The workaround is to define a new interface IMathe2, add the Mul () function to the new interface, and still retain the Add () function. In this way, the old user does not know the new interface IMathe2 exists, he still uses the old interface imathe, while the new user can abandon imathe, directly use the new interface function IMathe2. Look, how smooth the upgrade way Ah!

Third, the realization

3-1, First use ATL to implement a custom interface imathe COM component, the interface to complete the Add () integer addition function. Attention!!! must be a custom interface (the dual dual interface is introduced later). If you don't understand this, read back to "fifth" or "sixth back" again.

3-2 . View IDL file. After completing the previous step, open the IDL file with the following: (the name and UUID will be different from the IDL in your program)

Import "Oaidl.idl"; Import "Ocidl.idl"; [Object,uuid (072ea6ca-7d08-4e7e-b2b7-b2fb0b875595), helpstring ("Imathe Interface"), pointer_default (unique)] Interface Imathe:iunknown{[helpstring ("Method Add")] HRESULT Add ([in] long N1, [in] long N2, [out,retval] long *pnval);} ; [UUID (CD7672F7-C0B4-4090-A2F8-234C0062F42C), version (1.0), helpstring ("Simple3 1.0 Type Library")] Library Simple3lib {importlib ("stdole32.tlb"); Importlib ("Stdole2.tlb"); [UUID (C6F241E2-43F6-4449-A024-B7340553221E), helpstring ("Mathe Class")]coclass Mathe{[default] interface imathe;}; 3};

  

1-2 Introduce additional interface profiles that are already defined by IUnknown and ATL. Import is similar to the #include in the C language
3-12 A complete description of an interface
4 Object indicates that this block describes an interface. IDL file is a description method borrowed from the PRC Remote Data Interchange Format
5 The IID of the UUID (...) interface, this value is automatically generated by ATL and can be manually modified or generated with Guidgen.exe (Note 3)
6 In some software or tools, you can see this hint
7 Define default properties for pointers used by parameters in interface functions (note 4)
9 The interface is called imathe derived from IUnknown, so the first three functions of the Imathe interface must be queryinterface,addref and release
10-12 List of interface functions
13-30 A complete description of the type library (the concept of the type library is later), the following lines are required to be understood first
18 The default namespace #import time
23 The first parameter of the component's Clsid,cocreateinstance () is its
27-29 Interface List
28 [Default] indicates who provided the IUnknown interface

3-3, manually modify the IDL file, the bold part of the word is entered manually. Save when finished.

Import "Oaidl.idl"; import "Ocidl.idl"; [Object,uuid (072ea6ca-7d08-4e7e-b2b7-b2fb0b875595), helpstring ("Imathe Interface"), pointer_default (unique)] Interface Imathe:iunknown{[helpstring ("Method Add")] HRESULT Add ([in] long N1, [in] long N2, [out,retval] long *pnval);} ;  [//The so-called manual input, actually also has the skill: the above Interface description (imathe) Copy, paste down, and then change more convenient ha object,uuid (072ea6cb-7d08-4e7e-b2b7-b2fb0b875595),// Manual or tool-generated IID helpstring ("IStr Interface"), pointer_default (unique)]interface istr:iunknown{//currently does not have any interface functions};  [UUID (CD7672F7-C0B4-4090-A2F8-234C0062F42C), version (1.0), helpstring ("Simple3 1.0 Type Library")]library Simple3lib {importlib ("stdole32.tlb"); Importlib ("Stdole2.tlb"); [UUID (C6F241E2-43F6-4449-A024-B7340553221E), helpstring ("Mathe Class")]coclass Mathe{[default] interface imathe;  Interface IStr; Don't forget, there's another one there;};

  

3-4, open the header file (Mathe.h), manually add the derivation of the class and the Interface Entry table, and then save.

Class Atl_no_vtable Cmathe:public CComObjectRootEx, public ccomcoclass, public imathe,//don't forget, add a comma public IStr//Add a Base class {public:cmathe () {}declare_registry_resourceid (idr_mathe) declare_protect_final_construct () BEGIN_COM_MAP ( cmathe)//Interface Entry table. Here to fill in the interface to be QueryInterface () to find Com_interface_entry (imathe) com_interface_entry (ISTR) End_com_map ()

  

3-5, okay, everything's ready. Next, you can add the function to the ISTR interface. Add a function to the string connection function in the sample program:
HRESULT Cat ([in] BSTR S1, [in] BSTR S2, [Out,retval] BSTR *psval); If you don't know how to do this, please re-read the first three back.

Four, interface upgrade

Our component has been released, but suddenly one day we need to add another function on the Imathe interface ... No way! Absolutely cannot be modified directly on the imathe! What to do? The solution is to------add another interface, we call IMathe2 bar, if you want to add a function later, then we add an interface called IMathe3 ... Posterity, Endless also.

4-1, modify the IDL file, in fact, if you understand the above section of the content, and then add an interface is very simple thing.

Import "Oaidl.idl"; import "Ocidl.idl"; [Object,uuid (072ea6ca-7d08-4e7e-b2b7-b2fb0b875595), helpstring ("Imathe Interface"), pointer_default (unique)] Interface Imathe:iunknown{[helpstring ("Method Add")] HRESULT Add ([in] long N1, [in] long N2, [out,retval] long *pnval);} ; [Object,uuid (072ea6cb-7d08-4e7e-b2b7-b2fb0b875595), helpstring ("IStr Interface"), pointer_default (unique)]   Interface Istr:iunknown{[helpstring ("Method Cat")] HRESULT Cat ([in] BSTR S1, [in] BSTR S2, [Out,retval] BSTR *psval);}; [Object,uuid (072ea6cc-7d08-4e7e-b2b7-b2fb0b875595), helpstring ("IMathe2 Interface"), pointer_default (unique)] Interface imathe2:iunknown{//below this Add () function, only the declaration in IDL, without adding any program code, because this function has already implemented in Imathe [HelpString ("Method Add")] HRESULT ADD ([in] long N1, [in] long N2, [out,retval] long *pnval);}; [UUID (CD7672F7-C0B4-4090-A2F8-234C0062F42C), version (1.0), helpstring ("Simple3 1.0 Type Library")]library Simple3lib {importlib ("stdole32.tlb"); Importlib ("Stdole2.tlb"); [UUID (c6f241e2-43f6-4449-a024-b7340553221e), heLpstring ("Mathe Class")]coclass Mathe{[default] interface imathe;interface IStr; interface IMathe2;//Don't forget, there's another line! };};

  

4-2, open header file, add derivation and Interface Entry table

Class Atl_no_vtable Cmathe:public CComObjectRootEx, public ccomcoclass, public imathe,public istr,//add a comma public IMa here the2 {public:cmathe () {}declare_registry_resourceid (idr_mathe) declare_protect_final_construct () BEGIN_COM_MAP ( cmathe) com_interface_entry (imathe) com_interface_entry (ISTR) com_interface_entry (IMathe2) End_com_map ()

  

4-3, an integer multiplication function is added in the sample program:

HRESULT Mul ([in] long N1, [in] long N2, [out,retval] long *pnval); If you don't know how to do it, don't learn:-( It's been told several times, how can you not master it? Do you know how the bear died? (Note 5)

4-4, because our components have been upgraded, we should also modify the version number (this is not required, but it is best to modify it). Open the registry file (. rgs) to modify the version "Mathe.1" of the ProgID to "Mathe.2". Also, if you prefer, change the version and hint text in the IDL file. This will not paste the contents of the file, because it is very simple, you download the sample program (note 6), see for yourself.

V. Summary

For the motherland's software business and struggle!

Next book Introduction "Automation"---IDispatch interface, fun! Thank you for your attention:-)

Note 1: The instantaneous memory capacity of chimpanzees is 3, and the instantaneous memory of human is 7. Scientists have done experiments, in front of the face, put a piece of sugar in one of the 3 bowls, chimpanzees can be found exactly right away, but if more than 3 bowls, the orangutan will faint ... If you look at a bunch of numbers and then let you say it right away, the average person will only remember 7 of them.

Note 2: Once a component is published, do not modify an existing interface. So that the software upgrade to achieve "robust" sex.

Note 3:guidgen.exe tool, described in "COM Component design and application (ii)".

Note 4: The component function handles the memory pointer, and there is a special chapter back to discuss it later.

Note 5: Dumb-dead!

Note 6: The sample program has two parts, the vc6.0 version and the Vc.net 2003 version respectively.

"Reprint" COM Component design and application (eight)--implement multi-interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.