Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to run exe and get its handle

Status
Not open for further replies.

Spent

Programmer
Mar 20, 2003
100
BG
Can you tell me how to execute *.exe file and get its Handle. Thanks.

 
Code:
function Run(EXEName : string; Arguments : TStrings; HideEXE : Boolean) : THandle;
var
   RunInfo : TProcessInformation;
   StartupInfo : TStartupInfo;
   ACommandLine : string;
   Counter : Integer;
begin
   Result := 0;
   Screen.Cursor := crHourglass;
   try
       ACommandLine := '"' + EXEName + '"';
       if Arguments <> nil then
       begin
           for Counter := 0 to Arguments.Count - 1 do
               ACommandLine := ACommandLine + ' &quot;' + Arguments[Counter] + '&quot;';
       end;

       with StartupInfo do
       begin
           cb := SizeOf(StartupInfo);
           lpReserved := nil;
           dwFlags := STARTF_USESHOWWINDOW or STARTF_FORCEONFEEDBACK;
           if HideEXE then
               wShowWindow := SW_HIDE
           else
               wShowWindow := SW_SHOW;
           lpDesktop := nil;
           lpTitle := nil;
           cbReserved2 := 0;
           lpreserved2 := nil;
       end;

       if Windows.CreateProcess(nil,                                                   //      Application name
           PChar(ACommandLine),                                                //      Command line
           nil,                                                                //      Process security attributes
           nil,                                                                //      Thread security attributes
           False,                                                              //      Do not inherited file handles
           CREATE_NEW_CONSOLE,                                                 //      Creation flags (default priority)
           nil,                                                                //      Share environment settings
           nil,                                                                //      Startup directory
           StartupInfo,                                                        //      No Startup window structure
           RunInfo) then                                                       //      Output information
       begin
           Result := RunInfo.hProcess;
           Windows.CloseHandle(RunInfo.hThread);           //  we don't return it; no one can use it
           Application.ProcessMessages;
       end
       else
           raise Exception.Create('Error creating process ' + EXEName + '. Error code is ' + IntToStr(GetLastError));
   finally
       Screen.Cursor := crDefault;
   end;
end;

function GetRunExitCode(EXEName : string; TimeOutSeconds : Integer; Arguments : TStrings; HideEXE : Boolean) : DWord;
var
   ProcessHandle : THandle;
   ExpireTime : TDateTime;
begin
   ProcessHandle := Run(EXEName, Arguments, HideEXE);

   if ProcessHandle > 32 then
   try
       Result := STILL_ACTIVE;
       ExpireTime := Now + (TimeoutSeconds / (24 * 60 * 60));
       while (Result = STILL_ACTIVE) do
       begin
           Sleep(500);
           if (ExpireTime < Now)  then
               raise Exception.Create('Timed-out waiting for ' + EXEName + ' to finish');
           Windows.GetExitCodeProcess(ProcessHandle, Result);
       end;
   finally
       Windows.CloseHandle(ProcessHandle);
   end;
end;

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top