process.pp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. {
  2. $Id$
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {$mode objfpc}
  12. {$h+}
  13. unit process;
  14. interface
  15. Uses Classes,
  16. pipes,
  17. SysUtils;
  18. Type
  19. TProcessOption = (poRunSuspended,poWaitOnExit,
  20. poUsePipes,poStderrToOutPut,
  21. poNoConsole,poNewConsole,
  22. poDefaultErrorMode,poNewProcessGroup,
  23. poDebugProcess,poDebugOnlyThisProcess);
  24. TShowWindowOptions = (swoNone,swoHIDE,swoMaximize,swoMinimize,swoRestore,swoShow,
  25. swoShowDefault,swoShowMaximized,swoShowMinimized,
  26. swoshowMinNOActive,swoShowNA,swoShowNoActivate,swoShowNormal);
  27. TStartupOption = (suoUseShowWindow,suoUseSize,suoUsePosition,
  28. suoUseCountChars,suoUseFillAttribute);
  29. TProcessPriority = (ppHigh,ppIdle,ppNormal,ppRealTime);
  30. TProcessOptions = Set of TPRocessOption;
  31. TstartUpoptions = set of TStartupOption;
  32. Type
  33. TProcess = Class (TComponent)
  34. Private
  35. FProcessOptions : TProcessOptions;
  36. FStartupOptions : TStartupOptions;
  37. FProcessID : Integer;
  38. FThreadID : Integer;
  39. FProcessHandle : Thandle;
  40. FThreadHandle : Thandle;
  41. FFillAttribute : Cardinal;
  42. FApplicationName : string;
  43. FConsoleTitle : String;
  44. FCommandLine : String;
  45. FCurrentDirectory : String;
  46. FDeskTop : String;
  47. FEnvironment : Tstrings;
  48. FExitCode : Cardinal;
  49. FShowWindow : TShowWindowOptions;
  50. FInherithandles : Boolean;
  51. FInputSTream : TOutputPipeStream;
  52. FOutputStream : TInPutPipeStream;
  53. FStdErrStream : TInputPipeStream;
  54. FRunning : Boolean;
  55. FPRocessPriority : TProcessPriority;
  56. dwXCountchars,
  57. dwXSize,
  58. dwYsize,
  59. dwx,
  60. dwYcountChars,
  61. dwy : Cardinal;
  62. Procedure FreeStreams;
  63. Function GetExitStatus : Integer;
  64. Function GetRunning : Boolean;
  65. Function GetWindowRect : TRect;
  66. Procedure SetWindowRect (Value : TRect);
  67. Procedure SetShowWindow (Value : TShowWindowOptions);
  68. Procedure SetWindowColumns (Value : Cardinal);
  69. Procedure SetWindowHeight (Value : Cardinal);
  70. Procedure SetWindowLeft (Value : Cardinal);
  71. Procedure SetWindowRows (Value : Cardinal);
  72. Procedure SetWindowTop (Value : Cardinal);
  73. Procedure SetWindowWidth (Value : Cardinal);
  74. Procedure CreateStreams(InHandle,OutHandle,Errhandle : Longint);
  75. procedure SetApplicationname(const Value: String);
  76. procedure SetProcessOptions(const Value: TProcessOptions);
  77. procedure SetActive(const Value: Boolean);
  78. procedure SetEnvironment(const Value: TStrings);
  79. function PeekExitStatus: Boolean;
  80. procedure CloseProcessHandles;
  81. Public
  82. Constructor Create (AOwner : TComponent);override;
  83. Destructor Destroy; override;
  84. Procedure Execute; virtual;
  85. Function Resume : Integer; virtual;
  86. Function Suspend : Integer; virtual;
  87. Function Terminate (AExitCode : Integer): Boolean; virtual;
  88. Function WaitOnExit : DWord;
  89. Property WindowRect : Trect Read GetWindowRect Write SetWindowRect;
  90. Property Handle : THandle Read FProcessHandle;
  91. Property ProcessHandle : THandle Read FProcessHandle;
  92. Property ThreadHandle : THandle Read FThreadHandle;
  93. Property ProcessID : Integer Read FProcessID;
  94. Property ThreadID : Integer Read FThreadID;
  95. Property Input : TOutPutPipeStream Read FInPutStream;
  96. Property OutPut : TInputPipeStream Read FOutPutStream;
  97. Property StdErr : TinputPipeStream Read FStdErrStream;
  98. Property ExitStatus : Integer Read GetExitStatus;
  99. Property InheritHandles : Boolean Read FInheritHandles Write FInheritHandles;
  100. Published
  101. Property Active : Boolean Read Getrunning Write SetActive;
  102. Property ApplicationName : String Read FApplicationname Write SetApplicationname;
  103. Property CommandLine : String Read FCommandLine Write FCommandLine;
  104. Property ConsoleTitle : String Read FConsoleTitle Write FConsoleTitle;
  105. Property CurrentDirectory : String Read FCurrentDirectory Write FCurrentDirectory;
  106. Property DeskTop : String Read FDeskTop Write FDeskTop;
  107. Property Environment : TStrings Read FEnvironment Write SetEnvironment;
  108. Property Options : TProcessOptions Read FProcessOptions Write SetPRocessOptions;
  109. Property Priority : TProcessPriority Read FProcessPriority Write FProcessPriority;
  110. Property StartUpOptions : TStartUpOptions Read FStartUpOptions Write FStartupOptions;
  111. Property Running : Boolean Read GetRunning;
  112. Property ShowWindow : TShowWindowOptions Read FShowWindow Write SetShowWindow;
  113. Property WindowColumns : Cardinal Read dwXCountchars Write SetWindowColumns;
  114. Property WindowHeight : Cardinal Read dwYsize Write SetWindowHeight;
  115. Property WindowLeft : Cardinal Read dwx Write SetWindowLeft;
  116. Property WindowRows : Cardinal Read dwYcountChars Write SetWindowRows;
  117. Property WindowTop : Cardinal Read dwy Write SetWindowTop ;
  118. Property WindowWidth : Cardinal Read dwXsize Write SetWindowWidth;
  119. Property FillAttribute : Cardinal read FFillAttribute Write FFillAttribute;
  120. end;
  121. implementation
  122. {$i process.inc}
  123. Constructor TProcess.Create (AOwner : TComponent);
  124. begin
  125. Inherited;
  126. FProcessPriority:=ppNormal;
  127. FShowWindow:=swoNone;
  128. FInheritHandles:=True;
  129. FEnvironment:=TStringList.Create;
  130. end;
  131. Destructor TProcess.Destroy;
  132. begin
  133. FEnvironment.Free;
  134. FreeStreams;
  135. CloseProcessHandles;
  136. Inherited Destroy;
  137. end;
  138. Procedure TProcess.FreeStreams;
  139. procedure FreeStream(var S: THandleStream);
  140. begin
  141. if (S<>Nil) then
  142. begin
  143. FileClose(S.Handle);
  144. FreeAndNil(S);
  145. end;
  146. end;
  147. begin
  148. If FStdErrStream<>FOutputStream then
  149. FreeStream(FStdErrStream);
  150. FreeStream(FOutputStream);
  151. FreeStream(FInputStream);
  152. end;
  153. Function TProcess.GetExitStatus : Integer;
  154. begin
  155. If FRunning then
  156. PeekExitStatus;
  157. Result:=FExitCode;
  158. end;
  159. Function TProcess.GetRunning : Boolean;
  160. begin
  161. IF FRunning then
  162. FRunning:=Not PeekExitStatus;
  163. Result:=FRunning;
  164. end;
  165. Procedure TProcess.CreateStreams(InHandle,OutHandle,Errhandle : Longint);
  166. begin
  167. FreeStreams;
  168. FInputStream:=TOutputPipeStream.Create (InHandle);
  169. FOutputStream:=TInputPipeStream.Create (OutHandle);
  170. if Not (poStdErrToOutPut in FProcessOptions) then
  171. FStdErrStream:=TInputPipeStream.Create(ErrHandle);
  172. end;
  173. Procedure TProcess.SetWindowColumns (Value : Cardinal);
  174. begin
  175. if Value<>0 then
  176. Include(FStartUpOptions,suoUseCountChars);
  177. dwXCountChars:=Value;
  178. end;
  179. Procedure TProcess.SetWindowHeight (Value : Cardinal);
  180. begin
  181. if Value<>0 then
  182. include(FStartUpOptions,suoUsePosition);
  183. dwYSize:=Value;
  184. end;
  185. Procedure TProcess.SetWindowLeft (Value : Cardinal);
  186. begin
  187. if Value<>0 then
  188. Include(FStartUpOptions,suoUseSize);
  189. dwx:=Value;
  190. end;
  191. Procedure TProcess.SetWindowTop (Value : Cardinal);
  192. begin
  193. if Value<>0 then
  194. Include(FStartUpOptions,suoUsePosition);
  195. dwy:=Value;
  196. end;
  197. Procedure TProcess.SetWindowWidth (Value : Cardinal);
  198. begin
  199. If (Value<>0) then
  200. Include(FStartUpOptions,suoUseSize);
  201. dwXSize:=Value;
  202. end;
  203. Function TProcess.GetWindowRect : TRect;
  204. begin
  205. With Result do
  206. begin
  207. Left:=dwx;
  208. Right:=dwx+dwxSize;
  209. Top:=dwy;
  210. Bottom:=dwy+dwysize;
  211. end;
  212. end;
  213. Procedure TProcess.SetWindowRect (Value : Trect);
  214. begin
  215. Include(FStartupOptions,suouseSize);
  216. Include(FStartupOptions,suoUsePosition);
  217. With Value do
  218. begin
  219. dwx:=Left;
  220. dwxSize:=Right-Left;
  221. dwy:=Top;
  222. dwySize:=Bottom-top;
  223. end;
  224. end;
  225. Procedure TProcess.SetWindowRows (Value : Cardinal);
  226. begin
  227. if Value<>0 then
  228. Include(FStartUpOptions,suoUseCountChars);
  229. dwYCountChars:=Value;
  230. end;
  231. procedure TProcess.SetApplicationname(const Value: String);
  232. begin
  233. FApplicationname := Value;
  234. If (csdesigning in ComponentState) and
  235. (FCommandLine='') then
  236. FCommandLine:=Value;
  237. end;
  238. procedure TProcess.SetProcessOptions(const Value: TProcessOptions);
  239. begin
  240. FProcessOptions := Value;
  241. If poNewConsole in FPRocessOptions then
  242. Exclude(FProcessoptions,poNoConsole);
  243. if poRunSuspended in FProcessOptions then
  244. Exclude(FPRocessoptions,poWaitOnExit);
  245. end;
  246. procedure TProcess.SetActive(const Value: Boolean);
  247. begin
  248. if (Value<>GetRunning) then
  249. If Value then
  250. Execute
  251. else
  252. Terminate(0);
  253. end;
  254. procedure TProcess.SetEnvironment(const Value: TStrings);
  255. begin
  256. FEnvironment.Assign(Value);
  257. end;
  258. end.
  259. {
  260. $Log$
  261. Revision 1.24 2004-09-11 22:26:40 michael
  262. * Added ThreadID, ProcessID properties
  263. Revision 1.23 2004/09/09 13:47:38 michael
  264. + Patch from Vincent Snijders to correctly handle PeekExitStatus
  265. Revision 1.22 2004/09/08 18:17:23 michael
  266. + Removed extra handle on process.
  267. Revision 1.21 2004/08/12 14:33:55 michael
  268. + New split of process.pp
  269. Revision 1.20 2004/07/30 12:55:42 michael
  270. Closing process handles in Windows. Patch from Vincent Snijders
  271. Revision 1.19 2004/02/03 08:12:22 michael
  272. + Patch from Vincent Snijders to fix passing environment vars in win32
  273. Revision 1.18 2003/10/30 20:34:47 florian
  274. * fixed inherited destroy; call of tprocess
  275. Revision 1.17 2003/09/20 12:38:29 marco
  276. * FCL now compiles for FreeBSD with new 1.1. Now Linux.
  277. Revision 1.16 2003/08/12 13:49:42 michael
  278. + Freed streams were not closed correctly
  279. Revision 1.15 2003/05/08 20:04:16 armin
  280. * Dont close FStartupInfo.hStdError if options include poStdErrToOutPut
  281. Revision 1.14 2003/04/27 21:21:42 sg
  282. * Added typecast to prevent range check error in TProcess.WaitOnExit
  283. Revision 1.13 2002/09/07 15:15:25 peter
  284. * old logs removed and tabs fixed
  285. }