An actual OLE server development and use

zhaozj2021-02-16  52

An actual OLE server development and use

First knowledge: Delphi / Com / Ole Automation / SQL Server

Difficulty: ★★ ☆☆☆

In the first few articles we have discussed knowledge about VCL and OLE. In this article we will complete a more actual OLE Automation server program, and finally we package them into the VCL components used in Delphi.

First let's do a practical program, this is a program used to manage customer purchase records before it does not change to the server (it own with the SQL Server connection), which can entroy and delete the customer's purchase record and intuitive display All data is stored in SQL Server. We use it as an OLE Automation for such considerations, suppose we are a large supply company, we may have a lot of systems to use this customer to purchase records and use it to handle corresponding data in SQL Server, but we don't want to Repeat the same processing code each time, we prefer to separate this handler and provide services to other programs. So in the following work we completed this server program, the interface is as follows: (Note, this is just an example, we do not evaluate the good and bad J)

We have more discussing the code of this program (because this is not different from the development of the general program, you can pay the full code of this article according to the final address). Then we will turn it into a server. Select FileànewàothersàActiveXàAutomation Object. Next, Delphi defines the type library and implementation files, and we have to do just add the corresponding server properties and events we want to use in the type library. We simply give interfaces that define this OLE Automation function (from the Object Pascal code generated by the type library):

Icustformole = Interface (Idispatch)

['{D7AE75F9-F838-4702-a8EB-EAD0EED242DE}']

Function GET_CUSTNAME: WIDESTRING; SAFECALL;

Procedure set_custname (const value: wideString); SaFECALL;

Function Get_ProductName: WideString; SaFECALL;

Procedure set_ProductName (const value: wideString); SaFECALL;

Function Get_ProductNum: Integer; SaFECALL;

Procedure set_ProductNum (value: integer); SaFECALL;

Function GET_REMARK: WIDESTRING; SaFECALL;

Procedure set_remark (const value: wideString); SaFECALL;

// The following methods and attributes correspond to the corresponding methods and properties in the original program.

PROCEDURE ADDTODATA; SAFECALL;

PROCEDURE DELDATA; SAFECALL;

Property Custname: WideString Read_CustName Write set_custname

Property ProductName: WideString Read Get_ProductName Write Set_ProductName

Property ProductNum: Integer Read_ProductNum Write Set_ProductNum

Property Remark: WideString Read_Remark Write set_remark;

Icustformoledisp = DISPINTERFACE

['{D7AE75F9-F838-4702-a8EB-EAD0EED242DE}']

Property Custname: WideString Dispid 201;

Property ProductName: WideString Dispid 202;

Property ProductNum: Integer Dispid 203;

Property Remark: WideString Dispid 204;

Procedure addtodata; dispid 205;

Procedure Deldata; DISPID 206;

END;

We now return to the implementation file of the interface, pay attention to the comments in the code, in fact this code is quite simple:

UNIT CustoleImpunit;

{WARN SYMBOL_PLATFORM OFF}

Interface

Uses

Comobj, ActiveX, CustViewole_TLB, STDVCL, Windows

Type

TcustFormole = Class (Tautoobject, IcustFormole)

// Note that we have implemented our ICustFormole interface in front.

protected

Function GET_CUSTNAME: WIDESTRING; SAFECALL;

Function Get_ProductName: WideString; SaFECALL;

Function Get_ProductNum: Integer; SaFECALL;

Function GET_REMARK: WIDESTRING; SaFECALL;

PROCEDURE ADDTODATA; SAFECALL;

PROCEDURE DELDATA; SAFECALL;

Procedure set_custname (const value: wideString); SaFECALL;

Procedure set_ProductName (const value: wideString); SaFECALL;

Procedure set_ProductNum (value: integer); SaFECALL;

Procedure set_remark (const value: wideString); SaFECALL;

END;

IMPLEMENTATION

Uses comServ, CustformUnit;

Function TCUSTFORMOLE.GET_CUSTNAME: WIDESTRING

Begin

Result: = Custform.customedit.Text;

// Can see, we just use the controls and properties of the initial program form, the interface here is equivalent to

// Simplely closed our original program, the following code is similar.

END;

Function TCUSTFORMOLE.GET_PRODUCTNAME: WIDESTRING

Begin

Result: = Custform.ProductedIt.Text;

END;

Function TCUSTFORMOLE.GET_PRODUCTNUM: Integer;

Begin

Result: = Custform.pronuMedit.Value;

END;

Function TCUSTFORMOLE.GET_REMARK: WIDESTRING

Begin

Result: = Custform.Memo1.Lines.Text;

END;

Procedure tcustformole.addtodata;

Begin

Custform.addbutton.click;

END;

Procedure tcustformole.deldata;

Begin

Custform.delbutton.click;

END;

Procedure tcustformole.set_custname (const value: wideString);

Begin

Custform.customedit.text: = value;

END;

Procedure tcustformole.set_ProductName (const value: wideString);

VAR

i: integer;

Begin

I: = Custform.Productedit.Items.indexof (Value);

IF i <> - 1 THEN

Custform.Productedit.ItemIndex: = i

Else

Begin

MessageBox (Custform.handle, 'You don't exist in the product type specified by the customer program!', 'CustProle General error', MB_ICONWARNING);

Custform.Productedit.ItemIndex: = 0;

END;

END;

Procedure tcustformole.set_ProductNum (Value: Integer);

Begin

Custform.pronumEdit.Value: = value;

END;

Procedure tcustformole.set_remark (const value: wideString);

Begin

Custform.Memo1.Lines.Text: = Value;

END;

INITIALIZATION

TautoObjectFactory.create (Comserver, Tcustformole, Class_custFormole,

CIMULTIINSTANCE, TMAPARTMENT);

End.

Now we can actually test and use this server, we can create a new project, choose Project -> Import Type Library ... You can find that there is already the server information we have just created (of course, you have already run the server Program), then Create Unit adds the PASCAL file generated by the corresponding type file to our project, one but we launched the server. We can easily use the properties and methods in the interface:

Function TFORM1.GETDEFAULTINTERFACE: ICUSTFORMOLE;

Begin

IF not assigned (finterface) THEN

Finterface: = CocustFormole.create; // Note that you can find CocustFormole meaning in the PASCAL file generated by the type library file

RESULT: = FINTERFACE;

END;

Due to the reason, we cannot give all the code for the test program (in fact with server programs, our test client wants to handle the corresponding data in SQL Server is quite simple.), You can follow the address of the text I will ask (explain that the database in this article uses the SQL Server, so I will send you the program you need to restore the database backup to your SQL Server and modify the corresponding connection string, otherwise the program cannot run).

At the end of this article, we introduce a simpler way to use the server we have developed, that is to package it into the components in Delphi, choose Project -> Import Type Library ... we developed in the server, then install Delphi will do a lot of work for us in an existing package or your new component package, and finally you can find new components installed from the panel you specify, now you can use the ordinary VCL components. Use our developed server. (Note that Delphi defines a class inherited from toleContol, all complex work is done by Delphi behind us, if you are interested, it is recommended to study this component Delphi automatically generated a lot of code. ). Request address: hk.barton@sohu.com

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

New Post(0)