windows Delphi:如何向其他应用程序发送命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6115296/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:55:34  来源:igfitidea点击:

Delphi: How to send command to other application?

windowsdelphiprocessdelphi-7

提问by Little Helper

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

如何从其他 Delphi 创建的应用程序发送和接收命令?我想向我编写的另一个应用程序发送命令。

回答by Andreas Rejbrand

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'.

确保接收表格的标题是“我的第二个窗口”。

回答by Sorrow

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

Windows 消息可能是一个解决方案 - 可以在这里找到一篇有趣的文章:http: //delphi.about.com/od/windowsshellapi/a/aa020800a.htm

回答by Mike Kwan

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

查找进程间通信。一些适合您的轻量级选项可能是:

  • Define your own custom windows message
  • Use WM_COPYDATA
  • 定义您自己的自定义窗口消息
  • 使用 WM_COPYDATA

回答by sav

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.

如果您正在编写这两个应用程序,TCP/IP 可能是比 Windows 消息更简洁的解决方案。这两个应用程序甚至可以位于网络中的不同计算机上。