实现执行外部程序,并等待程序结束的函数。
作者:陆岛工作室
![](https://www.cnblogs.com/images/cnblogs_com/iinsnian/139915/t_xoteclogo2.gif)
{ ExecuteFileWait }
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
function ExecuteFileWait(Handle: HWND; const FileName, Params, StartDir: string; InitialState: TExecState): Integer;
const
ShowCommands: array[TExecState] of LongInt = (SW_SHOWNORMAL, SW_MINIMIZE, SW_SHOWMAXIMIZED, SW_HIDE);
var
Info: TShellExecuteInfo;
ExitCode: DWORD;
FActive: HWND;
begin
FillChar(Info, SizeOf(Info), 0);
Info.cbSize := SizeOf(TShellExecuteInfo);
with Info do
begin
fMask := SEE_MASK_NOCLOSEPROCESS;
Wnd := Application.MainForm.Handle;
lpFile := PChar(FileName);
lpParameters := PChar(Params);
lpDirectory := PChar(StartDir);
nShow := ShowCommands[InitialState];
end;
FActive := GetActiveWindow;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
if ShellExecuteEx(@Info) then
begin
EnableWindow(Handle, False);
repeat
Application.ProcessMessages;
GetExitCodeProcess(Info.hProcess, ExitCode);
until (ExitCode <> STILL_ACTIVE) or Application.Terminated;
EnableWindow(Handle, True);
ShowWindow(Handle, SW_SHOW);
SetActiveWindow(FActive);
SetForegroundWindow(FActive);
Result := ExitCode;
end
else Result := -1;
end;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
begin
ExecuteFileWait(Application.Handle, 'C:\Windows\NotePad.EXE', 'C:\A.txt', '', esMaximized);
ShowMessage('OK! 程序已退出!');
end;
![](https://www.cnblogs.com/images/cnblogs_com/iinsnian/139915/t_xoteclogo2.gif)
type
TExecState = (esNormal, esMinimized, esMaximized, esHidden);
function ExecuteFileWait(Handle: HWND; const FileName, Params, StartDir: string; InitialState: TExecState): Integer;
{
Handle: 传入当前窗口或 Applicaton 的句柄
FileName: 需要传入的文件名
Params: 执行文件所带的参数
StartDir: 执行文件启动路径
InitialState: 窗口显示模式
}
实现代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
示范:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
作者: 陆岛工作室