COM program writing entry

xiaoxiao2021-03-06  107

COM program writing entry preparation: Li Xianmin

Date: 2004-5-2

Noun explanation: OLE: (Object Linking and Embedding object links and embedded)

Make the server module and the client module communicate through the standard interface. The two modules can be ignored by the same computer or in different computers. The server module implements a set of interfaces, and the client module communicates through this set of interfaces.

COM: (Component Object Modal Component Object Model)

Implement the function of the OLE, the specific function can be completed:

l Write code for multiple languages;

Multiple languages: Refers to the establishment of a good COM component does not care about accessing its programming language. Any programming language will only know the interface of COM components, access is the same function.

l Create an ActiveX control;

l Manipip other applications by Ole Automation;

Such as: Microsoft Excel's OLE programming interface, after creating an object, any program can implement the operation of Excel.

l Communication with applications on other computers;

Actually, communication between COM interfaces and interfaces, because it implements different languages, different computers, the communication of applications on different computers is very easy.

COM module:

COM's module refers to a stand-alone application (EXE) or a dynamic connection library (DLL). When you implement COM, it is easier to use DLL mode. Because: Applications are independent address controls in memory when loading, and DLL can reside in memory. When multiple clients call COM, if the EXE form is used, there will be multiple EXEs to be loaded, and when the COM handles the client's access, you must switch back and forth in different address spaces. And the DLL form will always have one resident memory, and COM can find the execution code in the same memory space.

Step by step: DLL we have written a lot, usually we write DLLs are all methods or processes to achieve specific operations, of course, exports, are also these methods or processes. Now let's write an exit to a class of DLL to expand our COM write.

Ready to work:

Open Delphi, select File / New / Others, select DLL Wizard to create a DLL project, select File / New / Unit to create new units, all saved.

write the code:

Define an abstract class in the new Unit:

Type

Tcalculator = Class

Public

Function Addition (OP1, OP2: Double): Double; Virtual; Abstract

END;

This class is simple, defining an abstract method to achieve two numbers.

After defining an abstract class, define a class to implement this abstract class:

Type

Tcalcimple = Class (TCALCULATOR)

Public

Function Addition (OP1, OP2: DOUBLE): Double; OVERRIDE;

END;

Method implementation:

Function TCALCIMPLE.Addition (OP1, OP2: Double): Double;

Begin

Result: = OP1 OP2;

END;

Of course, we are still a way to create this class, we are adding as follows: function createcalcimple: tcalcimple; stdcall;

Begin

Result: = tcalcimple.create;

END;

Define the exit:

Exports CreateCalcImple;

This way, our DLL is completed, I will ask again to write an Exe program to call it. The specific is no longer operated here, some source code listing EXE:

Unit unit1;

Interface

Uses

Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, stdctrls;

{Define the same class as the abstract class defined in the DLL, class name can be customized}

Type

Tcalculator = Class

Public

Function Addition (OP1, OP2: Double): Double; Virtual; Abstract

END;

Type

TFORM1 = Class (TFORM)

Button1: tbutton;

EDIT1: TEDIT;

EDIT2: TEDIT;

EDIT3: TEDIT;

Label1: TLABEL;

Label2: TLABEL;

Label3: TLABEL;

Procedure Button1Click (Sender: TOBJECT);

Private

{Private Declarations}

public

{Public declarations}

v_obj: tcalculator;

END;

{Static call DLL, note that the return type is different from the DLL, and the name of the abstract class must be consistent with the DLL.

Function CreateCalcImple: Tcalculator; stdcall; external 'comdll.dll';

VAR

FORM1: TFORM1;

IMPLEMentation

{$ R * .dfm}

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

v_obj: = CREATECALCIMPLE;

Edit3.Text: = floattostr (v_obj.addition (strtofloat (edit1.text), strtofloat (edit2.text));

v_obj.free;

END;

End.

Sublimation to theory:

The object defined in L D ll can only lead to abstract methods. When an object is established, D L is returns a pointer table VTABLE of the virtual method to the application.

l When defining an abstract class, the method defined is:

Function Addition (OP1, OP2: Double): Double; Virtual; Abstract

The reason why the Abstract (abstract method representation) is added later because only abstract methods can be taken out.

l In executing files, actually only create an interface through the DLL exit, but can call it like the object, which is a bit starting to come.

There is a few more points:

1, COM interface can be seen as a placeholder, the specific implementation is in the class corresponding to the interface; just like the Addition method in the TCalculator, just a descriptor without any meaning, but passed After the TCALCIMPLE, there is a specific significance;

2, the access of the COM interface must be accessed after entering the interface class. Just as the CreateCalcimple method is added in our example, the specific interface can only be accessed after the interface class (Interface) is established. 3, that is, a COM must have three elements: interface definition class, interface implementation class, interface creation class. As long as you have these three elements, COM can be implemented.

COM's theory to talk about COM's interface (Interface) is the core of COM. All COM interfaces are derived through IUNKNOWN, which informs customers that those interfaces are valid, that is, they have been defined. The general way to define is as follows:

ISIMPLEINTERFACE = Interface (IUNKNOWN)

Function GetName: String

Procedure setName (v_name: string)

END;

If you join this one in the above interface:

ISIMPLEINTERFACE = Interface (IUNKNOWN)

V_name: String;

Function GetName: String

Procedure setName (v_name: string)

END;

This is not allowed because we say that the interface method is like a placeholder. It is necessary to implement class leadership. V_Name: String This sentence is just a data member will never have any meaning, if you want to define Can only be defined in the implementation class.

Now give an example of COM, there is no practical use, but at least the problem:

Unit unit1;

Interface

Uses

Windows, Messages, Sysutils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, stdctrls;

Type

TFORM1 = Class (TFORM)

Label1: TLABEL;

EDIT1: TEDIT;

Button1: tbutton;

Button2: tbutton;

Procedure formcreate (Sender: TOBJECT);

Procedure Button1Click (Sender: TOBJECT);

Procedure Button2Click (Sender: TOBJECT);

Procedure formclose (Sender: Tobject; VAR Action: Tclosection);

Private

{Private Declarations}

public

{Public declarations}

END;

ISIMPLEINTERFACE = Interface (IUNKNOWN)

Procedure setValue (v_value: integer);

Function GetValue: Integer;

END;

TSIMPLEIMPLE = Class (TinterFaceDObject, ISIMPLEINTERFACE)

Public

Value: integer;

Procedure setValue (v_value: integer);

Function GetValue: Integer;

END;

VAR

FORM1: TFORM1;

v_obj: tsimpleimple;

IMPLEMentation

{$ R * .dfm}

{TsimpleImple}

Function TSIMPLE.GETVALUE: Integer;

Begin

Result: = Value;

END;

Procedure TsimpleIMple.SetValue (v_value: integer);

Begin

Value: = v_value;

Procedure TFORM1.FormCreate (Sender: TOBJECT);

Begin

v_obj: = TsimpleImple.create;

END;

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

v_obj.setValue (strt ");

Edit1.clear;

END;

Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);

Begin

Edit1.Text: = INTTOSTR (v_obj.getvalue);

END;

Procedure TFORM1.FORMCLOSE (Sender: TpoBject; VAR Action: Tclosection);

Begin

v_obj.free;

END;

End.

Blue words define an interface, almost defined in the form of IsImpleInterface, but I want to emphasize that the interface definition is to implement the access of the OLE mode, and implement the class Definition is an implementation of interface functions. Both are different from functional and implementation.

The survival period of the COM object is divided into two parts with the living cycle of the IunkNown Interface COM object: 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 to Ctrl Shift G.

COM implementation believes in Delphi believes that COM should have a preliminary understanding of COM, and now you will write COM in Delphi now. 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 a 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-103048.html

New Post(0)