?

Log in

No account? Create an account
Кот канарский полосатый толстый

VALID INFO! My crazy life!

Задержись в реальности!

Previous Entry Share Next Entry
Delphi unit для работы с Named Pipes (Russell Libby)
Кот канарский полосатый толстый
r3code
Код юнита Pipes.pas написан товарищем Russell.
Представляет готовый для работы компонент TPipeServer и TPipeClient.
See formatted code updated at 2010 here and raw source here
В виде компонентов для Design Time http://cc.embarcadero.com/Item/21507 (только зарегисрированные пользователи)

Examples of use


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.


  • 1
This Pipe-Unit could be quite useful, but I've got a stupid problem. If I try to put a server on a form and a client on another, that works, but the message that comes to the server is somehow chinese. I'm not shure if the problem is that I don't know how to transform my string to buffer var used in the write command of the client. I think, the other part om the server side is ok:

procedure TForm1.FServerPipeMessage(Sender: TObject; Pipe: Cardinal; Stream: TStream);
var SL : TStringList;
var Meldung : string;
begin
SL := TStringList.Create;
SL.LoadFromStream(Stream);
Meldung := SL.Text;
FreeAndNil(SL);
Memo1.Lines.Add(Meldung);
end;

Ih you have got an idea where the problem could be, it could be really helpful.

  • 1