Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Wait for ShellExecute

116 views
Skip to first unread message

Dave de Buys

unread,
Jun 26, 2003, 3:35:06 AM6/26/03
to
i use the following coe i got from Unoffical Delphi Developers FAQ


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;

Hon Yuen, Ng

unread,
Jun 26, 2003, 3:17:11 AM6/26/03
to
hi

May i know how to make an application wait until ShellExecute finishes its
operation?
Thanks in advance.

From,
Hon Yuen, Ng


Peter Below (TeamB)

unread,
Jun 26, 2003, 5:51:28 AM6/26/03
to
In article <3efa...@newsgroups.borland.com>, Dave de Buys wrote:
> i use the following coe i got from Unoffical Delphi Developers FAQ
>
>
> function WinExecAndWait(const FileName:String; const Visibility :

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


Dave de Buys

unread,
Jun 26, 2003, 6:09:21 AM6/26/03
to
ooops, well spotted peter, i will update my snippet library as such, thanks

Bruce Roberts

unread,
Jun 26, 2003, 4:14:45 PM6/26/03
to

"Hon Yuen, Ng" <hy...@tm.net.my> wrote in message
news:3efa9e1b$1...@newsgroups.borland.com...

> May i know how to make an application wait until ShellExecute finishes its
> operation?

In addition to CreateProcess there is ShellExecuteEx.


0 new messages