0

I have a requirement, Where I have to pass a value from application1 to application2. Got a solution from following link:

Delphi: How to send command to other application?

The sample code of sender is as below:

unit Unit1;

interface

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

const
  WM_MY_MESSAGE = WM_USER + 1;

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

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  h: HWND;
begin
  h := FindWindow(nil, 'My Second Window');
  if IsWindow(h) then
    SendMessage(h, WM_MY_MESSAGE, 123, 520);
end;

end.

The sample code of receiver is as below:

unit Unit1;

interface

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

const
  WM_MY_MESSAGE = WM_USER + 1;

type
  TForm1 = class(TForm)
  private
    { Private declarations }
  protected
    procedure WndProc(var Message: TMessage); override;    
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WndProc(var Message: TMessage);
begin
  inherited;
  case Message.Msg of
    WM_MY_MESSAGE:
      ShowMessageFmt('The other application sent the data %d and %d.', [Message.WParam, Message.LParam]);
  end;
end;

end.

But this doesn't work properly when the application with title 'My Second Window' is minimized. Any help is appreciated.

user3733328
  • 133
  • 1
  • 7
  • 2
    Don't use the wibdow handle of a form. These can cha he during the life of the form because of VCL window recreation. Use AllocateHWnd instead to allocate a stable window handle. – David Heffernan Aug 20 '18 at 05:59
  • 1
    I can not reproduce the problem with Delpfi XE7 on Windows 7 32bit, as I also could not reproduce the same problem presented some 4 days ago by @IndhuRajendran (where I used a different code) . Which Delphi version and Windows version are you using? And please do follow the advice of David Heffernan. – Tom Brunberg Aug 20 '18 at 09:16
  • I am using Delphi 5 and windows 7. – user3733328 Aug 20 '18 at 09:59
  • I don't have Delphi 5 anymore, but I tested with Delphi 7 and no problem. AppB shows the message box independent of minimized or not. I guess you need to debug, what fails, if anything, in `Button1Click` of AppA – Tom Brunberg Aug 20 '18 at 10:34
  • Do not search for window, but [RegisterWindowMessage](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644947(v=vs.85).aspx) then ;-) – Victoria Aug 20 '18 at 10:54
  • Can we pass values using the Registerwindowmessage across applications? Can you give me an example implementation?? – user3733328 Aug 20 '18 at 10:58
  • You can presumably answer everything in theat last comment by doing some research. Find a websearch engine. – David Heffernan Aug 20 '18 at 11:24
  • - *" Windows message fails .... "* - How do you know that? - *" .. this doesn't work properly .."* - What does that mean? – Sertac Akyuz Aug 20 '18 at 19:43

0 Answers0