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

DELPHI : WinExec or ShellExecute ?

1,558 views
Skip to first unread message

Charles

unread,
Jul 31, 1995, 3:00:00 AM7/31/95
to

How do you use WinExec or ShellExecute to run a program and cause the
calling program to wait until the child program finishes executing?

Does either WinExec or ShellExecute allow you to execute programs like
PROGNAME.XXX whose file extension has been changed from .EXE to .PRG (as
in TP Dos' Exec command)?

Thankx In Advance.

Martin Wolstencroft

unread,
Jul 31, 1995, 3:00:00 AM7/31/95
to
uni...@clark.net (Charles) wrote:

>How do you use WinExec or ShellExecute to run a program and cause the
>calling program to wait until the child program finishes executing?

You will need to set up a while loop that calls :-
1. GetModuleUsage (windows - takes a value returned from the exec or
shell)
and 2. Application.ProcessMessages. (delphi)

>Does either WinExec or ShellExecute allow you to execute programs like
>PROGNAME.XXX whose file extension has been changed from .EXE to .PRG (as
>in TP Dos' Exec command)?

Dunno

Sig 95 - Marketing Beta
Mail: martin.wo...@liffe.com


Tom Wheeley

unread,
Aug 1, 1995, 3:00:00 AM8/1/95
to
In article <3vi6aa$4...@clarknet.clark.net> uni...@clark.net "Charles" writes:

> Does either WinExec or ShellExecute allow you to execute programs like
> PROGNAME.XXX whose file extension has been changed from .EXE to .PRG (as
> in TP Dos' Exec command)?

The example you give requires WinExec, which assumes your program is an EXE.
ShellExecute will look at the extension, and see if it is registered to
another program, then run that. fe ShellExecuting a .WRI will load Write
with that file.

A common use of WinExec is to run ScreenSavers (EXEs with .SCR ext)

--
I've got more important things to do than a bloody .sig file, alright?!

Tom Wheeley

Abimbola A Olowofoyeku

unread,
Aug 1, 1995, 3:00:00 AM8/1/95
to
In article <3vi6aa$4...@clarknet.clark.net> (31 Jul 1995 09:04:10 GMT), Charles (uni...@clark.net) wrote:

: >How do you use WinExec or ShellExecute to run a program and cause the
: >calling program to wait until the child program finishes executing?


{///////////// a PeekMessage() loop /////////}
procedure Pause;
var
Msg: TMsg;

Begin
While PeekMessage (Msg, 0, 0, 0, pm_Remove) do
begin
if Msg.Message = wm_Quit then
begin
PostQuitMessage(msg.wParam);
Exit;
end{Msg.Message};

if not IsDialogMessage (0, Msg) then
with Msg do
begin
TranslateMessage (Msg);
DispatchMessage (Msg);
end{With Msg do};
end {While PeekMessage};

End {Pause};

{///////////////////////////////////////////}
Function WinExecWait (PName: PChar; ShowCmd : Integer) : THandle;
Var
InstanceID : THandle;

Begin
InstanceID := WinExec (PName, ShowCmd); {call WinExec() }
WinExecWait := InstanceID; {return InstanceID }
if InstanceID <= 32 then Exit; {some error }

Repeat
Pause; {keep waiting }
Until GetModuleUsage (InstanceID) = 0; {until this instance ends }
End { WinExecWait() };
{/////////////////////////////////}

: >Does either WinExec or ShellExecute allow you to execute programs like

: >PROGNAME.XXX whose file extension has been changed from .EXE to .PRG (as
: >in TP Dos' Exec command)?

With WinExec you can rename a .EXE to anything and run it.

The Chief
---------
Dr. Abimbola A. Olowofoyeku (The African Chief)
E-mail : la...@keele.ac.uk
Author of: Chief's Installer Pro 1.70 for Windows:
winner of PC PLUS Magazine Gold Award (April 1995 U.K. edition)
ftp://ftp.demon.co.uk/pub/ibmpc/windows/chief/pro/cinstp17.zip

Charles

unread,
Aug 2, 1995, 3:00:00 AM8/2/95
to
Thankx everyone for the assistance with WinExec. It seems that all of
the reponses I've received so far have worked. It's a shame that this
information was not readily available in the online help. But, I guess,
Borland can't do everything for us (actually, I didn't see much of it in
the VC++ online help either).

Again, thankx for the help.

Andrew R. Rolfe

unread,
Aug 2, 1995, 3:00:00 AM8/2/95
to
aao...@huxley.anu.edu.au (Abimbola A Olowofoyeku) wrote:
>{///////////////////////////////////////////}
>Function WinExecWait (PName: PChar; ShowCmd : Integer) : THandle;
>Var
> InstanceID : THandle;
>
>Begin
> InstanceID := WinExec (PName, ShowCmd); {call WinExec() }
> WinExecWait := InstanceID; {return InstanceID }
> if InstanceID <= 32 then Exit; {some error }
>
> Repeat
> Pause; {keep waiting }
> Until GetModuleUsage (InstanceID) = 0; {until this instance ends }
>End { WinExecWait() };
>{/////////////////////////////////}

Be aware this will not always work since AppInstance handles are reused
and therefore you have a window of vulnerability here. Granted in such a
tight loop as above the window is small. But you may also like to let
the app continue to do other things while waiting.

I have found it more fool-proof to test for a combination of
instance handle, window handle and task handle. The problem is there
is no easy way to get the other handles from the instance.
You have to iterate thru extra window memory to find the main window
for the instance. Use WinAPI EnumWindows() to do this. The added
benefit is, once you have the window handle, you can do lots of things;
like do a Show to return to the launched app if the user attempts to
execute it again.

Andy--
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
"You are no more or less than what you know, you remember, and you
create."
Andy Rolfe - aro...@mcs.com | 76244...@compuserve.com


Stephen Yost

unread,
Aug 4, 1995, 3:00:00 AM8/4/95
to
In article <3vi6aa$4...@clarknet.clark.net>, Charles <uni...@clark.net> wrote:
>
>How do you use WinExec or ShellExecute to run a program and cause the
>calling program to wait until the child program finishes executing?
>
>Does either WinExec or ShellExecute allow you to execute programs like
>PROGNAME.XXX whose file extension has been changed from .EXE to .PRG (as
>in TP Dos' Exec command)?
>

I don't believe you can cause the parent program to wait for the child
using WinExec. However, there is an article (with some sample code
I think) in the Microsoft Knowledge Base) that explains how it can be
done. As I recall it's a little hairy but gets the job done.

===
Steve Yost
sy...@atria.com

Bruce Silberman

unread,
Aug 10, 1995, 3:00:00 AM8/10/95
to


If the child program creates a window, you can use its handle to check
if it's still valid.

Try something like this:

while (iswindow(Handle_of_Child)) do
Application.ProcessMessages;


{IsWindow is a Windows API function}
{Application.ProcessMessages will prevent locking up other programs}

Hope this helps...

bsi...@infi.net

0 new messages