Evolution from C ++ to COM

xiaoxiao2021-03-06  108

The problem we have to solve is to reuse questions, our goals:

(1) When the reused software is upgraded, the client does not need to recompile.

(2) The reused software should be easily extended and upgraded.

(3) On the basis of the customer's normal use of multiplexed component functions, the component exposes as few information as possible.

(4) It is more convenient to use the components as possible to transfer many cumbersome work from the client to the components. == "All things that can be completed inside the component will never be completed outside the components.

The core of COM is: Interface. It solves two main problems when multiplexing:

(1) Different compilers have issued problems with different implementation problems and names of specific technologies. First, the client program source code only needs to introduce interface definitions, and different compilers are the same as the structure of the VTBL of the same interface, and all component functions are transferred through the same VTBL. Second, since the user calls the functionality of the component through the interface, there is no problem with other export functions, so there is no problem with the name.

(2) Components only export interfaces, rather than derived classes, avoiding problems that generate operational errors when the client does not recompile the size of the size of the class in the component.

From C start evolution:

1. There are two ways for C programs:

(1) Source multiplexing:

That is, the client gets directly A.h file and a.cpp source code, add it to the client, and then compiles all the code, link.

Question: First, when there are three running applications on the client machine multiplex A, then there will be three same code in memory. Second, when A is upgraded or modified, all source code must be distributed to all customers again, and all client programs must be recompiled.

(2) Target code multiplexed:

a. Static library: Customers get A.h files and corresponding static library files (mainly composed of .Obj), then compile customer code, and the static library files obtained with the link.

Question: As above, only when A.h does not change, the client program only needs to be re-linked.

b. Ordinary DLL: Customers need to know the Signature Signature and exported class of the DLL export, and then load the DLL at runtime and call the function.

Question: Mainly, each compiler is different from the processing of the same problem, such as the export name of the same signature, the name of the same signature, may be inconsistent.

All of the above methods have a potential assumption: that is the same as the compiler of the development source code and the compiler used by all customers, and it is impossible in reality. Due to different compilers, the layout of the derived function may not be the same, so the three basic reuse above is difficult to establish, not to mention the upgrade support.

2. Ordinary DLL

The above is mentioned above, and then put the problem first, there is a very important issue, assuming A.dll exports a class CA, a customer program uses this DLL export class CA work very well. Among there, customers need to know the declaration structure of the DLL. There is a similar code in the client program:

Ca * aa = new CA ();

But when A. DLL is upgraded to AA.DLL in order to enhance the function, the class of CAs derived. When the customer uses AA.DLL instead of A.dll, it will appear again. This is because the client has not recompiled, so it still uses the SizeOf (original CA) space to accommodate a larger CA object, of course, there is an error. (1) A solution to solve this problem is to use a handle, so that the size of this package CA * will never change regardless of the size of the CA. == "It is very cumbersome to implement a handle for each export class.

(2) Another solution: Do not create objects in the DLL in the client program, but to move the work of the object from the client to the DLL inside, the DLL only exports another customer method, the method creates an object And return this new object's pointer.

3.1 / 2com DLL

It should be noted that different compilers are for an interface (this interface does not contain a pure puzzle, because different compilers are different from the VPTR and VTBLs that are unlocked in the VTBL in VTBL). of. Therefore, consider that the interface is completely separated from the implementation. In conjunction with the above 2 (2) client, you don't need to know the structure of the DLL in DLL, but only the declaration structure of the interface used.

4.3 / 4 COM DLL == "The class in the DLL is inherited from the multi-interface

The client will use RTTI when converting the pointer returned to the upward conversion 2 (2), but the RTTI is related to the compiler, so again, move this upward converted action to the DLL, let The DLL exports a function again to perform this conversion and returns the appropriate pointer.

5.4 / 5com DLL

From 4, the customer can get multiple interface type pointers to the same entity, which executes the DELETE operation for multiple pointers, which will cause the runtime error, and the customer must remember which one of the pointers should be, guaranteed Each object only calls a DELETE operation. == "Once again, this operation is moved from the client to the DLL inside, and the reference count mechanism is generated.

6. COM DLL, COM EXE

Finally standardize all operations and provide the COM library support for COM components, and the COM library is the bridge between customers and COM components.

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

New Post(0)