***************************************************************** * Neveprise Inc. Delphi FAQ * ***************************************************************** * Section: Misc Topic: Exec app and wait * * Created: July 27th, 1999 Revised: July 27th, 1999 * * Updates: www.neveprise.de Info: support@neveprise.de * ***************************************************************** Q: How can I execute an external application and let my one wait until it is finished / detect finalization? A: Often this function is described as "ExecAndWait". Using the CreateProcess() function you can archieve this. This function creates a process (an app is a process having one or more threads), passing the process handle to an other function that queries its existence, we can reach our goal: function ExecAndWaitH(Filename, Parameter: string; nCmdShow: integer): longword; var zAppName: array[0..max_path] of char; zParameter: array[0..max_path] of char; zCurDir: array[0..max_path] of char; WorkDir: string; StartupInfo: TStartupInfo; ProcessInfo: TProcessInformation; begin StrPCopy(zAppName, FileName); StrPCopy(zParameter, Parameter); GetDir(0, WorkDir); StrPCopy(zCurDir, WorkDir); FillChar(StartupInfo, Sizeof(StartupInfo),#0); //init, remove trash StartupInfo.cb:=Sizeof(StartupInfo); StartupInfo.dwFlags:=StartF_UseShowWindow; //we want to set window flags StartupInfo.wShowWindow:=nCmdShow; //that is it (e.g. ws_ShowNormal) if not CreateProcess(zAppName, zParameter, nil, nil, false, Create_New_Console or Normal_Priority_Class, nil, nil, StartupInfo, ProcessInfo) then Result:=0 else begin WaitForSingleObject(ProcessInfo.hProcess, Infinite); GetExitCodeProcess(ProcessInfo.hProcess, Result); end; end; WaitForSingleobject() retrieves the Process' handle and only continues code's flow if the process has terminated, while GetExitCodeProcess() returns eventual error, 0 means all okay. To look up other parameters used un StartupInfo.wShowWindow please refer the Win32 SDK help delivered with Delphi. The flags are listed under the function ShowWindow(). A second thing of interest is the sixth parameter of CreateProcess(): there you can specify the priority of the application to be started. That means that you can "give" apps more computing cycles, but that should only be done if really nessessary (e.g. special timers etc.). To get the other params for these priority classes, refer CreateProcess() in the Win32 help. Blazko