Mega Code Archive

 
Categories / Delphi / Examples
 

Client-server with com

This example shows step by step how to make a fundamental COM Client-Server application. The Server: Choose: New application. Put a Memo on the Form. Choose: New-ActiveX-Automation Object. Give the Object a CoClass name. (MasComObj) Instancing: Single instance Threading model: single Click OK. In the Project-Library wizard: Right-click on the interface: IMasComObj Choose: New - Method. Give the method a name: WriteToMemo Choose: Parameters - Name: Data, Type: BSTR, Modifier: [in] Refresh implementation. In Unit2: Put Unit1 in the Uses clause. Put this code in the WriteToMemo procedure: implementation uses ComServ, Unit1; procedure TMasComObj.WriteToMemo(const Data: WideString); begin Form1.Memo1.Lines.Add(Data); end; Compile and save the project as Server.dpr The Client : Choose: New application. Put a Memo and a Button on the Form. Save the unit as Cl.pas and the project as Client.dpr Put Server_TLB in the upper uses clause: uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Server_TLB; Put this code in the private clause: MasServer: IMasComObj; Put this code in the FormCreate eventhandler: MasServer:= CoMasComObj.Create; Put this code in the Button1Click eventhandler: MasServer.WriteToMemo(Memo1.Lines.Text); Compile and run. If everything is right both the Client and the Server should start. Write something in the Client's Memo. Press the Button and the same text should appear in the Server's Memo.