函数一:
1 uses
2 Windows,
3 SysUtils,
4 Classes,
5 ShellAPI;
6 function RunAndWait(FileName: string; Visibility: Integer): THandle;
7 var
8 zAppName: array[0..512] of Char;
9 zCurDir: array[0..255] of Char;
10 WorkDir: string;
11 StartupInfo: TStartupInfo;
12 ProcessInfo: TProcessInformation;
13 begin
14 try
15 StrPCopy(zAppName, FileName);
16 GetDir(0, WorkDir);
17 StrPCopy(zCurDir, WorkDir);
18 FillChar(StartupInfo, SizeOf(StartupInfo), #0);
19 StartupInfo.cb := SizeOf(StartupInfo);
20 StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
21 StartupInfo.wShowWindow := Visibility;
22 if not CreateProcess(nil, zAppName, nil, nil, false, Create_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
23 begin
24 result := 0;
25 Exit;
26 end
27 else
28 begin
29 WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
30 GetExitCodeProcess(ProcessInfo.hProcess, result);
31 end;
32 finally
33 end;
34 end;
2 Windows,
3 SysUtils,
4 Classes,
5 ShellAPI;
6 function RunAndWait(FileName: string; Visibility: Integer): THandle;
7 var
8 zAppName: array[0..512] of Char;
9 zCurDir: array[0..255] of Char;
10 WorkDir: string;
11 StartupInfo: TStartupInfo;
12 ProcessInfo: TProcessInformation;
13 begin
14 try
15 StrPCopy(zAppName, FileName);
16 GetDir(0, WorkDir);
17 StrPCopy(zCurDir, WorkDir);
18 FillChar(StartupInfo, SizeOf(StartupInfo), #0);
19 StartupInfo.cb := SizeOf(StartupInfo);
20 StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
21 StartupInfo.wShowWindow := Visibility;
22 if not CreateProcess(nil, zAppName, nil, nil, false, Create_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
23 begin
24 result := 0;
25 Exit;
26 end
27 else
28 begin
29 WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
30 GetExitCodeProcess(ProcessInfo.hProcess, result);
31 end;
32 finally
33 end;
34 end;
函数二:
1 function WinExecAndWait32(FileName: string; Visibility: Boolean): integer;
2 var
3 zAppName: array[0..512] of char; //存放应用程序名
4 StartupInfo: TStartupInfo;
5 ProcessInfo: TProcessInformation;
6 exitCode: Dword;
7 aVisibility: integer;
8 begin
9 StrPCopy(zAppName, FileName);
10 FillChar(StartupInfo, Sizeof(StartupInfo), #0);
11 //给StartupInfo结构体赋值
12 StartupInfo.cb := Sizeof(StartupInfo);
13 StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
14 if Visibility then
15 aVisibility := 1
16 else
17 aVisibility := 0;
18
19 StartupInfo.wShowWindow := aVisibility;
20 //调用CreateProcess 创建进程,执行指定的可执行文件
21 if not CreateProcess(nil, zAppName, nil, nil, false
22 , CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS
23 , nil, nil, StartupInfo, ProcessInfo) then
24 Result := -1
25 else
26 begin
27 //等待可执行文件退出
28 WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
29 //得到进程终止状态码
30 GetExitCodeProcess(ProcessInfo.hProcess, exitCode);
31 result := Exitcode;
32 end;
33 end;
2 var
3 zAppName: array[0..512] of char; //存放应用程序名
4 StartupInfo: TStartupInfo;
5 ProcessInfo: TProcessInformation;
6 exitCode: Dword;
7 aVisibility: integer;
8 begin
9 StrPCopy(zAppName, FileName);
10 FillChar(StartupInfo, Sizeof(StartupInfo), #0);
11 //给StartupInfo结构体赋值
12 StartupInfo.cb := Sizeof(StartupInfo);
13 StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
14 if Visibility then
15 aVisibility := 1
16 else
17 aVisibility := 0;
18
19 StartupInfo.wShowWindow := aVisibility;
20 //调用CreateProcess 创建进程,执行指定的可执行文件
21 if not CreateProcess(nil, zAppName, nil, nil, false
22 , CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS
23 , nil, nil, StartupInfo, ProcessInfo) then
24 Result := -1
25 else
26 begin
27 //等待可执行文件退出
28 WaitforSingleObject(ProcessInfo.hProcess, INFINITE);
29 //得到进程终止状态码
30 GetExitCodeProcess(ProcessInfo.hProcess, exitCode);
31 result := Exitcode;
32 end;
33 end;
其中 参数 Visibility 取值如下:
行号 参数 含义
1 SW_HIDE 隐藏这个窗体,并激活其他窗体。
2 SW_MAXIMIZE 最大化指定的窗体。
3 SW_MINIMIZE 最小化指定的窗体,并按顺序激活最上层的窗体。
4 SW_RESTORE 激活并显示窗体。如果窗体为最小化或者最大化,窗体恢复到原始大
小和置。应用程序当恢复一个最小化的窗体时将指定标记。
5 SW_SHOW 以当前的大小和位置激活并显示窗体。
6 SW_SHOWDEFAULT
7 SW_SHOWMAXIMIZED 激活并最大化显示窗体。
8 SW_SHOWMINIMIZED 激活并最小化现实窗体。
9 SW_SHOWMINNOACTIVE 最小化窗体,保持其激活状态。
10 SW_SHOWNA 以当前状态显示窗体,保持其激活状态。
11 SW_SHOWNOACTIVATE 以当前的大小和位置显示窗体,并保持其激活状态。
12 SW_SHOWNORMAL 激活并显示一个窗体。如果窗体为最大化或者最小化,窗体恢复到原始的大小和位置。当窗体第一次显示的时候,应用程序记录标记。