启动程序的同时传参给接收程序(XE8+WIN764)
相关资料:
http://blog.csdn.net/yanjiaye520/article/details/7590252
注意事项:
1.ParamStr(0)是实例自己。
2.传的参数是以空格分看的。
3.“‘2 Hello World”,ParamStr(1) = '2' ;
发送窗体:
1 unit Unit1;
2
3 interface
4
5 uses
6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, ShellAPI, Vcl.StdCtrls;
8
9 type
10 TForm1 = class(TForm)
11 Button1: TButton;
12 procedure Button1Click(Sender: TObject);
13 private
14 { Private declarations }
15 public
16
17 { Public declarations }
18 end;
19
20 var
21 Form1: TForm1;
22
23 implementation
24
25 {$R *.dfm}
26
27 procedure TForm1.Button1Click(Sender: TObject);
28 begin
29 ShellExecute(Handle, 'open','project2.exe','2 Hello World',nil,SW_SHOWNORMAL);
30 end;
31
32 end.
接收窗体:
1 unit Unit1;
2
3 interface
4
5 uses
6 Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
7 Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
8
9 type
10 TForm1 = class(TForm)
11 ListBox1: TListBox;
12 Label1: TLabel;
13 procedure FormCreate(Sender: TObject);
14 private
15 { Private declarations }
16 public
17 { Public declarations }
18 end;
19
20 var
21 Form1: TForm1;
22
23 implementation
24
25 {$R *.dfm}
26
27 procedure TForm1.FormCreate(Sender: TObject);
28 begin
29 //ParamStr(0)是实例自己,所以参数从1开始;而且传的参数是以空格分看的,“‘2 Hello World”,ParamStr(1) = '2' 了;
30 ListBox1.Items.Add(ParamStr(1));
31 end;
32
33 end.
PS:如果使用CreateProcess建进程,怎么发参数呢?
1 if not IsBuildInFit then
2 begin //不代参数
3 bActiveState := CreateProcess(PChar(nil), PChar(GetExePath + sHardWareName), @lpsaProcess,
4 @lpsaThread, false, CREATE_SHARED_WOW_VDM, PChar(nil), PChar(nil),
5 lpsiStartInfo, oProcInfo);
6 end
7 else
8 begin //代参数
9 bActiveState := CreateProcess(PChar(nil), PChar(GetExePath + sHardWareName + ' True'), @lpsaProcess,
10 @lpsaThread, false, CREATE_SHARED_WOW_VDM, PChar(nil), PChar(nil),
11 lpsiStartInfo, oProcInfo);
12 end;
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我