COM program writing entry
Write: Li Xianmin
date:
2004-5-2
Glossary:
OLE: (Object Linking and Embedding "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:
We have written a lot, usually we write DLLs are some ways to define or process to achieve specific operations, of course, the defined 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.
(to be continued…)