process.txt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. This file describes the TProcess object.
  2. The TProcess object provides an easy way to start and manipulate
  3. the running of other programs (processes) by your application.
  4. On top of that, it allows you to redirect the program's input, output
  5. and standard error to streams that are readable/writeable by your
  6. program.
  7. It is a descendent class of TObject, but this is easily changeable to
  8. TComponent, should you desire to do so. None of the properties will
  9. conflict with the existing properties of TComponent.
  10. Furthermore it is written in such a way that it is easily extensible,
  11. although most of the properties that a Process has, are accessible and
  12. can be controlled with this object.
  13. In what follows, is a description of the object's methods and properties.
  14. The following two types control the creation of the TProcess Object.
  15. See The constructor description for a description on what they do.
  16. TProcessOptions = (poExecuteOnCreate,poRunSuspended,poUsePipes,
  17. poNoConsole,poStderrToOutPut,poWaitOnExit);
  18. TCreateOptions = Set of TPRocessOptions;
  19. Constructor Create (Const ACommandline : String;
  20. Options : TCreateOptions);
  21. This creates an TPRocess object.
  22. ACommandline is the commandline to execute, including any options
  23. you wish to pass to the program. If you don't specify an explicit path
  24. Windows will look for your program in the Windows directory and in the
  25. path.
  26. Options control the behaviour of the object. It can be a set of the
  27. following constants:
  28. poExecuteOnCreate
  29. If you include this option, the constructor will immediatly
  30. call the Execute method, using default settings for all parameters.
  31. This has the effect that the program is run at once.
  32. poRunSuspended
  33. If you include this option, the Execute method will start the
  34. program in a suspended state, and the program will start running
  35. only after you have called the Resume method.
  36. poUsePipes
  37. If you include this option, the Execute method will redirect the
  38. standard input,output and error descriptors to 3 pipes, which you
  39. can read from or write to.
  40. (see Input,OutPut and Error properties)
  41. It makes little sense to use ths for GUI applications (i.e. non-
  42. console applications)
  43. poNoConsole
  44. If you include this option, the application will not display a
  45. console, untill it explicitly needs one or requests one using the
  46. AllocConsole method. This is very convenient in combination with the
  47. poUsePipes option, allowing you to run an application without getting
  48. the Console window, and being able to read it's output at once.
  49. poStderrToOutPut
  50. If This option is included, then the error desciptor is redirected to
  51. the standard output director, i.e. all output goes to the standard
  52. output.
  53. poWaitOnExit
  54. If you specify this option, then the Execute method will wait for the
  55. executed program to finish, before returning.
  56. This option will be ignored if you also specified ExecuteOnCreate and
  57. CreateSuspended.
  58. Destructor Destroy; virtual;
  59. Destroys the TProcess Object. Be careful NOT to close a TProcess
  60. object when you use pipes, and the application is still running.
  61. you may kill it.
  62. Procedure Execute; virtual;
  63. This actually runs the application. It will return immediatly, unless
  64. you specified the poWaitOnExit option when creating the object.
  65. Function Resume : Integer; virtual;
  66. Resume lowers the suspend count of the application.
  67. it returns the new suspend count of the application. As long as the
  68. suspend count is larger than 0, the application will not run.
  69. If the suspend count reaches 0, the application will continue
  70. running.
  71. Function Suspend : Integer; virtual;
  72. Increases the suspend count of the application, and returns the
  73. new suspend count of the application.
  74. Function Terminate (AExitCode : Integer): Boolean; virtual;
  75. Terminate terminates the main thread of the application, giving it
  76. exitcode 'AExitCode'
  77. It returns True on succes, False on failure.
  78. Function WaitOnExit : Integer;
  79. This function returns immediatly if the application is not running,
  80. and waits for the application to finish if it was still running.
  81. Property ApplicationName : String;
  82. Sets the name of the application.
  83. Property CommandLine : String;
  84. Read-Only
  85. contains the commandline of the application, as set by the create
  86. method of TProcess.
  87. Property ConsoleTitle : String;
  88. For console applications only :
  89. Sets the title that appears in the title bar of the Console window.
  90. Property CreateOptions : TCreateOptions;
  91. Read-Only
  92. Contains the options as set by the Create method of TProcess.
  93. Property CreationFlags : Cardinal;
  94. This contains the creation flags that are passed to the CreateProcess
  95. call. These flags are modified by the Execute call to reflect any
  96. settings tat you may have made.
  97. Property CurrentDirectory : String;
  98. When set, the process wil start in the directory that you have set
  99. for it.
  100. Property DeskTop : String;
  101. NT only:
  102. Contains the name of the desktop or window station that the process
  103. will be run on. See STARTUPINFO in the win32 programmers manual.
  104. Property Environment : Pointer;
  105. A pointer to a null-terminated list of environment variable pointers.
  106. Each pair is of the form 'Name=Value'.
  107. If this is nil, the environment of your application is used.
  108. Property ExitStatus : Integer;
  109. Read-Only
  110. This returns the exit status of the application, or STILL_ACTIVE
  111. (defined in Windows.pas) if the application is still running.
  112. Property FillAttribute : Integer;
  113. For console processes only.
  114. Sets the fill color for the console window.
  115. Property Handle : THandle;
  116. Read-Only;
  117. Returns the handle of the process, which can be used to pass on to
  118. calls that require a handle of a process.
  119. Onl valid if the process is running.
  120. Property Input : TOutPutPipeStream;
  121. Read-Only
  122. Returns the Input handle of the process.
  123. Anything you write to this stream, will appear on the applications
  124. input file descriptor.
  125. Only valid if you used poUsePipes when you created the TProcess
  126. object.
  127. Property InheritHandles : LongBool;
  128. If you set this to true, each inheritable handle of your application
  129. is inherited by the new application.
  130. Property OutPut : TInputPipeStream;
  131. Read-Only
  132. Returns the Output handle of the process. Anything the process writes
  133. to its standard output can be read from this stream.
  134. Only valid if you used poUsePipes when you created the TProcess
  135. object.
  136. Property ProcessAttributes : TSecurityAttributes;
  137. Property ProcessInformation : TProcessInformation;
  138. Read-Only
  139. Gives access to the ProcessInformation returned by Windows
  140. upon executing the program. This contains
  141. hProcess : Process Handle (See Handle property)
  142. hThread : Process' main thread handle (See ThreadHandle property)
  143. dwProcessId : Process ID. (as seen in the task manager)
  144. dwThreadId : Process' main thread ID
  145. Property Running : Boolean;
  146. Read-Only
  147. Retruns True if the application is still running, False otherwise.
  148. If the application is suspended or not doesn't affect the result.
  149. Property ShowWindow : Word;
  150. You can set the applications ShowWindow attribute here.
  151. Property StartupInfo : TStartupInfo;
  152. Read-Only
  153. Gives access to the TStartupInfo that will be passed to the
  154. application in the CreateProcess Call. You can manipulate its various
  155. members through the properties of the TProcess object.
  156. Property StdErr : TinputPipeStream;
  157. Read-Only
  158. Returns the Output handle of the process. Anything the process writes
  159. to its error output can be read from this stream.
  160. Only valid if you used poUsePipes when you created the TProcess
  161. object.
  162. If you specified poStderrToOutput then this is the same as the
  163. 'Output' stream.
  164. Property ThreadAttributes : TSecurityAttributes;
  165. Contains the security attributes that will be passed to the process'
  166. main thread. By default, no security attributes are passed.
  167. Property ThreadHandle : THandle;
  168. Read-Only
  169. Returns the Handle of the process' main thread.
  170. Property WindowColumns : Integer;
  171. For console applications:
  172. This will set the number of screen columns that the console window
  173. will have.
  174. If you don't set this property nor the WindowRows property, Windows will
  175. choose default values.
  176. You can only set this PRIOR to calling the execute method, after
  177. the application was executed, or while it is running, the setting
  178. will be ignored until you run it again.
  179. Property WindowHeight : Integer;
  180. Set the height of the application's main window.
  181. If you don't specify this, nor WindowWidth, Windows will choose
  182. the height and Width of the applications window.
  183. You can only set this PRIOR to calling the execute method, after
  184. the application was executed, or while it is running, the setting
  185. will be ignored until you run it again.
  186. Property WindowLeft : Integer;
  187. Set the applications main window position, in pixels from the left
  188. side of the screen.
  189. If you don't specify this, nor WindowTop, Windows will choose
  190. the Left and Top of the applications window.
  191. You can only set this PRIOR to calling the execute method, after
  192. the application was executed, or while it is running, the setting
  193. will be ignored until you run it again.
  194. Property WindowRows : Integer;
  195. For console applications:
  196. This will set the number of screen rows (lines) that the console window
  197. will have.
  198. If you don't set this property nor the WindowColumns property, Windows will
  199. choose default values.
  200. You can only set this PRIOR to calling the execute method, after
  201. the application was executed, or while it is running, the setting
  202. will be ignored until you run it again.
  203. Property WindowTop : Integer;
  204. Set the applications main window position, in pixels from the Top
  205. side of the screen.
  206. If you don't specify this, nor WindowLeft, Windows will choose
  207. the Left and Top of the applications window.
  208. You can only set this PRIOR to calling the execute method, after
  209. the application was executed, or while it is running, the setting
  210. will be ignored until you run it again.
  211. Property WindowWidth : Integer;
  212. Set the Width of the application's main window.
  213. If you don't specify this, nor WindowWidth, Windows will choose
  214. the height and Width of the applications window.
  215. You can only set this PRIOR to calling the execute method, after
  216. the application was executed, or while it is running, the setting
  217. will be ignored until you run it again.
  218. Property WindowRect : Trect;
  219. This sets the bounding rectangle of the application's main window.
  220. It allows to set the WindowTop, WindowLeft, WindowHeight, WindowWidth
  221. properties in 1 call.