process.pp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. FRunning : Boolean;
  51. FProcessPriority : TProcessPriority;
  52. dwXCountchars,
  53. dwXSize,
  54. dwYsize,
  55. dwx,
  56. dwYcountChars,
  57. dwy : Cardinal;
  58. Procedure FreeStreams;
  59. Function GetExitStatus : Integer;
  60. Function GetRunning : Boolean;
  61. Function GetWindowRect : TRect;
  62. Procedure SetWindowRect (Value : TRect);
  63. Procedure SetShowWindow (Value : TShowWindowOptions);
  64. Procedure SetWindowColumns (Value : Cardinal);
  65. Procedure SetWindowHeight (Value : Cardinal);
  66. Procedure SetWindowLeft (Value : Cardinal);
  67. Procedure SetWindowRows (Value : Cardinal);
  68. Procedure SetWindowTop (Value : Cardinal);
  69. Procedure SetWindowWidth (Value : Cardinal);
  70. procedure SetApplicationName(const Value: String);
  71. procedure SetProcessOptions(const Value: TProcessOptions);
  72. procedure SetActive(const Value: Boolean);
  73. procedure SetEnvironment(const Value: TStrings);
  74. function PeekExitStatus: Boolean;
  75. Protected
  76. FInputStream : TOutputPipeStream;
  77. FOutputStream : TInputPipeStream;
  78. FStderrStream : TInputPipeStream;
  79. procedure CloseProcessHandles; virtual;
  80. Procedure CreateStreams(InHandle,OutHandle,ErrHandle : Longint);virtual;
  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. EProcess = Class(Exception);
  122. implementation
  123. {$i process.inc}
  124. Constructor TProcess.Create (AOwner : TComponent);
  125. begin
  126. Inherited;
  127. FProcessPriority:=ppNormal;
  128. FShowWindow:=swoNone;
  129. FInheritHandles:=True;
  130. FEnvironment:=TStringList.Create;
  131. end;
  132. Destructor TProcess.Destroy;
  133. begin
  134. FEnvironment.Free;
  135. FreeStreams;
  136. CloseProcessHandles;
  137. Inherited Destroy;
  138. end;
  139. Procedure TProcess.FreeStreams;
  140. procedure FreeStream(var S: THandleStream);
  141. begin
  142. if (S<>Nil) then
  143. begin
  144. FileClose(S.Handle);
  145. FreeAndNil(S);
  146. end;
  147. end;
  148. begin
  149. If FStderrStream<>FOutputStream then
  150. FreeStream(FStderrStream);
  151. FreeStream(FOutputStream);
  152. FreeStream(FInputStream);
  153. end;
  154. Function TProcess.GetExitStatus : Integer;
  155. begin
  156. If FRunning then
  157. PeekExitStatus;
  158. Result:=FExitCode;
  159. end;
  160. Function TProcess.GetRunning : Boolean;
  161. begin
  162. IF FRunning then
  163. FRunning:=Not PeekExitStatus;
  164. Result:=FRunning;
  165. end;
  166. Procedure TProcess.CreateStreams(InHandle,OutHandle,ErrHandle : Longint);
  167. begin
  168. FreeStreams;
  169. FInputStream:=TOutputPipeStream.Create (InHandle);
  170. FOutputStream:=TInputPipeStream.Create (OutHandle);
  171. if Not (poStderrToOutput in FProcessOptions) then
  172. FStderrStream:=TInputPipeStream.Create(ErrHandle);
  173. end;
  174. Procedure TProcess.SetWindowColumns (Value : Cardinal);
  175. begin
  176. if Value<>0 then
  177. Include(FStartupOptions,suoUseCountChars);
  178. dwXCountChars:=Value;
  179. end;
  180. Procedure TProcess.SetWindowHeight (Value : Cardinal);
  181. begin
  182. if Value<>0 then
  183. include(FStartupOptions,suoUsePosition);
  184. dwYSize:=Value;
  185. end;
  186. Procedure TProcess.SetWindowLeft (Value : Cardinal);
  187. begin
  188. if Value<>0 then
  189. Include(FStartupOptions,suoUseSize);
  190. dwx:=Value;
  191. end;
  192. Procedure TProcess.SetWindowTop (Value : Cardinal);
  193. begin
  194. if Value<>0 then
  195. Include(FStartupOptions,suoUsePosition);
  196. dwy:=Value;
  197. end;
  198. Procedure TProcess.SetWindowWidth (Value : Cardinal);
  199. begin
  200. If (Value<>0) then
  201. Include(FStartupOptions,suoUseSize);
  202. dwXSize:=Value;
  203. end;
  204. Function TProcess.GetWindowRect : TRect;
  205. begin
  206. With Result do
  207. begin
  208. Left:=dwx;
  209. Right:=dwx+dwxSize;
  210. Top:=dwy;
  211. Bottom:=dwy+dwysize;
  212. end;
  213. end;
  214. Procedure TProcess.SetWindowRect (Value : Trect);
  215. begin
  216. Include(FStartupOptions,suoUseSize);
  217. Include(FStartupOptions,suoUsePosition);
  218. With Value do
  219. begin
  220. dwx:=Left;
  221. dwxSize:=Right-Left;
  222. dwy:=Top;
  223. dwySize:=Bottom-top;
  224. end;
  225. end;
  226. Procedure TProcess.SetWindowRows (Value : Cardinal);
  227. begin
  228. if Value<>0 then
  229. Include(FStartupOptions,suoUseCountChars);
  230. dwYCountChars:=Value;
  231. end;
  232. procedure TProcess.SetApplicationName(const Value: String);
  233. begin
  234. FApplicationName := Value;
  235. If (csDesigning in ComponentState) and
  236. (FCommandLine='') then
  237. FCommandLine:=Value;
  238. end;
  239. procedure TProcess.SetProcessOptions(const Value: TProcessOptions);
  240. begin
  241. FProcessOptions := Value;
  242. If poNewConsole in FProcessOptions then
  243. Exclude(FProcessOptions,poNoConsole);
  244. if poRunSuspended in FProcessOptions then
  245. Exclude(FProcessOptions,poWaitOnExit);
  246. end;
  247. procedure TProcess.SetActive(const Value: Boolean);
  248. begin
  249. if (Value<>GetRunning) then
  250. If Value then
  251. Execute
  252. else
  253. Terminate(0);
  254. end;
  255. procedure TProcess.SetEnvironment(const Value: TStrings);
  256. begin
  257. FEnvironment.Assign(Value);
  258. end;
  259. end.