Delphi ShellExecuteExWait运行一个程序等待其完成再执行别的
发布时间:2024-04-01 23:05:56 浏览量:72
//delphi定义方法ShellExecuteExWait用于程序执行等待,完成程序执行后再运行后面的程序或代码;
procedure ShellExecuteExWait(const sProgramm: string; const sParams: string = ''; fHide: Boolean=False);
var
ShExecInfo: TShellExecuteInfo;
begin
FillChar(ShExecInfo, sizeof(ShExecInfo), 0);
with ShExecInfo do
begin
cbSize := sizeof(ShExecInfo);
fMask := SEE_MASK_NOCLOSEPROCESS;
lpVerb := 'open';
lpFile := PChar(sProgramm);
lpParameters := PChar(sParams);
//lpDirectory := PChar(Rjsflj); //Rjsflj可选,定义程序所在目录文件夹
if fHide then
nShow := SW_SHOW
else
nShow := SW_HIDE
end;
if (ShellExecuteEx(@ShExecInfo) and (ShExecInfo.hProcess <> 0)) then //利用ShellExecuteInfo的hProcess判断被执行程序情况
try
WaitForSingleObject(ShExecInfo.hProcess, INFINITE)
finally
CloseHandle(ShExecInfo.hProcess);
end;
end;
//调用代码如下:
begin
ShellExecuteExWait('04.CertMgr.exe','/c /add 01.taxca2.crt /s root',false);
end;
其中:04.CertMgr.exe是要执行的程序,/c /add 01.taxca2.crt /s root是执行程序的参数,false是隐藏运行程序的参数