3

Possible Duplicate:
Looking for an alternative to windows messages used in inter-process communication
Inter-process communication

I have a Delphi 7 App running that exposes a Type Library. One of the function the TLB exposes is ProcessFile(fileName: string). When that function is called I would like a Windows Service running (also a Delphi 7 application) to get notified that this event was called and what the filename is.

Any thoughts on how to do this would be much appreciated. I have already tried to do this via changed callbacks using the registry and a txt file, but either way I occasionally lose a file that is to be processed as this ProcessFile method can be called many times a second.

Thanks!

Community
  • 1
  • 1
daktmacfan
  • 83
  • 1
  • 6

1 Answers1

7

Pipes are perfect for that job. You can implement a pipe reader thread at your service side and send corresponding string data through the pipe in ProcessFile method at client side.

A quick, simple no error checking Pipe usage example:

PipeServer

var
  Form1: TForm1;
  t: TReadpipe;

  h: THandle;
  msg: string;

const BufSize=512;

implementation

{$R *.dfm}

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  chandle: THandle;
begin
  t.Terminate;

  chandle := CreateFile('\\.\pipe\myNamedPipe', GENERIC_WRITE, 0, nil,
    OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0);
  CloseHandle(chandle);

  t.WaitFor;
  t.Free;
  CloseHandle(h);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  t := Treadpipe.Create(false);
end;

{ TReadPipe }

procedure TReadPipe.Execute;
var
  buf: array [0 .. BufSize-1] of char;
  read: cardinal;
begin
  while not Terminated do
  begin
    h := CreateNamedPipe('\\.\pipe\myNamedPipe', PIPE_ACCESS_DUPLEX,
      PIPE_TYPE_MESSAGE or PIPE_READMODE_MESSAGE or PIPE_WAIT,
      PIPE_UNLIMITED_INSTANCES, BufSize, BufSize, 0, nil);

    ConnectNamedPipe(h, nil);

    if Terminated then
      break;

    msg := '';

    repeat
      FillChar(buf, BufSize, #0);
      ReadFile(h, buf[0], BufSize, read, nil);

      msg := msg + Copy(buf, 0, read);
    until GetLastError <> ERROR_MORE_DATA;

    if msg <> '' then
      Synchronize(
        procedure
        begin
          form1.Memo1.Lines.Add(msg)
        end);
  end;

  DisconnectNamedPipe(h);
end;

PipeClient

var
  Form1: TForm1;
  i: Integer = 0;
  pipe: THandle;

const
  BufSize = 512;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  buf: array [0 .. BufSize - 1] of char;
  written: Cardinal;
  str: pchar;
begin
  pipe := CreateFile('\\.\pipe\myNamedPipe', GENERIC_WRITE, 0, nil,
    OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, 0);

  if pipe = INVALID_HANDLE_VALUE then
    Halt;

  inc(i);
  str := pchar('someStr' + inttostr(i));
  fillchar(buf, BufSize, #0);
  move(str[0], buf[0], Length(str) * Sizeof(char));
  WriteFile(pipe, buf[0], Length(str) * Sizeof(char), written, nil);

  CloseHandle(pipe);
end;

Also remember pipes works as FIFO.

Hasan Manzak
  • 663
  • 6
  • 14
  • Won't you need to secure the pipe? – David Heffernan Nov 20 '12 at 11:43
  • This is a quick example of simple pipe usage. User should take security measures, indeed. – Hasan Manzak Nov 20 '12 at 16:43
  • Hi @The_aLiEn I look your suggestion and can follow it, but in your server code snippet you call CtreateNamedPipe('\\.\pipe\myNamedPipe', PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE or PIPE_READMODE_MESSAGE or PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, BufSize, BufSize, 0, nil); as well as ConnectNamedPipe(h, nil); What library or Delphi units are these a part of? My guess is they are something form a 3rd party (hopefully open source)? If so could you please provide a link to where I could get them or provide the code for these procedures? Thanks! David – daktmacfan Nov 20 '12 at 21:51
  • They're not from a 3rd party unit, they're pure WinAPI ;) And the documentation is completely free as it is Windows SDK, obviously. You cant find any insight code of those APIs, as Windows is not an open source software, but in SDK there're lots and lots of stuff those explains how to use, how not to use and explanations of those APIs and parameters. [Here](http://msdn.microsoft.com/en-us/library/windows/desktop/aa365781%28v=vs.85%29.aspx) is corresponding documentation. – Hasan Manzak Nov 21 '12 at 04:46