News
Photos
Articles
Components
Applications
Kleinkunst

Delphi - Starting external applications

Sometimes you need to start an external application from your own application. In the past this could be done with the WinAPI function WinExec. Nowadays it is better to use the ShellExecute (unit ShellAPI) function. This function can also be used to open an URL, to create an email and to open a file with the application which is linked in the Windows Explorer.

Syntax in the Win32 SDK help :

HINSTANCE ShellExecute(
  HWND hwnd,            // handle to parent window
  LPCTSTR lpOperation,  // pointer to string that specifies operation to perform
  LPCTSTR lpFile,       // pointer to filename or folder name string
  LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters 
  LPCTSTR lpDirectory,  // pointer to string that specifies default directory
  INT nShowCmd          // whether file is shown when opened
);

hwnd is the handle to your form or application. The parameter lpOperation is a string which can have 3 possible values; “open”; “print” and “explore”. Some constants can be used for the last parameter nShowCmd. The most interesting values are :

  • SW_SHOW Activates the window and displays it in its current size and position.
  • SW_SHOWMAXIMIZED Activates the window and displays it as a maximized window.
  • SW_SHOWMINIMIZED Activates the window and displays it as a minimized window.


Some examples

Start an application

ShellExecute(Handle, 'open', 'c:\AnApplication.exe', nil, nil, SW_SHOW);

Start an application with an extra parameter

ShellExecute(Handle, 'open', 'c:\AnApplication.exe', 'C:\ATextFile.txt', nil, SW_SHOW);

Open or print a file

ShellExecute(Handle, 'open', 'c:\ABitmap.bmp', nil, nil, SW_SHOW);
ShellExecute(Handle, 'open', 'c:\AWordDocument.doc', nil, nil, SW_SHOW);
ShellExecute(Handle, ‘print’, 'c:\AWordDocument.doc', nil, nil, SW_SHOW);

Open a folder in Explorer

ShellExecute(Handle, 'explore', 'c:\', nil, nil, SW_SHOWMAXIMIZED);

Open a URL in the default webbrowser

ShellExecute(Handle, 'open', 'www.scip.be', nil, nil, SW_SHOW);


Waiting for an external application to close

Sometimes it is necessary to wait until the external application is being closed before continuing the execution of your program. This can not be done with the ShellExecute function. But with some WinAPI functions like CreateProcess (to start an application) and WaitForSingleObject (to check if application is being closed) you can create your own ExecuteAndWait function.

function ExecuteAndWait(const strCommandLine : String; intVisibility: Integer = SW_SHOW) : Cardinal;
var
  StartupInfo : TStartupInfo;
  ProcessInformation : TProcessInformation;
  intWaitState : DWORD;
begin
  Result := 0;
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  StartupInfo.cb := SizeOf(TStartupInfo);
  StartupInfo.wShowWindow := intVisibility;

  if (CreateProcess(nil, PChar(strCommandLine), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInformation)) then
  begin
    intWaitState := WaitForSingleObject(ProcessInformation.hProcess, INFINITE);

    if (intWaitState = WAIT_OBJECT_0) then
      if (GetExitCodeProcess(ProcessInformation.hProcess, intWaitState)) then
        Result := intWaitState;

    CloseHandle(ProcessInformation.hProcess);
    CloseHandle(ProcessInformation.hThread);
  end;
end;