Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Delphi with Other Programs

How do I execute another program from my Delphi application 2 by sggaunt
Posted: 31 Aug 05

This function encapslates the API Functions CreateProcess and WaitForSingleObject, createprocess effectivly starts the new process (program) in a new thread so the calling program can continue to run concurrently.
The timeout value can be used to force the process to terminate.


{ Useage: Filename, The file to be executed
          Params, Any parameters that require passing to the file
          Timeout, Time to wait in mS. Pass the constant INFINITE to wait forever
          Visibility, Run window status e.g SW_SHOW, SW_MAXIMIZE
          It returns a string containing the last error if any
function WaitProcess(Filename: TFilename; Params: string;
         Timeout: DWORD; Visibility: integer): string;
var StartupInfo: TStartupInfo;
    ProcessInfo: TProcessInformation;
    WaitResult: integer;
   ZeroMemory(@StartupInfo, SizeOf(TStartupInfo));
    with StartupInfo do
         cb := SizeOf(TStartupInfo);
         wShowWindow := Visibility;
    if fileexists(filename) or fileexists(params) then
         if (CreateProcess(nil, Pchar(Filename +' '+ Params), Nil, Nil,
                     False, NORMAL_PRIORITY_CLASS,
                     Nil, nil { pchar(extractfilepath(Params))}, StartupInfo, ProcessInfo))
                    WaitResult := WaitForSingleObject(ProcessInfo.hProcess, Timeout);
                    if WaitResult = WAIT_TIMEOUT then
                          result := 'Timeout has occured in waiting for result from compiler.';
                          TerminateProcess(ProcessInfo.hprocess, 1);
                    Result := 'OK';
                result := 'Create Proccess failed ' + filename + ' '+ Params+ ' Error: '+inttostr(getlasterror);
      result := 'Process file does not exist';

Back to Embarcadero: Delphi FAQ Index
Back to Embarcadero: Delphi Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close