Implement multi-interface 1 to manually add interfaces. 2. Interface upgrade: The imathe interface adds new functions and upgrades them to imathe2.

Source: Internet
Author: User

 

COM Component Design and Application (8)
Implement multiple interfaces
Http://www.vckbase.com/document/viewdoc? Id = 1501
Author: Instructor Yang

Download source code

I. Preface
From the fifth round to the seventh round, we use
ATL writes a simple COM component, which is simple because only one custom (IFM) interface ifun is implemented in the component. Of course, if you want to be lazy, we can add all 200 functions to this interface. If so, no one will like to use this component. Since a component can provide multiple interfaces, we should classify functions according to their functions during design, and present functions of different function classifications using multiple interfaces. This provides the following benefits:
1. The number of functions in an interface is limited and functions are concentrated. Users can easily learn, remember, and call functions. How many functions does an interface provide? The answer is: if you are a gorilla, a single interface can have up to three functions. If you are a human, it is best to have no more than seven functions for one interface. (Note 1)
2. easy to maintain. At least it is easier for you to search by the naked eye.
3. Easy to upgrade. When adding a function to a component, do not modify the published interface, but provide a new interface to complete function expansion. (Note 2)
This book is written in ------ how to implement one component and multiple interfaces.

Ii. Interface Structure

Figure 1. component A has two custom interfaces. Component B is an upgrade of component.

One day, we designed component A, which has two custom interfaces. Imathe has the add () function to complete the integer addition, and istr has the function CAT () to complete the string connection. One day later, we upgraded component A to component B to add a function MUL () to complete integer multiplication. Note that since we have published component A, we cannot arrange this function in the imathe interface of the old interface. The solution is to define another interface imathe2, add the MUL () function to the new interface, and retain the add () function. In this way, the old user does not know the existence of the new interface imathe2, and he still uses the old interface imathe, while the new user can discard it.
Imathe, which directly uses the new imathe2 interface function. Look, how smooth the upgrade is!

Iii. Implementation
3-1,
First, Use ATL to implement a COM component of the custom (M m) interface imathe, and complete the add () integer addition function in the interface.Note !!!It must be a custom interface (dual interface will be introduced later ). If you do not know about this operation, please read the "fifth return" or "Sixth return" again ".
3-2,
View the IDL file. After completing the previous step, open the IDL file with the following content: (the name and UUID will be different from the IDL in your program)

1 import "oaidl.idl";2 import "ocidl.idl";3[4object,5uuid(072EA6CA-7D08-4E7E-B2B7-B2FB0B875595),6helpstring("IMathe Interface"),7pointer_default(unique)8]9interface IMathe : IUnknown10{11[helpstring("method Add")] HRESULT Add([in] long n1, [in] long n2, [out,retval] long *pnVal);12};13 [14uuid(CD7672F7-C0B4-4090-A2F8-234C0062F42C),15version(1.0),16helpstring("Simple3 1.0 Type Library")17 ]18 library SIMPLE3Lib19 {20importlib("stdole32.tlb");21importlib("stdole2.tlb");22[23uuid(C6F241E2-43F6-4449-A024-B7340553221E),24helpstring("Mathe Class")25]26coclass Mathe27{28[default] interface IMathe;29};30 };
1-2 Introduce other interface description files defined by iunknown and ATL. Import is similar to # include in C.
3-12 Complete description of an interface
4 Object indicates that this block describes an interface. The IDL file adopts the PRC remote data exchange format description.
5 The IID of the uuid (...) interface, which is automatically generated by ATL and can be manually modified or generated by guidgen.exe (note 3)
6 You can see this prompt in some software or tools.
7 Define the default properties of the pointer used by parameters in the interface function (note 4)
9 The interface is called imathe and is derived from iunknown. Therefore, the first three functions of the imathe interface must be QueryInterface, addref, and release.
10-12 Interface function list
13-30 The complete description of the Type Library (the concept of the Type Library will be discussed later). The lines described below need to be understood first.
18 # Default namespace during import
23 CLSID of the component. The first parameter of cocreateinstance () is
27-29 Interface list
28 [Default] indicates who provides the iunknown Interface

3-3Manually modify the IDL file. The black text is manually entered. Save the settings.

Import "oaidl. IDL "; import" ocidl. IDL "; [object, UUID (UID), 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 is actually skillful: copy and paste the above interface description (imathe), and then modify it more conveniently.Object, UUID (072ea6cb-7d08-4e7e-b2b7-b2fb0b875595 ),// The IID generated manually or using a toolHelpstring ("istr interface"), pointer_default (unique)] interface istr: iunknown {// No 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 is another one here };};

3-4, Open the header file (mathe. h), manually add the class derivation relationship and interface entry table, and then save.

Class atl_no_vtable cmathe: Public ccomobjectrootex <ccomsinglethreadmodel>, public ccomcoclass <cmathe, & clsid_mathe>, public imathe, // do not forget to add a comma herePublic istr// Add a base class {public: cmathe () {} declare_registry_resourceid (idr_mathe) declare_protect_final_construct () begin_com_map (cmathe) // interface entry table. The interface entered here can be found by QueryInterface () com_interface_entry (imathe)Com_interface_entry (istr)End_com_map ()

3-5All right, everything is ready. Next, you can add a function to the istr interface. The following example adds a function for string connection:
Hresult CAT ([in] BSTR S1, [in] BSTR S2, [out, retval] BSTR * psval); if you do not know how to do this, read the content of the first three times.

Iv. Interface upgrade
We have released this component, but suddenly we need to add another function on the imathe interface one day... no! It cannot be directly modified on imathe! What should I do? The solution is ------ add another interface. Let's call it imathe2. If we want to add a function later, we will add another interface called imathe3.
4-1Modify the IDL file. If you understand the content in the above section, it is very easy to add an interface.

Import "oaidl. IDL "; import" ocidl. IDL "; [object, UUID (UID), 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 {// The following add () function only has the Declaration in IDL, instead of adding any program code, because this function has been implemented as early as 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 is another line here !};};

4-2, Open the header file, add a derived relationship and interface entry table

Class atl_no_vtable cmathe: Public ccomobjectrootex <ccomsinglethreadmodel>, public ccomcoclass <cmathe, & clsid_mathe>, public imathe, public istr, // Add a comma herePublic imathe2{Public: cmathe () {} declare_registry_resourceid (idr_mathe) Evaluate () begin_com_map (cmathe) com_interface_entry (imathe) com_interface_entry (istr)Com_interface_entry (imathe2)End_com_map ()

4-3In the example program, an integer multiplication function is added:
Hresult MUL ([in] Long N1, [in] Long N2, [out, retval] Long * pnval); if you don't know how to do this, stop learning: -(I 've talked about it several times. Why don't I know it? Do you know how a bear died? (Note 5)
4-4Because our components have been upgraded, we should also modify the version number (this is not necessary, but it is best to modify it ). Open the Registry file (. RGS) and change the version of progid "mathe.1" to "mathe.2 ". Modify the version and prompt text in the IDL file if you want. Here we will not paste the file content, because it is very simple. After you download the sample program (note 6), let's see it for yourself.

V. Summary
Strive for the software industry in the motherland!
Next, I will introduce the "Automation" --- idispatch interface, which is very interesting! Thank you for your attention :-)

NOTE 1: The Instantaneous Memory volume of a chimboo is 3, and that of a human being is 7. Scientists have done experiments and put a piece of sugar in one of the three bowls in front of them. The boogs can be found right away, but if there are more than three bowls, the orangutan is dizzy ...... if you look at a string of numbers and immediately ask you to tell them, the average person will only remember seven of them.
NOTE 2: Once a component is published, do not modify the existing interface. In this way, the software upgrade can be "robust.
Note 3: The guidgen.exe tool has been introduced in COM Component Design and Application (II.
Note 4: the processing of memory pointers by component functions will be discussed in detail later.
Note 5: Stupid!
Note 6: The sample program consists of vc6.0 and vc.net 2003.

 
Copyright 1999-2011 VC Knowledge Base

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.