想起来简单,做起来难!?大家来讨论讨论 (50分)

  • 主题发起人 desertsmoke
  • 开始时间
D

desertsmoke

Unregistered / Unconfirmed
GUEST, unregistred user!
两个可执行文件exe1,exe2,由于特殊原因需要在exe1中调用exe2来完成一部分相关的操作,
问题就是如何在exe2结束前让exe1保留在原地不动,等exe2执行完毕后在接着执行exe1?
 

运行一个程序并等待它终止
function WinExecAndWait32(
FileName:String;
Visibility :integer):integer;
var
zAppName:array[0..512] of char;
zCurDir:array[0..255] of char;
WorkDir:String;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
begin
StrPCopy(zAppName,FileName);
GetDir(0,WorkDir);
StrPCopy(zCurDir,WorkDir);
FillChar(StartupInfo,Sizeof(StartupInfo),#0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := Visibility;
if not CreateProcess(nil,zAppName,
{ pointer to command line string }
nil, { pointer to process security attributes }
nil, { pointer to thread security attributes }
false, { handle inheritance flag }
CREATE_NEW_CONSOLE or { creation flags }
NORMAL_PRIORITY_CLASS,
nil, { pointer to new environment block }
nil, { pointer to current directory name }
StartupInfo, { pointer to STARTUPINFO }
ProcessInfo) then
Result := -1
{ pointer to PROCESS_INF }
else
begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;

 
启动外部程序并等待它结束的函数
我们可以通过进程句柄(process handle)来查看进程(程序)是否结束。为了得到进程句柄,有两个Win32 API函数可以利用:ShellExecuteEx 或者CreateProces。解决这个问题最简单的方法是,使用ShellExecuteEx启动一个外部程序,然后使用WaitForSingleObject管理这个外部程序的进程句柄。我们可以这样定义一个函数:
{ ExecAppWait:功能:运行外部程序并等待它结束。。
运行外部程序APPNAME,参数PARAMS;
Returns:如果外部程序出错返回 FASLE }
function ExecAppWait(AppName, Params: string): Boolean ;
……
function ExecAppWait(AppName, Params: string): Boolean;
var
// Structure containing and receiving info about application to start
ShellExInfo: TShellExecuteInfo;
begin
FillChar(ShellExInfo, SizeOf(ShellExInfo), 0);
with ShellExInfo do
begin
cbSize := SizeOf(ShellExInfo);
fMask := see_Mask_NoCloseProcess;
Wnd := Application.Handle;
lpFile := PChar(AppName);
lpParameters := PChar(Params);
nShow := sw_ShowNormal;
end;
Result := ShellExecuteEx(@ShellExInfo);
if Result then
while WaitForSingleObject(ShellExInfo.HProcess, 100) = WAIT_TIMEOUT do
begin
Application.ProcessMessages;
if Application.Terminated then
Break;
end;
end;
……
不难理解吧?
 
大家接着说,我在做测试
 
dhycq的方法可以,就结束吧!谢谢二位
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
896
DelphiTeacher的专栏
D
D
回复
0
查看
803
DelphiTeacher的专栏
D
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
顶部