COM technology insider Reading Notes
Chapter 2 com history
Chapter 3 interface (using pure abstract base class of C ++ to implement COM interface)
Q1: What is an interface?The DLL interface is the functions it outputs, and the C ++ class interface is a member function set of the class. The COM interface also involves a set of functions implemented by components and provided to customers. It is a memory structure that contains an array of function pointers. In C ++, abstract base classes (not abstract functions) can be used to implement interfaces. A component can implement any number of interfaces, c ++ can be implemented using multiple inheritance of abstract base classes.
Virtual functions marked with 0 are called pure virtual functions, and classes containing pure virtual functions are called pure abstract base classes. The standard call convention and the C call Convention (_ stdcall and _ cdecl) Use _ stdcall for WIN32API calls with fixed parameters, variable Parameter calls using _ cdeclcom do not require a C ++ class to correspond to the same COM. In fact, a component can be implemented by multiple C ++ classes. COM does not require the class that implements an interface to inherit from that interface, because the customer does not need to understand the inheritance relationship of COM components. The inheritance of interfaces is not just an implementation detail.
Q2 virtual function table
Struct IX {virtual void _ stdcall fx1 () = 0; virtual void _ stdcall fx2 () = 0; virtual void _ stdcall FX3 () = 0; virtual void _ stdcall fx4 () = 0 ;}
Fortunately, all windows-Compatible C ++ compilers can generate the correct vtbl that com can use. The memory structure generated by the C ++ compiler for a pure abstract base class is the same as the memory structure required by the COM interface.
Com requires that all interfaces support three functions. These three functions must be the first three functions in the vtbl of the interface (used for interface query ).Chapter 2 QueryInterface Functions
Interface iunkown {virtual hresult _ stdcall QueryInterface (const IID & IDD, void ** GMM) = 0; virtual ulong _ stdcall addref () = 0; virtual ulong _ stdcall release () = 0 ;};
Iunkown * createinstance (); where IID is the interface identifier QueryInterface is the most important part of COM, mainly because a component is actually defined by QueryInterface,
Chapter 2 reference countThis chapter describes how to use the rules of addref and release. If Smart pointers are used, these are easy to solve.
Chapter 2 dynamic Connection Library
This chapter uses DLL and COM standards to establish an architecture that can link components with customers at runtime. DLL: exports a createinstance function, which returns an iunkown pointer, which is all of the DLL interfaces. Customer: loadlibary, call createinstance to obtain an iunknow pointer, and then query other interfaces based on this interface. In addition, iface. h defines the Interface Information implemented by the COM component (Implemented Using DLL. The content of iface. H is as follows:
Interface IX: iunknown{Virtual void _ stdcall FX () = 0;};Interface Iy: iunknown{Virtual void _ stdcall Fy () = 0;};Interface iz: iunknown{Virtual void _ stdcall FZ () = 0;};Extern "C"{Extern const IID iid_ix;Extern const IID iid_iy;Extern const IID iid_iz;}
Chapter 2 about hresult, guid, registry, and other detailsThe correct or wrong code is returned through the hresult value, which is not clear, and the guid_defin macro is not clear. Com library initialization, coinitialize, couninitialize. The void * cotaskmemalloc (ulong CB) and void cotaskmemfree (void * PV) in the com library can be allocated and released among multiple threads between different processes. It is powerful !! The com library defines some macros. C/C ++ defines the COM interface in the same format. Chapter 1 class factory customers can create components in different ways.Process, locally, on the remote server... The class factory is also part of the COM library. Now I feel that Microsoft COM includes two parts: COM standard and Microsoft COM Library (generated to facilitate the operation of COM). Chapter 4 Component Reuse inclusion and aggregation inheritance includes implementation inheritance and interface inheritance. Implementation Inheritance refers to the code or implementation that the class inherits from its base class. Interface inheritance is the type or interface between class inheritance and the base class. COM does not support inheritance. We can use component inclusion to fully simulate inheritance. Inclusiveness and aggregation are actually a technology that enables one component to use another component. The two components are called external and internal components respectively. In the case of inclusiveness, external components include internal components, while in the case of aggregation, external components are just called internal components. Aggregation is an inclusive feature. When an external component aggregates an interface of an internal component, it does not implement this interface like inclusion and does not explicitly forward requests to the internal component. On the contrary, the external component directly returns the interface pointer of the internal component to the customer.The purpose of aggregation is to convince the customer that the interfaces used by internal components are implemented by external components. Components to be aggregated need to know the iunknow pointer of the external interface and know that they are aggregated.