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

How to wait for an ShellExecute to finish

3,182 views
Skip to first unread message

Gerrit Beuze

unread,
Aug 25, 2004, 2:47:25 PM8/25/04
to
Hi all,

I start an utility exe with ShellExecute(...) from my code
and I'd like to wait until that exe completed.
How can I do that?

Diggin in the win32 help, I found ShellExecuteEx that returns the
handle of the instance created. There's also WaitForSingleObject(handle, timeout)
But how can I combine these?
Should I look for the process associated with the instance? and how would that work?


Thanks in advance,

Gerrit Beuze


Avatar Zondertau

unread,
Aug 25, 2004, 2:52:52 PM8/25/04
to

A process handle becomes signaled when the process terminated.
WaitForSingleObject returns when the specified object becomes signaled
or the timeout elapses. If ShellExecuteEx executed (and indeed returned
a valid process handle - not an error) you can call

WaitForSingleObject(ReturnedHandle, INFINITE);

to wait until the process is terminated.

Gerrit Beuze

unread,
Aug 25, 2004, 3:00:02 PM8/25/04
to
Hi,

I understand the WaitForSigleObject bit, but how do I get the process handle?

Gerrit

"Avatar Zondertau" <avatarz...@hotmail.com> wrote in message news:412ce004$1...@newsgroups.borland.com...

Avatar Zondertau

unread,
Aug 25, 2004, 3:07:07 PM8/25/04
to
> I understand the WaitForSigleObject bit, but how do I get the process
> handle?

The hProcess member of the SHELLEXECUTEINFO structure passed to
ShellExecuteEx should contain it after the call if the proper flags are
set (set fMask to SEE_MASK_NOCLOSEPROCESS).

Gerrit Beuze

unread,
Aug 25, 2004, 4:30:35 PM8/25/04
to
Now I see,

Thanks,

Gerrit
"Avatar Zondertau" <avatarz...@hotmail.com> wrote in message news:412ce35b$1...@newsgroups.borland.com...

Francisco Alvarado

unread,
Aug 26, 2004, 1:15:07 AM8/26/04
to
// I found this in some forum
function WinExecAndWait32(FileName: string; Visibility: integer): integer;
{ returns -1 if the Exec failed, otherwise returns the process' exit
code when the process terminates }
var
zAppName: array[0..512] of char;
lpCommandLine: array[0..512] of char;
zCurDir: array[0..255] of char;
WorkDir: string;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
StrPCopy(zAppName, '');
StrPCopy(lpCommandLine, 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, { pointer to command line string }
lpCommandLine,
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);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;

Jens Geyer

unread,
Aug 26, 2004, 4:16:24 AM8/26/04
to
> Diggin in the win32 help, I found ShellExecuteEx that returns the
> handle of the instance created. There's also
> WaitForSingleObject(handle, timeout) But how can I combine these?

You are on the right track. The rest of the puzzle you'll find here:

http://groups.google.com/groups?&q=shellexecuteex+waitforsingleobject&meta=group%3Dborland.public.delphi.language.delphi.win32

(one line)

Another possibility is CreateProcess(), but ShellExecuteEx() is somewhat
simpler to use and ok in most cases.
JensG


0 new messages