COM technology insider Reading Notes-Chapter 1 Implementation of COM interface

Source: Internet
Author: User

The knowledge points in this chapter are listed below, which are not sorted out and extended.

 

1. com interfaces are implemented using purely abstract base classes in C ++. a com component can provide multiple interfaces, A c ++ class can use multiple inheritance to implement a component that can provide multiple interfaces.

2. functions marked with _ stdcall will use the standard call Convention, that is, these functions will delete the parameters from the stack between the returned and the caller. In the conventional C/C ++ call conventions, the stack cleaning is done by the caller. The name of the standard call Convention comes from all WIN32API functions. Except those functions with variable parameters, this call method is used. The C call Convention is still used for functions with variable parameters, that is, _ cdecl. Windows adopts the standard call Convention because it can reduce the code size. Another reason is that early windows was running on a 640kb system.

3. Remember that an interface is implemented by a virtual pure base class without implementation details.

4. A class is not a component. During component development, classes are not necessarily unavailable, but it is easier to use classes to implement components than other methods.

5. interfaces are not always inherited. Generally, a class is used to implement all interfaces. This is simple, easy to understand, and makes it more natural to use C ++ for COM programming.

6. An interface is a function set, a component is an interface set, and a system is a set of components. Interfaces can be viewed as behaviors.

7. com is immutable. Once an interface is published, it will remain unchanged.

8. Multiple Interfaces make polymorphism more important. Customers can process different components in the same way. The more interfaces a component supports, the smaller the components. The more actions an interface represents, the more specific it is, and the less likely it will be reused by other components. For interfaces that cannot be reused, the customer code that uses this interface cannot be reused.

9. Although C ++ can directly operate and use instance data, the COM component will never access any instance data. In COM, access to a component can only be done through functions, rather than directly through variables.

 

This chapter also takes a long time to introduce the knowledge of C ++ virtual function tables-basic knowledge required by C ++ programmers, this is a review for the profound understanding of COM. If the reader cannot fully understand the implementation principle and memory structure of the C ++ virtual function, it is difficult to deeply understand the principles and implementation mechanisms of COM.

The following is a simple and complete interface implementation example of interfaces IX and IX. Here ca implements a component that supports interfaces IX and Iy, and its customer is the function main.

//// Iface.cpp// To compile, use: cl Iface.cpp//#include <iostream.h>#include <objbase.h>   // Define interface.void trace(const char* pMsg) {cout << pMsg << endl ;}// Abstract interfacesinterface IX{virtual void __stdcall Fx1() = 0 ;virtual void __stdcall Fx2() = 0 ;} ;interface IY{virtual void __stdcall Fy1() = 0 ;virtual void __stdcall Fy2() = 0 ;} ;// Interface implementationclass CA : public IX,            public IY{public:// Implement interface IX.virtual void __stdcall Fx1() {cout << "CA::Fx1" << endl ;}virtual void __stdcall Fx2() {cout << "CA::Fx2" << endl ;}// Implement interface IY.virtual void __stdcall Fy1() {cout << "CA::Fy1" << endl ;}virtual void __stdcall Fy2() {cout << "CA::Fy2" << endl ;}} ;// Clientint main(){trace("Client: Create an instance of the component.") ;CA* pA = new CA ;// Get an IX pointer.IX* pIX = pA ;trace("Client: Use the IX interface.") ;pIX->Fx1() ;pIX->Fx2() ;// Get an IY pointer.IY* pIY = pA ;trace("Client: Use the IY interface.") ;pIY->Fy1() ;pIY->Fy2() ;trace("Client: Delete the component.") ;delete pA ;return 0 ;}

Output content:

Client: Create an instance of the component.

Client: Use the IX interface.

CA: fx1

CA: fx2

Client: Use the Iy interface.

CA: fy1

CA: FY2

Client: Delete the component

In the above example, we can see that the customer and the component communicate through two interfaces. Two purely abstract base classes IX and Iy are used in the interface implementation. The component in the example is implemented by class ca, which inherits IX and Iy and implements all the members of the two interfaces. The customer main function creates an instance of the component and obtains the pointer to the interface provided by the component. Then, it uses these two pointers as it uses the C ++ pointer, because the interface is implemented using a pure base class.

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.