COM program preparation entry (full text-3)

zhaozj2021-02-16  49

The survival cycle of COM objects and IUNKNOWN interface

The survival cycle of the COM object is divided into two parts: the client and COM itself:

In the client, depending on the COM object interface, like V_OBJ in our example, define a global variable, then COM objects are generated when creating, only when the program is exited. We can also release them in the form, such as: v_obj: = NIL, so this COM interface is invalid.

In the case of COM itself, the COM interface passes the way to complete the survival cycle of COM, why is it using a count, of course, very simple - because COM may be called by multiple programs simultaneously. There is a program to connect to a COM timer plus 1, and a counter is reduced. When the counter is 0, the COM object is really removed from memory.

IUNKNOWN interface:

Why is the reasons why IUNKNOWN interfaces are placed together with the living cycle, and the counters in the COM living cycle are defined in the iUnknown interface: AddRef, Release, QueryInterface. These three interfaces are also IUNKNOWN all houses. For three interfaces or explain:

AddRef: When COM generates a client connection, the AddRef method is responsible for adding a counter 1;

Release: When COM releases a client connection, the Release method is responsible for minus the counter. If the counter is 0, release the COM;

Queryinterface: Because COM supports multiple interfaces, QueryInterface is responsible for identifying the user specified by the user to return the correct VTABLE;

Interface global identification:

When I said QueryInterface, I mentioned the correct interface. In fact, the correct interface is identified by the global identifier. It is a 128-bit number, which is calculated according to the statistical method, and can only identify each interface (theoretical). Specific implementation We don't use the tube, it produces a simple method, and you can generate one in Delphi.

COM implementation in Delphi

I believe that the above introduction to COM should have a preliminary understanding, and now you will write a point, how to write COM in Delphi.

Under Delphi, COM is easier, the most basic element of the Delphi package COM development, as long as you write objects to implement classes, the other full Delphi is getting it.

1. Open Delphi, select File / New / Others, select ActiveX Library, select File / New / Others, select the Com object of the Active page, the more important options in the wizard appears as follows:

Class name: Implement the name of the class, custom.

INCLUDE TYPE LIBRARY: Does Delphi will not generate a type library file if not selected, and the Class Name input is also invalid. It means that the interface class, implement the class, and implement itself. For those who are not very familiar with COM, don't use this way.

Other parameters can be used by default, and the specific significance can be found.

2. Writing of the interface

Select View / Type library, select the interface, right-click New Select Method, enter the name of the interface in the right attributes' Name, add the input and output parameters you want to join in Parameters. Note: When setting the parameter type, if it is returned, the parameter type is added later, add "*". Click Refresh, and a just defined interface appears in the program unit, and you can write a real current code. 3, COM installation

After writing, compile, register with the compiled COM through Run / Register ActiveX Server, install the COM component through Run / Install COM Objects, select the interface in the pop-up dialog box, you can choose to install it in the next dialog. You can also be installed in a new COM application in the COM application. This completes the installation of COM, you can open the component service in the system to see the COM you install.

Due to the busy work, I have not described personal experience more detailed, I will share my own experience and share it with you. Friends are interested in contact me;

QQ: 103222465

Email: jackielee1979@163.com

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

New Post(0)