process.pp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. procedure FreeStream(var AStream: THandleStream);
  82. Public
  83. Constructor Create (AOwner : TComponent);override;
  84. Destructor Destroy; override;
  85. Procedure Execute; virtual;
  86. procedure CloseInput; virtual;
  87. procedure CloseOutput; virtual;
  88. procedure CloseStderr; virtual;
  89. Function Resume : Integer; virtual;
  90. Function Suspend : Integer; virtual;
  91. Function Terminate (AExitCode : Integer): Boolean; virtual;
  92. Function WaitOnExit : DWord;
  93. Property WindowRect : Trect Read GetWindowRect Write SetWindowRect;
  94. Property Handle : THandle Read FProcessHandle;
  95. Property ProcessHandle : THandle Read FProcessHandle;
  96. Property ThreadHandle : THandle Read FThreadHandle;
  97. Property ProcessID : Integer Read FProcessID;
  98. Property ThreadID : Integer Read FThreadID;
  99. Property Input : TOutputPipeStream Read FInputStream;
  100. Property Output : TInputPipeStream Read FOutputStream;
  101. Property Stderr : TinputPipeStream Read FStderrStream;
  102. Property ExitStatus : Integer Read GetExitStatus;
  103. Property InheritHandles : Boolean Read FInheritHandles Write FInheritHandles;
  104. Published
  105. Property Active : Boolean Read GetRunning Write SetActive;
  106. Property ApplicationName : String Read FApplicationName Write SetApplicationName;
  107. Property CommandLine : String Read FCommandLine Write FCommandLine;
  108. Property ConsoleTitle : String Read FConsoleTitle Write FConsoleTitle;
  109. Property CurrentDirectory : String Read FCurrentDirectory Write FCurrentDirectory;
  110. Property Desktop : String Read FDesktop Write FDesktop;
  111. Property Environment : TStrings Read FEnvironment Write SetEnvironment;
  112. Property Options : TProcessOptions Read FProcessOptions Write SetProcessOptions;
  113. Property Priority : TProcessPriority Read FProcessPriority Write FProcessPriority;
  114. Property StartupOptions : TStartupOptions Read FStartupOptions Write FStartupOptions;
  115. Property Running : Boolean Read GetRunning;
  116. Property ShowWindow : TShowWindowOptions Read FShowWindow Write SetShowWindow;
  117. Property WindowColumns : Cardinal Read dwXCountChars Write SetWindowColumns;
  118. Property WindowHeight : Cardinal Read dwYSize Write SetWindowHeight;
  119. Property WindowLeft : Cardinal Read dwX Write SetWindowLeft;
  120. Property WindowRows : Cardinal Read dwYCountChars Write SetWindowRows;
  121. Property WindowTop : Cardinal Read dwY Write SetWindowTop ;
  122. Property WindowWidth : Cardinal Read dwXSize Write SetWindowWidth;
  123. Property FillAttribute : Cardinal read FFillAttribute Write FFillAttribute;
  124. end;
  125. EProcess = Class(Exception);
  126. implementation
  127. {$i process.inc}
  128. Constructor TProcess.Create (AOwner : TComponent);
  129. begin
  130. Inherited;
  131. FProcessPriority:=ppNormal;
  132. FShowWindow:=swoNone;
  133. FInheritHandles:=True;
  134. FEnvironment:=TStringList.Create;
  135. end;
  136. Destructor TProcess.Destroy;
  137. begin
  138. FEnvironment.Free;
  139. FreeStreams;
  140. CloseProcessHandles;
  141. Inherited Destroy;
  142. end;
  143. Procedure TProcess.FreeStreams;
  144. begin
  145. If FStderrStream<>FOutputStream then
  146. FreeStream(FStderrStream);
  147. FreeStream(FOutputStream);
  148. FreeStream(FInputStream);
  149. end;
  150. Function TProcess.GetExitStatus : Integer;
  151. begin
  152. If FRunning then
  153. PeekExitStatus;
  154. Result:=FExitCode;
  155. end;
  156. Function TProcess.GetRunning : Boolean;
  157. begin
  158. IF FRunning then
  159. FRunning:=Not PeekExitStatus;
  160. Result:=FRunning;
  161. end;
  162. Procedure TProcess.CreateStreams(InHandle,OutHandle,ErrHandle : Longint);
  163. begin
  164. FreeStreams;
  165. FInputStream:=TOutputPipeStream.Create (InHandle);
  166. FOutputStream:=TInputPipeStream.Create (OutHandle);
  167. if Not (poStderrToOutput in FProcessOptions) then
  168. FStderrStream:=TInputPipeStream.Create(ErrHandle);
  169. end;
  170. procedure TProcess.FreeStream(var AStream: THandleStream);
  171. begin
  172. if AStream = nil then exit;
  173. FileClose(AStream.Handle);
  174. FreeAndNil(AStream);
  175. end;
  176. procedure TProcess.CloseInput;
  177. begin
  178. FreeStream(FInputStream);
  179. end;
  180. procedure TProcess.CloseOutput;
  181. begin
  182. FreeStream(FOutputStream);
  183. end;
  184. procedure TProcess.CloseStderr;
  185. begin
  186. FreeStream(FStderrStream);
  187. end;
  188. Procedure TProcess.SetWindowColumns (Value : Cardinal);
  189. begin
  190. if Value<>0 then
  191. Include(FStartupOptions,suoUseCountChars);
  192. dwXCountChars:=Value;
  193. end;
  194. Procedure TProcess.SetWindowHeight (Value : Cardinal);
  195. begin
  196. if Value<>0 then
  197. include(FStartupOptions,suoUsePosition);
  198. dwYSize:=Value;
  199. end;
  200. Procedure TProcess.SetWindowLeft (Value : Cardinal);
  201. begin
  202. if Value<>0 then
  203. Include(FStartupOptions,suoUseSize);
  204. dwx:=Value;
  205. end;
  206. Procedure TProcess.SetWindowTop (Value : Cardinal);
  207. begin
  208. if Value<>0 then
  209. Include(FStartupOptions,suoUsePosition);
  210. dwy:=Value;
  211. end;
  212. Procedure TProcess.SetWindowWidth (Value : Cardinal);
  213. begin
  214. If (Value<>0) then
  215. Include(FStartupOptions,suoUseSize);
  216. dwXSize:=Value;
  217. end;
  218. Function TProcess.GetWindowRect : TRect;
  219. begin
  220. With Result do
  221. begin
  222. Left:=dwx;
  223. Right:=dwx+dwxSize;
  224. Top:=dwy;
  225. Bottom:=dwy+dwysize;
  226. end;
  227. end;
  228. Procedure TProcess.SetWindowRect (Value : Trect);
  229. begin
  230. Include(FStartupOptions,suoUseSize);
  231. Include(FStartupOptions,suoUsePosition);
  232. With Value do
  233. begin
  234. dwx:=Left;
  235. dwxSize:=Right-Left;
  236. dwy:=Top;
  237. dwySize:=Bottom-top;
  238. end;
  239. end;
  240. Procedure TProcess.SetWindowRows (Value : Cardinal);
  241. begin
  242. if Value<>0 then
  243. Include(FStartupOptions,suoUseCountChars);
  244. dwYCountChars:=Value;
  245. end;
  246. procedure TProcess.SetApplicationName(const Value: String);
  247. begin
  248. FApplicationName := Value;
  249. If (csDesigning in ComponentState) and
  250. (FCommandLine='') then
  251. FCommandLine:=Value;
  252. end;
  253. procedure TProcess.SetProcessOptions(const Value: TProcessOptions);
  254. begin
  255. FProcessOptions := Value;
  256. If poNewConsole in FProcessOptions then
  257. Exclude(FProcessOptions,poNoConsole);
  258. if poRunSuspended in FProcessOptions then
  259. Exclude(FProcessOptions,poWaitOnExit);
  260. end;
  261. procedure TProcess.SetActive(const Value: Boolean);
  262. begin
  263. if (Value<>GetRunning) then
  264. If Value then
  265. Execute
  266. else
  267. Terminate(0);
  268. end;
  269. procedure TProcess.SetEnvironment(const Value: TStrings);
  270. begin
  271. FEnvironment.Assign(Value);
  272. end;
  273. end.