测试环境:Delphi7 + Win7
发起端
unit uRequest; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TfrmRequest = class(TForm) Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var frmRequest: TfrmRequest; implementation uses ShellAPI, superobject, EncdDecd; {$R *.dfm} //推荐此种方式 procedure TfrmRequest.Button1Click(Sender: TObject); var sExeName, sParams: string; soParams: ISuperObject; begin //待执行的应用程序路径,可以是绝对路径,也可以是相对路径。 //当然目录地址可以通过ShellExecute的第五个参数设置,如果不设置,默认的就是当前目录。 sExeName := 'Response.exe'; //标准的传参是允许通过空格的方式传入多个参数的, //但是有可能某个参数值中就有空格,所以这里采用JSON的方式,转义之后再接收 soParams := SO(); soParams.S['name'] := '张三'; soParams.I['age'] := 20; soParams.S['url'] := 'https://www.baidu.com/'; sParams := UTF8Encode(AnsiToUtf8(soParams.AsJSon())); ShellExecute(0, 'open', PChar(sExeName), PChar(sParams), nil, SW_SHOWNORMAL); end; //不建议此种方式 procedure TfrmRequest.Button2Click(Sender: TObject); var sExeName, sParams: string; begin sExeName := 'Response.exe'; //传参是允许通过空格的方式传入多个参数的 sParams := '张三 20 www.baid.com'; ShellExecute(0, 'open', PChar(sExeName), PChar(sParams), nil, SW_SHOWNORMAL); end; end.
接收端(即被打开的应用程序)
unit uResponse; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TfrmResponse = class(TForm) btnResponse: TButton; Memo1: TMemo; procedure btnResponseClick(Sender: TObject); procedure FormCreate(Sender: TObject); private { Private declarations } function ReponseParam: Boolean; public { Public declarations } end; var frmResponse: TfrmResponse; implementation uses superobject, EncdDecd; {$R *.dfm} procedure TfrmResponse.btnResponseClick(Sender: TObject); var i: Integer; s: string; begin for i := 1 to ParamCount do begin s := ParamStr(i); ShowMessage(s); end; end; function TfrmResponse.ReponseParam: Boolean; var sResponse: string; soParams: ISuperObject; begin Result := False; //接受传参 if ParamCount = 0 then Exit; //注意,传参的下标是从1开始 case ParamCount of 1: begin sResponse := ParamStr(1); sResponse := UTF8Decode(sResponse); soParams := SO(sResponse); Memo1.Lines.Add('姓名:' + soParams.S['name']); Memo1.Lines.Add('年龄:' + IntToStr(soParams.I['age'])); Memo1.Lines.Add('博客地址:' + soParams.S['url']); end; 3: begin Memo1.Lines.Add('姓名:' + ParamStr(1)); Memo1.Lines.Add('年龄:' + ParamStr(2)); Memo1.Lines.Add('博客地址:' + ParamStr(3)); end; else Exit; end; Result := True; end; procedure TfrmResponse.FormCreate(Sender: TObject); begin if not ReponseParam then close; end; end.