Delphi程序带参数运行

来源:互联网 发布:淘宝店铺租赁平台 编辑:程序博客网 时间:2024/06/04 17:39

程序1

program E1;uses  Forms,Dialogs,SysUtils,  EndM1 in 'EndM1.pas' {Form2};{$R *.res}begin  Application.Initialize;  Application.CreateForm(TForm2, Form2);  if ParamCount<>3 then  begin    ShowMessage('缺少参数:'+Inttostr(ParamCount));    Application.Terminate;    Exit;  end;  //在运行时去读是否有带参数值  if ParamStr(1)<>'-x' then  begin    ShowMessage('缺少参数:'+ParamStr(1));    Application.Terminate;    Exit;  end;  Application.Run;end.

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

程序2

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

相关参考:

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

原创粉丝点击