2

Possible Duplicate:
Inter-process communication

Using Delphi, is there any possibility that I build two simple programs that communicate and interact with each other, let's say by clicking a button in the first one the other one displays a message.

Is It Possible?

Community
  • 1
  • 1
Billo .S
  • 119
  • 1
  • 10
  • See [Cromis IPC and IMC](http://www.cromis.net/blog/2012/05/cromis-ipc-and-cromis-imc-updated/) for examples of using named pipes and tcp for interprocess communication. – LU RD Dec 25 '12 at 09:29
  • The simplest solution is WM_COPYDATA – David Heffernan Dec 25 '12 at 09:43
  • It would help if you could specify a little bit more what level of quality (QoS) you are looking in the communication. Things like: can you afford to loose messages, what should happen when one application is not up. Additionally, performance and scalability requirements and message type/payload (if any). – Jack G. Dec 25 '12 at 15:12

2 Answers2

3

There are many possebilties for IPC

  • Sending window messages
  • using Named Pipes
  • using TCP-IP / UDP
  • shared memory

and so on ...

Easiest way would be sending Messages to a window handle found by FindWindow Named Pipes and TCP-IP should be prefered for extensive communications.

Microdemo:

First Project:

unit Unit2; 

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TTMiniDemoSender = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  TMiniDemoSender: TTMiniDemoSender;

implementation

{$R *.dfm}
Const
     C_MyMessage=WM_USER + 1234;



procedure TTMiniDemoSender.Button1Click(Sender: TObject);
var
 wnd:HWND;
begin
    wnd := FindWindow('TTMiniDemoReceiver',nil);
  if wnd<>0 then SendMessage(wnd,C_MyMessage,123,456);

end;

end.

Second Project:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

Const
     C_MyMessage=WM_USER + 1234;
type

  TTMiniDemoReceiver = class(TForm)
  private
    { Private-Deklarationen }
    Procedure MyMessage(var MSG:TMessage); message C_MyMessage;
  public
    { Public-Deklarationen }
  end;

var
  TMiniDemoReceiver: TTMiniDemoReceiver;

implementation

{$R *.dfm}

{ TTMiniDemoReceiver }

procedure TTMiniDemoReceiver.MyMessage(var MSG: TMessage);
begin
   Showmessage(IntToStr(MSG.WParam) + '-' + IntToStr(MSG.LParam) );
   msg.Result := -1;
end;

end.

For transferring more information you could use WM_CopyData

bummi
  • 27,123
  • 14
  • 62
  • 101
  • ,, please if you could provide more details on FindWindow and Tcp ,, i don't know what are those !!!! – Billo .S Dec 25 '12 at 09:25
  • TCP is http://en.wikipedia.org/wiki/Transmission_Control_Protocol – Arioch 'The Dec 25 '12 at 09:26
  • @bummy in windows TCP makes redundant memory copying. For extensive communication shared-mem based approach is slightly better – Arioch 'The Dec 25 '12 at 09:30
  • 2
    In fact shared mem is a nightmare of complexity. Message passing is far simpler. – David Heffernan Dec 25 '12 at 09:44
  • Complexity may be implemented once and incapsulated, like interlocked TQueue or somthing. And yes, massively extensive communication might be complex. Messaging requires either broadcast with RegisterWindowsMessage or lazy FindWindow approach, which always leaves me in FUD: how many men named David Hefferman live in your country ? :-) – Arioch 'The Dec 25 '12 at 09:57
  • @Arioch who is David Heffer*m*an? – David Heffernan Dec 25 '12 at 10:25
  • @David Oops, sorry :-) That's why WM_COPYDATA failed :-D – Arioch 'The Dec 25 '12 at 11:02
  • @Arioch'The Your preferred mechanism that allows one app to find another can also be used with `WM_COPYDATA`. I just don't see why you want to bring in performance issues here. Doesn't sound like performance is critical for Billo. Something simple that works is surely best. – David Heffernan Dec 25 '12 at 11:13
  • @David that comment of copying was not to Billo but to Bummi, who referenced extensive communications – Arioch 'The Dec 25 '12 at 11:35
  • @Bummi - one more note, you'd better change `SendMessage` to `PostMessage` to avoid program freeze whether another program misbehaves – Arioch 'The Dec 25 '12 at 11:37
  • @Arioch'The in this case you are right, but it depends, sometimes you'll need the result of a message, in other cases it might be dangerous e.g. WM_CopyData. – bummi Dec 25 '12 at 11:51
  • 2
    In fact SendMessageTimeout is usually the correct approach. – David Heffernan Dec 25 '12 at 12:10
1

For applications running on different systems and other advances requirements, there are also messaging solutions like Microsoft Message Queuing (MSMQ) or cross-platform message brokers based solutions like the open source systems Apache ActiveMQ, HornetQ and RabbitMQ.

With these messaging systems, it is easy to implement reliable peer-to-peer communication which even works if the receiver is currently not listening.

There are Delphi / Free Pascal client libraries available, commercial and open source.

mjn
  • 36,362
  • 28
  • 176
  • 378