Link to home
Start Free TrialLog in
Avatar of zordes
zordesFlag for Switzerland

asked on

An example for pipes.pas?

I have downloaded the pipes.pas from Russel Libbys web-page. I have no experience in using named pipes. Has somebody a working example how to use the server and clients?
Avatar of Hypo
Hypo
Flag of Sweden image

Hi,
I have put together an example of how to connect to a server and send data over the pipe. I've attached the code (pas and dfm) below in a zip-file, and also added included the pas-code as a snippet below. I noticed that there is a bug in the pipes.pas file that I got from Russel's page, don't know if it is the same version as you use. However, the bug is in the function TPipeListenThread.Create, and can be fixed in the way mentioned below:

constructor TPipeListenThread.Create(PipeServer: TPipeServer; KillEvent: THandle);
begin
  // Perform inherited create (suspended)
  inherited Create(True);

  // Increment the thread counter
  // FPipeServer.FThreadCount.Increment;  <- this row is where the bug was, FPipeServer is not assigned at this point.
  PipeServer.FThreadCount.Increment;  // Replace the code with this line instead.
...

Regards
Hypo
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Pipes, StdCtrls, ExtCtrls;
 
type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    Edit1: TEdit;
    Button2: TButton;
    Memo2: TMemo;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
    FServer : TPipeServer;
    FClient1 : TPipeClient;
  public
    { Public declarations }
    Procedure OnClientPipeMessage(Sender : TObject; Pipe : Cardinal; Stream : TStream);
    Procedure OnServerPipeMessage(Sender : TObject; Pipe : Cardinal; Stream : TStream);
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
procedure TForm1.FormCreate(Sender: TObject);
begin
  FServer := TPipeServer.Create(self);
  FServer.PipeName := 'MyPipe';
  FServer.Active := True;
  FServer.OnPipeMessage := OnServerPipeMessage;
 
  FClient1 := TPipeClient.Create(self);
  FClient1.PipeName := 'MyPipe';
  FClient1.OnPipeMessage := OnClientPipeMessage;
  if not FClient1.Connect then
    Memo2.Lines.Add('Not connected');
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var S : String;
begin
  S := Edit1.Text;
  if Length(S) > 0 then
    FServer.Write(FServer.Clients[0], S[1], Length(S));
end;
 
procedure TForm1.OnClientPipeMessage(Sender: TObject; Pipe: Cardinal; Stream: TStream);
var S : String;
begin
  if Stream.Size > 0 then begin
    SetLength(S, Stream.Size);
    Stream.Read(S[1], Length(S));
    Memo2.Lines.Add(S);
  end;
end;
 
procedure TForm1.Button2Click(Sender: TObject);
var S : String;
begin
  S := Edit2.Text;
  if Length(S) > 0 then
    FClient1.Write(S[1], Length(S));
end;
 
procedure TForm1.OnServerPipeMessage(Sender: TObject; Pipe: Cardinal; Stream: TStream);
var S : String;
begin
  SetLength(S, Stream.Size);
  Stream.Read(S[1], Length(S));
  Memo1.Lines.Add(S);
end;
 
end.

Open in new window

Avatar of zordes

ASKER

Thanks it works fine.

Could ypu please give me a tip, how to communicate between to applications?
Do I have to set the ServerName in client app?

Thank You
Regards
Zordes
ASKER CERTIFIED SOLUTION
Avatar of Hypo
Hypo
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zordes

ASKER

Thank You.
My app works as expected.
Just the last remark. I assume that the PipeServer can be created in a dll of the desktop application. Do You agree?
Hi, :)
yes, the pipe can be created from within a dll. The only reasons I can think of right now for why a pipe wouldn't be created, is that if the pipename is already occupied, or that the user context from which the process is running does not have enough privileges to create pipes.

regards
Hypo
Avatar of zordes

ASKER

That was really a prompt solution!
Thanks again.