RunProgram()

Syntax

Result = RunProgram(Filename$ [, Parameter$, WorkingDirectory$ [, Flags [, SenderProgram]]])
Description
Launches an external program.

Parameters

Filename$ The executable name including its path.
Parameters$ (optional) Specifies the command line parameters that will be passed to the program.
WorkingDirectory$ (optional) Specifies the directory that will then be the current directory for the launched program.
Flags (optional) It can be a combination (using '|' OR operator) of the following values:
  #PB_Program_Wait   : Wait until the launched program quits
  #PB_Program_Hide   : Launch the program in invisible mode
  #PB_Program_Open   : Open the program to communicate with it or get information about it.
  #PB_Program_Read   : Read the programs console output. (stdout)
  #PB_Program_Write  : Write to the input of the program. (stdin)
  #PB_Program_Error  : Read the error output of the program. (stderr)
  #PB_Program_Connect: Connect another programs output to this programs input.
  #PB_Program_Ascii  : Default read/write operation are in ASCII mode (default in ASCII mode).
  #PB_Program_Unicode: Default read/write operation are in Unicode mode.
  #PB_Program_UTF8   : Default read/write operation are in UTF8 mode (default in unicode mode).
A program executed with #PB_Program_Open must be closed with CloseProgram(). The 'Read', 'Write', 'Error' and 'Connect' flags require the #PB_Program_Open flag to be set as well.

When using the #PB_Program_Connect flag, another program must have been started before with #PB_Program_Open and #PB_Program_Read. The number returned when running this program must be passed as the 'SenderProgram' parameter to RunProgram(). The output of the sender program will be sent directly to the input of the now executed program. Several programs may be connected in this way, to 'pipe' data through that group of connected programs.

The following functions may be used with a program that has been executed with the #PB_Program_Open flag:

- IsProgram(): check if the program number represents a valid program executed by RunProgram().
- ProgramID(): returns the OS ProcessID for the program.
- ProgramRunning(): check if the program is still running.
- WaitProgram(): wait for the program to end.
- KillProgram(): terminate the program.
- ProgramExitCode(): get the exitcode of the program.
- CloseProgram(): close the connection to the program.

The following functions may be used for programs executed with the 'Read', 'Write' and 'Error' flags:

- AvailableProgramOutput(): check if the programs output is available.
- ReadProgramString(): read a string from the programs output.
- ReadProgramData(): read data from the programs output.
- ReadProgramError(): read a string from the programs error output.
- WriteProgramString(): write a string to the programs input.
- WriteProgramData(): write data to the programs input.

Return value

Nonzero if the program has been successfully launched, zero otherwise.

If #PB_Program_Open was included in the Flags, the return-value is a number that identifies the program. It may be used in calls to get information about the program such as ReadProgramString() or ProgramExitCode() or other functions mentioned above.

Example

  ; Executes the PB compiler with the /? option and displays the output (windows version)
  ; For Linux/MacOS change the "/?" to "-h".
  ;
  Compiler = RunProgram(#PB_Compiler_Home+"/Compilers/pbcompiler", "/?", "", #PB_Program_Open | #PB_Program_Read)
  Output$ = ""
  If Compiler
    While ProgramRunning(Compiler)
      If AvailableProgramOutput(Compiler)
        Output$ + ReadProgramString(Compiler) + Chr(13)
      EndIf
    Wend
    Output$ + Chr(13) + Chr(13)
    Output$ + "Exitcode: " + Str(ProgramExitCode(Compiler))
    
    CloseProgram(Compiler) ; Close the connection to the program
  EndIf
  
  MessageRequester("Output", Output$)
On Windows RunProgram() uses the default application associated with the file type of the given file. An example: RunProgram("Test.html") will open the web browser, which is generally used for displaying web sites on your system.

Supported OS

All

<- RemoveEnvironmentVariable() - Process Index - SetEnvironmentVariable() ->