0

I have two applications and would like the two to communicate texting when a release exception.

The problem is as follows: in an application I use the function

Application.Handle 

to grab the handle of the application.

And in my client I use:

ServerApplicationHandle: = FindWindow ('TForm1', 'Form1');

To know which application should I send the message, but both return different numbers, they would know tell me why?

fymoribe
  • 199
  • 1
  • 11
  • 2
    [pipes](http://en.wikipedia.org/wiki/Pipeline_%28software%29) are normaly the way to communicate between two or more applications. – Eun Apr 09 '14 at 13:58
  • 2
    See [Inter-process communication](http://stackoverflow.com/a/4090025/576719) for communication using pipes. – LU RD Apr 09 '14 at 14:23
  • Memory mapped files are very fast too, and work between applications (on the same computer) – mjn Apr 09 '14 at 16:36
  • @mjn memory mapping is surely not the way to go. How do you signal that there's a new message? How do you synchronize? – David Heffernan Apr 09 '14 at 17:09

2 Answers2

2

As already explained (Main)Form and Application are two different things.
Since Delphi 2007 there is another behavior to note. In dependency of Application.MainformOnTaskbar you are able (or not) to get the handle via Findwindow.

A little snipplet to show the different behavior

var
  FW_ah, FW_mfh, ah, mfh: THandle;

  Procedure Display(OnTask: Boolean);
  begin
    Application.MainFormOnTaskbar := OnTask;
    ah := Application.Handle;
    mfh := MainForm.Handle;
    FW_ah := FindWindow(PChar(Application.ClassName), PChar(Application.Title));
    FW_mfh := FindWindow(PChar(ClassName), PChar(Caption));
    Showmessage(Format('ah: %d FW_ah: %d  -  mfh: %d FW_mfh: %d', [ah, FW_ah, mfh, FW_mfh]));
  end;

begin
  Display(true);
  Display(false);
end;
bummi
  • 27,123
  • 14
  • 62
  • 101
1
  • Application.Handle is the window handle for the hidden window associated with the global Application object.
  • FindWindow('TForm1', 'Form1') will return the window handle of a top-level form in your application.

These are indeed not the same thing. You could, I suppose, use Form1.Handle instead of Application.Handle. However, you would need to be wary of window re-creation.

Frankly this doesn't sound like the best way to do inter-process communication. Perhaps you might consider sockets or named pipes.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490