Board index » delphi » howto winexec a program and wait for return

howto winexec a program and wait for return

Use CreateProcess and WaitForSingleObject. Both are documented in the Win32
API help.
 

Re:howto winexec a program and wait for return


I want to run a program then know when it's finished so I can run another
process.

I found some code on delphi3000.com but I can't make it work.

function WinExecAndWait(Path: PChar; Visibility: Word): Word;
var
  InstanceID: THandle;
  Msg: TMsg;
begin
  InstanceID := WinExec(Path, Visibility);
  if InstanceID < 32 then { a value less than 32 indicates an Exec
                            error }
    WinExecAndWait := InstanceID
  else
    repeat
       while PeekMessage(Msg, 0, 0, 0, pm_Remove) do
       begin
         if Msg.Message = wm_Quit then Halt(Msg.WParam);
         TranslateMessage(Msg);
         DispatchMessage(Msg);
       end;
    until GetModuleUsage(InstanceID) = 0;   // This line doesn't work ???
any idea's
  WinExecAndWait := 0;
end;

Thanks

Aaron.

Re:howto winexec a program and wait for return


You shouldn't use WinExec on Win32 ! Instead use:

function RunAndWait(const FileName: String): DWORD;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
begin
  Result := STILL_ACTIVE;
  GetStartupInfo(StartupInfo);
  if CreateProcess(nil, PChar(FileName), nil, nil, IsConsole,
    NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo) then
  try
    if WaitForSingleObject(ProcessInfo.hProcess, INFINITE) = WAIT_OBJECT_0
then
      GetExitCodeProcess(ProcessInfo.hProcess, Result);
  finally
    CloseHandle(ProcessInfo.hThread);
    CloseHandle(ProcessInfo.hProcess);
  end;
end;

"QBD The Bookshop" <supp...@qbdthebookshop.com.au> schreef in bericht
news:Tk3h5.17$SN.2549@nsw.nnrp.telstra.net...

Quote
> I want to run a program then know when it's finished so I can run another
> process.

> I found some code on delphi3000.com but I can't make it work.

> function WinExecAndWait(Path: PChar; Visibility: Word): Word;
> var
>   InstanceID: THandle;
>   Msg: TMsg;
> begin
>   InstanceID := WinExec(Path, Visibility);
>   if InstanceID < 32 then { a value less than 32 indicates an Exec
>                             error }
>     WinExecAndWait := InstanceID
>   else
>     repeat
>        while PeekMessage(Msg, 0, 0, 0, pm_Remove) do
>        begin
>          if Msg.Message = wm_Quit then Halt(Msg.WParam);
>          TranslateMessage(Msg);
>          DispatchMessage(Msg);
>        end;
>     until GetModuleUsage(InstanceID) = 0;   // This line doesn't work ???
> any idea's
>   WinExecAndWait := 0;
> end;

> Thanks

> Aaron.

Re:howto winexec a program and wait for return


In article <Tk3h5.17$SN.2...@nsw.nnrp.telstra.net>,
supp...@qbdthebookshop.com.au says...
Quote
> I want to run a program then know when it's finished so I can run another
> process.

Save the text at the end as pdthdu.pas. To use it, do something like
this:

IF CreateProcess(nil, PChar(TheCommand), NIL, NIL, False,
    DETACHED_PROCESS OR NORMAL_PRIORITY_CLASS, NIL, PChar(TheDir),
    TSI, TPI) THEN
  BEGIN
    CloseHandle(TPI.hThread);
    TProcDoneThread.Create(TPI.hProcess, WaitThdDone);
  END
ELSE ShowMessage('Failed to execute');

Here WaitThdDone is a simple TNotifyEvent method of your form that gets
called when the thread completes. The other suggestions, to have your
main program block on WaitForSingleObject, will turn your program into
dead meat until the other program terminates...

- - - - - - - -
unit pdthdu;
interface
uses Windows, Classes;
type
  TProcDoneThread = class(TThread)
    ProcHandle : THandle;
    Constructor Create(vProcHandle: THandle; OnTerm: TNotifyEvent);
    procedure Execute; override;
  end;

implementation

constructor TProcDoneThread.Create(vProcHandle: THandle;
  OnTerm: TNotifyEvent);
begin
  Inherited Create(False);
  ProcHandle      := vProcHandle;
  OnTerminate     := OnTerm;
  FreeOnTerminate := True;
end;

procedure TProcDoneThread.execute;
begin
  WaitForSingleObject(ProcHandle, INFINITE);
  CloseHandle(ProcHandle);
end;

end.
- - - - - - - -
--
Neil J. Rubenking
Contributing Technical Editor
PC Magazine

Other Threads