Winexec调用EXE文件的问题(50分)

  • 主题发起人 主题发起人 刘忠平
  • 开始时间 开始时间

刘忠平

Unregistered / Unconfirmed
GUEST, unregistred user!
Winexec(Pchar(ExtractFilePath(Application.ExeName)+'EXE文件名',9)后如何知道它执行<br>是否结束
 
好像没有办法知道
 
winexec只返回是否运行成功。<br>所以最后用其他办法得到application.exename 的句柄
 
我也想知道呀, 我還想在外部程式結束后自動關閉!<br>大俠在哪里?
 
function WinExecAndWait32(FileName: string; Visibility: Integer): Integer;<br>var<br>&nbsp; zAppName: array[0..512] of char;<br>&nbsp; zCurDir: array[0..255] of char;<br>&nbsp; WorkDir: string;<br>&nbsp; StartupInfo: TStartupInfo;<br>&nbsp; ProcessInfo: TProcessInformation;<br>begin<br>&nbsp; StrPCopy(zAppName, FileName);<br>&nbsp; GetDir(0, WorkDir);<br>&nbsp; StrPCopy(zCurDir, WorkDir);<br>&nbsp; FillChar(StartupInfo, Sizeof(StartupInfo), #0);<br>&nbsp; StartupInfo.cb := Sizeof(StartupInfo);<br><br>&nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; StartupInfo.wShowWindow := Visibility;<br>&nbsp; if not CreateProcess(nil,<br>&nbsp; &nbsp; zAppName, { pointer to command line string }<br>&nbsp; &nbsp; nil, { pointer to process security attributes }<br>&nbsp; &nbsp; nil, { pointer to thread security attributes }<br>&nbsp; &nbsp; false, { handle inheritance flag }<br>&nbsp; &nbsp; CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, { creation flags }<br>&nbsp; &nbsp; nil, { pointer to new environment block }<br>&nbsp; &nbsp; nil, { pointer to current directory name }<br>&nbsp; &nbsp; StartupInfo, { pointer to STARTUPINFO }<br>&nbsp; &nbsp; ProcessInfo) then { pointer to PROCESS_INF }<br>&nbsp; begin<br>&nbsp; &nbsp; Result := -1<br>&nbsp; end<br>&nbsp; else<br>&nbsp; begin<br>&nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess, INFINITE);<br>&nbsp; &nbsp; GetExitCodeProcess(ProcessInfo.hProcess, Cardinal(Result));<br>&nbsp; end;<br>end;<br>
 
var h:HWND;<br>begin<br>&nbsp;h:= Winexec(Pchar(ExtractFilePath(Application.ExeName)+'EXE文件名',9);<br>&nbsp;if h&lt;=32 then &nbsp; //如果调用失败<br>&nbsp; &nbsp; showmessage('调用失败');<br>end;<br>
 
同意delphiwolf,不用winexec,而用createprocess和waitforsigleobject<br><br>在昨天的另一个贴子中,我提供了c语言版的。
 
rxlib 早就有了。 <br><br>function FileExecuteWait(const FileName, Params, StartDir: string;<br>&nbsp; InitialState: TExecState): Integer;<br>{$IFDEF WIN32}<br>var<br>&nbsp; Info: TShellExecuteInfo;<br>&nbsp; ExitCode: DWORD;<br>begin<br>&nbsp; FillChar(Info, SizeOf(Info), 0);<br>&nbsp; Info.cbSize := SizeOf(TShellExecuteInfo);<br>&nbsp; with Info do begin<br>&nbsp; &nbsp; fMask := SEE_MASK_NOCLOSEPROCESS;<br>&nbsp; &nbsp; Wnd := Application.Handle;<br>&nbsp; &nbsp; lpFile := PChar(FileName);<br>&nbsp; &nbsp; lpParameters := PChar(Params);<br>&nbsp; &nbsp; lpDirectory := PChar(StartDir);<br>&nbsp; &nbsp; nShow := ShowCommands[InitialState];<br>&nbsp; end;<br>&nbsp; if ShellExecuteEx(@Info) then begin<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; GetExitCodeProcess(Info.hProcess, ExitCode);<br>&nbsp; &nbsp; until (ExitCode &lt;&gt; STILL_ACTIVE) or Application.Terminated;<br>&nbsp; &nbsp; Result := ExitCode;<br>&nbsp; end<br>&nbsp; else Result := -1;<br>end;<br>{$ELSE}<br>var<br>&nbsp; Task: THandle;<br>begin<br>&nbsp; Result := 0;<br>&nbsp; Task := FileExecute(FileName, Params, StartDir, InitialState);<br>&nbsp; if Task &gt;= HINSTANCE_ERROR then begin<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp; Application.ProcessMessages;<br>&nbsp; &nbsp; until (GetModuleUsage(Task) = 0) or Application.Terminated;<br>&nbsp; end<br>&nbsp; else Result := -1;<br>end;<br>{$ENDIF}<br>
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
908
SUNSTONE的Delphi笔记
S
S
回复
0
查看
885
SUNSTONE的Delphi笔记
S
后退
顶部