process.pp 8.5 KB

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