我们需要写一个启动外部程序并等待它结束的函数,查了一下发现,有两个Win32 API函数可以利用:ShellExecuteEx 或者CreateProcess。
function ExecAppWait(AppName, Params: string): Boolean;
var
ShellExInfo: TShellExecuteInfo;
begin
//FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
with ShellExInfo do begin
cbSize := SizeOf(ShellExInfo);
fMask := see_Mask_NoCloseProcess;
Wnd := Forms.Application.Handle;
lpFile := PChar(AppName);
lpParameters := PChar(Params);
nShow := sw_ShowNormal;
end;
Result := ShellExecuteEx(@ShellExInfo);
if Result then
WaitForSingleObject(ShellExInfo.HProcess, INFINITE);
end;
Function WinExecEx(cmd:string) word;
var
StartupInfo:Borland.Delphi.Windows.TStartupInfo;
ProcessInfo:Borland.Delphi.Windows.TProcessInformation;
i:integer;
begin
//FillChar(StartupInfo, SizeOf(StartupInfo), 0);
StartupInfo.cb:=SizeOf(StartupInfo);
StartupInfo.dwFlags:=STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow:=SW_SHOWNORMAL;
if not Borland.Delphi.Windows.CreateProcess('',cmd,nil,nil,False,Create_new_console
or Normal_priority_class,nil,'', StartupInfo, ProcessInfo) then
begin
Result:=0;
exit;
end;
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
Result:=ProcessInfo.dwProcessId;
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
现在写了两个函数,第一个有指针参数不能编译,第二个可以编译但始终不能运行程序
请问还有没有其它方法可以实现这个函数 |