How to send & receive commands from other Delphi created applications? I want to send command to another application that I've written.
Asked
Active
Viewed 1.9k times
13

Little Helper
- 2,419
- 9
- 37
- 67
-
1What 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. – Spencer Rathbun May 24 '11 at 18:58
-
It isn`t so complicated. =p Im just not so smart. – Little Helper May 24 '11 at 19:04
-
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. – Spencer Rathbun May 24 '11 at 19:08
-
I dont understand english so good. Please retell in Latvian. :D – Little Helper May 24 '11 at 19:09
-
1Courtesy 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. – Spencer Rathbun May 24 '11 at 19:11
4 Answers
28
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'.

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

Andreas Rejbrand
- 105,602
- 8
- 282
- 384
-
1@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](http://docwiki.embarcadero.com/RADStudio/en/Declaring_a_New_Message-handling_Method) (also, newly introduced message identifier should be really shared amongst projects, not copy-pasted, but thats for more advanced question) – Premature Optimization May 24 '11 at 19:51
-
...the point in writing a book about technical details. In addition, I still don't see any real pratical problem with my approach above. – Andreas Rejbrand May 24 '11 at 19:58
-
@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](http://docwiki.embarcadero.com/RADStudio/en/Understanding_the_message-handling_system), message handler method support ascends to `TObject`). This is VCL, a library designed to isolate us from Windows (or Qt) design! – Premature Optimization May 24 '11 at 20:18
-
@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. – Andreas Rejbrand May 24 '11 at 20:22
-
2@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. – David Heffernan May 24 '11 at 20:29
-
@user759588 and @Andreas please cease and desist. Revenge downvoting and tit for tat comments aren't helpful. Be nice. – Kev May 24 '11 at 20:52
-
5@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. – Andreas Rejbrand May 24 '11 at 20:54
5
Windows Messages might be a solution - an interesting article can be found here: http://delphi.about.com/od/windowsshellapi/a/aa020800a.htm

Miki
- 7,052
- 2
- 29
- 39
-
This is *the* solution. No reason to downvote. Compensating, +1. – Andreas Rejbrand May 24 '11 at 19:00
-
@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. – David Heffernan May 24 '11 at 19:31
-
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. – Miki May 24 '11 at 19:41
-
1What 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.] – Andreas Rejbrand May 24 '11 at 19:45
-
@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. – Miki May 24 '11 at 19:55
4
Look up interprocess communication. Some lightweight appropriate options for you could be:
- Define your own custom windows message
- Use WM_COPYDATA

Mike Kwan
- 24,123
- 12
- 63
- 96
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.

sav
- 2,064
- 5
- 25
- 45