Some basic knowledge about COM

zhaozj2021-02-11  244

The basic knowledge of the component object model is increasingly popular based on the component-based software development. Here I am sorting some things about COM, for beginners. One. Components (COM), is a new software development technique developed by Microsoft's software production in the computer industry to develop human behavior. Under the COM architecture, people can develop a variety of functional specific components, and then combine them, constitute a complex application system. The benefits of this is multifaceted: you can replace the components in the system so that you can upgrade and customize your system at any time; you can use the same component in multiple application systems; convenient application The system extends to the network environment; COM and language, the platform-independent features make all the programmers to make full playback component module; etc. COM is a way to develop software components. Components are actually some small binary executables that provide services to applications, operating systems, and other components. Developing custom COM components as developing dynamic, object-oriented APIs. Multiple COM objects can be connected to form an application or component system. And the components can be removed or replaced without being reinprinting or compiling applications without being reinprinting or compiling applications. Many technologies of Microsoft, such as ActiveX, DirectX, and OLE, etc. are established based on COM. And Microsoft developers also use COM components to customize their applications and operating systems. The concept contained in COM is not only valid under the Microsoft Windows operating system. COM is not a big API. It is actually a programming method like structured programming and object-oriented programming methods. In any operating system, both developers can follow the "COM method". An application is usually made up of a single binarily. When the compiler generates an application, the application generally does not change any changes before recompiling and issuing new versions of the next version. The changes in operating systems, hardware and customer needs must wait until the entire application is regenerated. This situation has changed. Developers began to separate individual applications into multiple separate parts, as parties. The advantage of this approach is to replace some components with new components with the continuous development of technology. The application at this time is gradually improved with the new component constantly replaces the old components. Moreover, users can also establish a new application quickly with existing components. Traditional approach is to divide the application into files, modules, or classes, and then compile them and link into a single mode application. It is very different from the process of establishing an application (called component architecture) with components. A component is similar to the same miniature application, ie the binary code that has been compiled and can be used, and the application is packaged by multiple such components. Single mode applications have only one binary code module. Custom components can connect to other components to constitute an application at runtime. When you need to modify or improve your application, you only need to replace some of the components that make up this application. COM, that is, component object models, is how to create components and how to establish an application through components, how to dynamically update components. Advantages of using components: One advantage of component architecture is to evolve with time over time. In addition, there are some convenient and flexible use of components that can make it more convenient and flexible in applications, such as the customization, component libraries, and distributed components. The advantages of using components are directly derived from the insertion or discharge applications that can be dynamically. To achieve this, all components must meet two conditions: First, the components must be dynamically linked; second, they must hide (or pack) their internal implementation details. Dynamic links are a crucial requirement for components, while messaging is a necessary condition for dynamic links.

two. The interface is for COM, the interface is a memory structure that contains a function pointer array. Each array element contains a function address implemented by the component. For COM, the interface is this memory structure, other things; all COM does not care for implementation details. In C , the COM interface can be implemented with an abstract base class. Since a COM component enables support for any number of interfaces, for such components, multiple inheritances of abstract base classes can be implemented. Using classes to implement components will be easier than other methods. For customers, a component is an interface set. Customers can only be deal with COM components through interfaces. Alternatively, customers can say little about a component. Normally, customers do not even know all of the interfaces provided by a component. The interaction of the client is done through the interface. When the client querying the other interfaces, it is also done through the interface. This interface is iUnknown. The definition of the IUNKNOWN interface is included in the header file of Unknown.h in Win32 SDK. The reference is as follows: Interface iUnknown {Virtual HRESULT-_ _STDCALL QUERYINTERFACE (const IID & IID, void ** PPV) = 0; Virtual Ulong__stdcall addRef () = 0; virtual ulong_ _release () = 0;

All COM must inherit IUNKNOWN. You can query other interfaces of the component with the interface pointer of IUNKNOWN, and the first three functions in the VTBL of each interface are queryinterface, addref, and release. This allows all COM interfaces to be processed as an IUNKNOWN interface. Since all interfaces support QueryInterface, any interface of the component can be used by the customer to get other interfaces it support. After using QueryInterface, the component is abstracted by multiple mutually independent interfaces, the life period of the component is also required. This is achieved by a reference for the interface. Customers cannot directly control the life of the components. When using an interface to use another interface to use the component, it cannot be released. The release of the component can be done by the component after all the components used in the customer. The other two member functions of IUNKNOWN is the role of addRef and release to provide customers with a means that makes it demonstrated to process an interface. AddRef and Release implementation is a memory management technology called reference technology. This reference count value will increase when the customer gets an interface from the component. When the customer uses an interface, the component's reference count value will be reduced. When the reference count value is 0, the component can delete it from memory. AddRef and Release can increase and reduce this count value. three. Creating a component is divided into multiple interfaces just to apply a single mode to a plurality of portions, and the components need to be placed in a dynamic link library (DLL). The DLL is a component service program, or is a way of distribution components. Components should actually be seen as an interface set implemented in the DLL. Before the customer gets a component interface pointer, it must first load the corresponding DLL into its process space and create this component. Since all functions needed by the customer component can be accessed by an interface pointer, the CreatinStance function can be called in the DLL to cause the user to call it. Thereafter, the DLL can be loaded and the function is called. This feature can be implemented by the COM library function cocreateInstance. COCREATEINSTANCE The process of creating a component is to pass it a CLSID, then it creates a corresponding component and returns a pointer to the requested interface. However, CoCreateInstance did not provide customers with a method of controlling component creation processes, lacking certain flexibility. In fact, common class plants to create components. Class Factory is a component with an interface that can create other components. The customer first creates a class factory itself, then uses an interface (such as iClassFactory) to create the required components. Then use DllRegisterSever in Windows in Windows. four. The multiplex COM component can be multiplexed, which supports "interface inheritance". This inheritance refers to a class or interface that inherits its base class. Abstract base categories are the purest interface inheritance, and it is also used to implement COM interfaces. In COM, we can use inclusion and aggregation to modify components. The inclusion is done at the interface level. The external component contains a pointer to the internal interface. At this point, the external components are only a customer of internal components, which will use internal components to implement its own interface. External components can also re-implement an interface supported by the internal components by forwarding the call to the internal components. And the external components can also be modified to the interface before and after the internal component code. Aggregation is a variant of a variation. When an external component aggregates an interface of an internal component, it does not re-implement this interface as an inclusive, and reverse the call request to the internal components. Instead, the external component returns the interface pointer of the internal components to the customer. With this method, the external components will not need to re-implement and forward all functions in the interface. The inclusion and aggregation provide a great robust mechanism for reuse of components. Under the assembly architecture, the customer is completely separated by the implementation of the component. Fives. Summary is about some basic knowledge about COM. Compliance with COM specification will greatly change the traditional software production method and have broad development prospects.

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

New Post(0)