process.pp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. Protected
  80. procedure CloseProcessHandles; 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. 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.