DelphiFAQ Home Search:

Execute and wait for termination (16 and 32bit applications)

 

comments2 comments. Current rating: 4 stars (1 votes). Leave comments and/ or rate it.

This unit is based upon the well-known and largely used WinExecAndWait function The former WinexecAndWait function doesn't compile under Delphi 2.0 because the GetModuleUsage function is no longer supported under Win95.

I have simply updated the previous code so that it works with Delphi 2.0 under Windows 95. With this function you can call Windows-based applications as well as Dos-based commands. That is 'c:\myapp\app32.exe' as well as command.com /c del *.bak'.

This new WinexecAndWait32 is intended for Delphi 2.0 Win95 only, it works for me but you use it at your own risk:

unit WinExc32;
// Author    : Francis Parlant.
// Update    : Bill Rinko-Gay
// Parameters:
//    Path: a null terminated string with the command line
//    Visibility: Windows SHOW constant such as WS_RESTORE
//    Timeout: DWORD time in miliseconds or INFINITE to wait forever 
//
// The previous version went into a loop, constantly checking to see
// if the exec'ed program had terminated.  This version uses the Win95
// API call to WaitForSingleObject to block processing until the
// exec'ed program has terminated.  The return value will be either
// an error code from the CreateProcess function, or the result
// of the wait.

interface

uses Windows;

function WinExecAndWait32(Path: PChar; Visibility: Word;
  Timeout : DWORD): integer;

implementation

function WinExecAndWait32(Path: PChar; Visibility: Word;
  Timeout : DWORD): integer;
var
  WaitResult : integer;
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  iResult : integer;
begin
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  with StartupInfo do
  begin
    cb := SizeOf(TStartupInfo);
    dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
	{ you could pass sw_show or sw_hide as parameter: }
    wShowWindow := visibility;
  end;
  if CreateProcess(nil,path,nil, nil, False,
		NORMAL_PRIORITY_CLASS, nil, nil,
		StartupInfo, ProcessInfo) then
  begin
    WaitResult := WaitForSingleObject(ProcessInfo.hProcess, timeout);
    { timeout is in miliseconds or INFINITE if
	  you want to wait forever }
    result := WaitResult;
  end
  else
  { error occurs during CreateProcess see help for details }
    result:=GetLastError;
end;

end.

Comments:

2006-06-26, 01:13:56
anonymous from Argentina  
You must also call CloseHandle on the Process and Thread handles returned on ProcessInfo structure.
2008-05-28, 06:25:07   (updated: 2008-05-28, 06:35:17)
[hidden]  
rating
I found the function to be not waiting at all for the current command to finish execution so I added the following, which makes it seem to be working like I expected.

Below 'WaitResult := WaitForSingleObject(ProcessInfo.hProcess, timeout);' add the following
while (WaitResult <> 0) do
begin
Sleep(500);
WaitResult := WaitForSingleObject(ProcessInfo.hProcess, timeout);
end;


Please do let me know whether my logic could result in a major disaster!


A bit later I found this to be only the case when I pass in a Timeout value of zero.

Thanks

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Los Angeles, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.