Delphi: How to send command to other application?
Asked Answered
C

4

13

How to send & receive commands from other Delphi created applications? I want to send command to another application that I've written.

Chancellor answered 24/5, 2011 at 18:47 Comment(5)
What you are asking is a complicated subject. First, what input pipes does your other application use? Is it command line based accepting input on stdin? Do you just want to start it with some switches? Or does it have some API which you can call? Perhaps it uses semaphores or signals? you can't determine how to communicate without determining the pipeline first.Interference
It isn`t so complicated. =p Im just not so smart.Chancellor
I didn't mean to imply anything. I just thought your other application was already written, in which case tossing out communication suggestions wouldn't be very helpful. If you don't have an API to interact with, then an answer explaining how to use one is irrelevant.Interference
I dont understand english so good. Please retell in Latvian. :DChancellor
Courtesy of google translate(I disavow all errors): Es tā negribēju, lai norādītu uz kaut ko. Es tikai domāju, citām jūsu pieteikums bija jau rakstīts, šajā gadījumā tossing no komunikācijas ieteikumi nebūtu ļoti noderīga. Ja jums nav API, kas mijiedarbojas ar, tad atbilde izskaidrotu, kā izmantot vienu, nav nozīmes.Interference
J
29

Sender:

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.

Receiver:

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.

Make sure that the caption of the receiving form is 'My Second Window'.

Jactitation answered 24/5, 2011 at 18:59 Comment(7)
@AndreasNN, this is tutorial type of question, and your answer contains large amount of code which fails to highlight a standard way to declare a message handler (also, newly introduced message identifier should be really shared amongst projects, not copy-pasted, but thats for more advanced question)Nickelplate
...the point in writing a book about technical details. In addition, I still don't see any real pratical problem with my approach above.Jactitation
@Andreas Rejbrand, (i copypasted it because SO fails to provide more convenient way to quote users' display name w/o any kind of shortening and/or mangling) i disagree, the point is to teach the right way™ from the beginning (look at this, message handler method support ascends to TObject). This is VCL, a library designed to isolate us from Windows (or Qt) design!Nickelplate
@user759588: But you need to learn Win API if you are to excel at Windows programming. What if the OP would like to send a string? Then, surely, he needs to consult the Win32 docs on WM_COPYDATA. But my main point is not to be mean... The world is a cruel place at it is, so I think it is a good thing to try to be nice to each other.Jactitation
@user you can't teach programming on stack overflow! This is a Q&A site. The code provided in answers must be understood to be illustrative. Andreas's answer is excellent in any case.Arboriculture
@user759588 and @Andreas please cease and desist. Revenge downvoting and tit for tat comments aren't helpful. Be nice.Molokai
@Kev: You are right. I am sorry. It became too much for me. @David: Thank you for restoring my post! I am sorry for becoming so upset.Jactitation
P
5

Windows Messages might be a solution - an interesting article can be found here: http://delphi.about.com/od/windowsshellapi/a/aa020800a.htm

Puppis answered 24/5, 2011 at 18:50 Comment(5)
This is the solution. No reason to downvote. Compensating, +1.Jactitation
@robrok you are very quick to downvote. Doing so will make us less keen on answering. If I were you I would concentrate on upvoting. Personally I never downvote on my own questions. I leave that to others.Arboriculture
What do you mean by edit the answer? You asked a question, I provided an answer, which is just enough for what you asked about. There is no code in it, but that does not mean it is wrong. You have not asked for code. The articles I linked to will help you to understand the topic of message passing, a complex topic well worth knowing when programming with Delphi or in Windows. EOT.Puppis
What Robrok meant was that if you make an edit to your answer (for instance, adding a %20 at the end of it), then he can change his downvote to an upvote. [If you downvote and wait for five minutes, the downvote gets 'locked in' and you cannot remove it or change it to an upvote unless the question/answer is edited.]Jactitation
@Andreas: Thanks, I did not know that. Seems that I must have read more about how Stack is working, still. @Robrok: please, ignore whatever I wrote in the comment. It seems I must be getting more sleep, I tend to get too nervous recently. Apologies.Puppis
S
4

Look up interprocess communication. Some lightweight appropriate options for you could be:

  • Define your own custom windows message
  • Use WM_COPYDATA
Streetcar answered 24/5, 2011 at 18:49 Comment(0)
I
2

If you are writing both these applications, TCP/IP can be a cleaner solution than windows messages. The two applications can even be on different computers in a network.

Intercollegiate answered 24/10, 2014 at 5:10 Comment(1)
Do u have any tutorial?Bouchier

© 2022 - 2024 — McMap. All rights reserved.