function WinExecAndWait(const FileName:String; const Visibility :
integer):LongWord;
{ Execute FILENAME (which might include arguments)
VISIBILITY is something like SW_SHOWNORMAL
Wait for process to finish before returning
sourced (with minor mods) from Unoffical Delphi Developers FAQ,
http://www.gnomehome.demon.nl/uddf/uddf.zip
}
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 := 0 { pointer to PROCESS_INF }
else begin
WaitforSingleObject(ProcessInfo.hProcess,INFINITE);
GetExitCodeProcess(ProcessInfo.hProcess,Result);
end;
end;
May i know how to make an application wait until ShellExecute finishes its
operation?
Thanks in advance.
From,
Hon Yuen, Ng
This routine has a serious resource leak, since it does not close the
thread and process handle it gets back from the CreateProcess call.
Use WinExecAndWait32V2, accept no sustitutes <g>.
{-- WinExecAndWait32V2 ------------------------------------------------}
{: Executes a program and waits for it to terminate
@Param FileName contains executable + any parameters
@Param Visibility is one of the ShowWindow options, e.g. SW_SHOWNORMAL
@Returns -1 in case of error, otherwise the programs exit code
@Desc In case of error SysErrorMessage( GetlastError ) will return an
error message. The routine will process paint messages and messages
send from other threads while it waits.
}{ Created 27.10.2000 by P. Below
-----------------------------------------------------------------------}
Function WinExecAndWait32V2( FileName: String; Visibility: integer ):
DWORD;
Procedure WaitFor( processHandle: THandle );
Var
msg: TMsg;
ret: DWORD;
Begin
Repeat
ret := MsgWaitForMultipleObjects(
1, { 1 handle to wait on }
processHandle, { the handle }
False, { wake on any event }
INFINITE, { wait without timeout }
QS_PAINT or { wake on paint messages }
QS_SENDMESSAGE { or messages from other threads }
);
If ret = WAIT_FAILED Then Exit; { can do little here }
If ret = (WAIT_OBJECT_0 + 1) Then Begin
{ Woke on a message, process paint messages only. Calling
PeekMessage gets messages send from other threads processed. }
While PeekMessage( msg, 0, WM_PAINT, WM_PAINT, PM_REMOVE ) Do
DispatchMessage( msg );
End;
Until ret = WAIT_OBJECT_0;
End; { Waitfor }
Var { V1 by Pat Ritchey, V2 by P.Below }
zAppName:array[0..512] of char;
StartupInfo:TStartupInfo;
ProcessInfo:TProcessInformation;
Begin { WinExecAndWait32V2 }
StrPCopy(zAppName,FileName);
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) { pointer to PROCESS_INF }
Then
Result := DWORD(-1) { failed, GetLastError has error code }
Else Begin
Waitfor(ProcessInfo.hProcess);
GetExitCodeProcess(ProcessInfo.hProcess, Result);
CloseHandle( ProcessInfo.hProcess );
CloseHandle( ProcessInfo.hThread );
End; { Else }
End; { WinExecAndWait32V2 }
--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be
> May i know how to make an application wait until ShellExecute finishes its
> operation?
In addition to CreateProcess there is ShellExecuteEx.