Pascal Scripting: ShellExec

Prototype:

function ShellExec(const Verb, Filename, Params, WorkingDir: String; const ShowCmd: Integer; const Wait: TExecWait; var ErrorCode: Integer): Boolean;

Description:

Opens the specified file or performs another action specified by Verb, using the same credentials as Setup/Uninstall. The filename can be an executable file, a document file, a folder, or a URL. Verb may be an empty string, in which case the default verb for the file type is used (usually "open"). The Wait parameter specifies whether the function should return immediately or wait until the launched process has terminated or is idle. Returns True if the specified file was opened successfully, False otherwise. If False is returned then ErrorCode specifies the error that occurred. Use SysErrorMessage(ErrorCode) to get a description of the error.

Remarks:

TExecWait is defined as:

TExecWait = (ewNoWait, ewWaitUntilTerminated, ewWaitUntilIdle);

Note that passing a Wait value other than ewNoWait will have no effect if a new process isn't spawned (for example, if the file is opened inside an already-running instance of the program that handles the file type).

Refer to the system error codes on MSDN [external link].

Example:
var
  ErrorCode: Integer;
begin
  if not ShellExec('', ExpandConstant('{app}\filename.rtf'),
     '', '', SW_SHOW, ewNoWait, ErrorCode) then
  begin
    // handle failure if necessary
  end;
end;
See also:

ShellExecAsOriginalUser