FavoriteLoading
0

Delphi程序带参数运行

程序1

  1. program E1;
  2. uses
  3. Forms,Dialogs,SysUtils,
  4. EndM1 in 'EndM1.pas' {Form2};
  5. {$R *.res}
  6. begin
  7. Application.Initialize;
  8. Application.CreateForm(TForm2, Form2);
  9. if ParamCount<>3 then
  10. begin
  11. ShowMessage('缺少参数:'+Inttostr(ParamCount));
  12. Application.Terminate;
  13. Exit;
  14. end;
  15. //在运行时去读是否有带参数值
  16. if ParamStr(1)<>'-x' then
  17. begin
  18. ShowMessage('缺少参数:'+ParamStr(1));
  19. Application.Terminate;
  20. Exit;
  21. end;
  22. Application.Run;
  23. end.

Paramcount-->用于返回命令行参数的个数
Paramstr数组-->用于返回指定的命令行参数
showmessage('命令行参数个数为:'+inttostr(paramcount));
showmessage('第1个命令行参数为:'+paramstr(1));
showmessage('应用程序名称为:'+paramstr(0));

程序2

  1. uses ShellAPI;
  2. {$R *.dfm}
  3. //function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,Directory: PChar; ShowCmd: Integer): HINST; stdcall;
  4. procedure TForm1.btn1Click(Sender: TObject);
  5. var
  6. sFileName,sParam,sFilePath: string;
  7. begin
  8. sFileName:=ExtractFilePath(Application.ExeName)+'E1.exe';
  9. sFilePath:=ExtractFilePath(Application.ExeName);
  10. sParam:='-x -y -z';
  11. ShellExecute(0, 'Open', PChar(sFileName), PChar(sParam), PChar(sFilePath), SW_SHOW);
  12. end;

 

相关参考:

Delphi调用外部程序函数:WinExec() 和ShellExecute详解