process.pp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. {
  2. $Id$
  3. This file is part of the Free Pascal run time library.
  4. Copyright (c) 1999-2000 by Michael Van Canneyt
  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. unit Process;
  12. {$mode delphi}
  13. {$H+}
  14. interface
  15. Uses Classes,Pipes;
  16. Type
  17. THandle = Longint;
  18. Type
  19. TProcessOptions = (poExecuteOnCreate,poRunSuspended,poUsePipes,
  20. poNoConsole,poStderrToOutPut,poWaitOnExit);
  21. TCreateOptions = Set of TPRocessOptions;
  22. TProcess = Class (TObject)
  23. Private
  24. FShowWindow : Boolean;
  25. FFillAttribute,
  26. FWindowColumns,
  27. FWindowHeight,
  28. FWindowLeft,
  29. FWindowRows,
  30. FWindowTop,
  31. FWindowWidth : Longint;
  32. FWindowRect : TRect;
  33. FApplicationName : string;
  34. FChildErrorStream : TOutPutPipeStream;
  35. FChildInputSTream : TInputPipeStream;
  36. FChildOutPutStream : TOutPutPipeStream;
  37. FConsoleTitle : String;
  38. FCreateOptions : TCreateOptions;
  39. FCreationFlags : Cardinal;
  40. FCommandLine : String;
  41. FCurrentDirectory : String;
  42. FDeskTop : String;
  43. FEnvironment : Pointer;
  44. FExitCode : Cardinal;
  45. FPID : Longint;
  46. FThreadHandle,
  47. FHandle : THandle;
  48. FInherithandles : LongBool;
  49. FParentErrorStream : TInputPipeStream;
  50. FParentInputSTream : TInputPipeStream;
  51. FParentOutputStream : TOutPutPipeStream;
  52. FPrepared : Boolean;
  53. FRunning : Boolean;
  54. Procedure FreeStreams;
  55. Function GetExitStatus : Integer;
  56. Function GetRunning : Boolean;
  57. Function GetWindowRect : TRect;
  58. Procedure SetWindowRect (Value : TRect);
  59. Public
  60. Constructor Create (Const ACommandline : String;
  61. Options : TCreateOptions);
  62. Destructor Destroy; override;
  63. Procedure Execute; virtual;
  64. Function Resume : Integer; virtual;
  65. Function Suspend : Integer; virtual;
  66. Function Terminate (AExitCode : Integer): Boolean; virtual;
  67. Function WaitOnExit : DWord;
  68. Property ApplicationName : String Read FApplicationname
  69. Write FApplicationname;
  70. Property CommandLine : String Read FCommandLine;
  71. Property ConsoleTitle : String Read FConsoleTitle Write FConsoleTitle;
  72. Property CurrentDirectory : String Read FCurrentDirectory
  73. Write FCurrentDirectory;
  74. Property CreateOptions : TCreateOptions Read FCreateOptions;
  75. Property CreationFlags : Cardinal Read FCreationFlags Write FCreationFlags;
  76. Property DeskTop : String Read FDeskTop Write FDeskTop;
  77. Property Environment : Pointer Read FEnvironment Write FEnvironment;
  78. Property ExitStatus : Integer Read GetExitStatus;
  79. Property FillAttribute : Longint Read FFillAttribute Write FFillAttribute;
  80. Property Handle : THandle Read FHandle;
  81. Property ThreadHandle : THandle Read FThreadHandle;
  82. Property PID : Longint;
  83. Property Input : TOutPutPipeStream Read FParentOutPutStream;
  84. Property InheritHandles : LongBool Read FInheritHandles;
  85. Property OutPut : TInputPipeStream Read FParentInputStream;
  86. Property Running : Boolean Read GetRunning;
  87. Property ShowWindow : Boolean Read FShowWindow Write FShowWindow;
  88. Property StdErr : TinputPipeStream Read FParentErrorStream;
  89. Property WindowColumns : Longint Read FWindowColumns Write FWindowColumns;
  90. Property WindowHeight : Longint Read FWindowHeight Write FWindowHeight;
  91. Property WindowLeft : Longint Read FWindowLeft Write FWindowLeft;
  92. Property WindowRows : Longint Read FWindowRows Write FWindowRows;
  93. Property WindowTop : Longint Read FWindowTop Write FWindowTop;
  94. Property WindowWidth : Longint Read FWindowWidth Write FWindowWidth;
  95. Property WindowRect : Trect Read GetWindowRect Write SetWindowRect;
  96. end;
  97. implementation
  98. {$i process.inc}
  99. Constructor TProcess.Create (Const ACommandline : String;
  100. Options : TCreateOptions);
  101. begin
  102. Inherited create;
  103. FCreateOptions:=Options;
  104. FCommandLine:=ACommandLine;
  105. FInheritHandles:=True;
  106. FFillAttribute := -1;
  107. FWindowColumns := -1;
  108. FWindowHeight := -1;
  109. FWindowLeft := -1;
  110. FWindowRows := -1;
  111. FWindowTop := -1;
  112. FWindowWidth := -1;
  113. If poExecuteOnCreate in FCreateOptions then
  114. execute;
  115. end;
  116. Destructor TProcess.Destroy;
  117. begin
  118. FreeStreams;
  119. end;
  120. Procedure TProcess.FreeStreams;
  121. begin
  122. if FChildErrorStream<>FChildoutputStream then
  123. begin
  124. FChildErrorStream.free;
  125. FParentErrorStream.free;
  126. end;
  127. FParentInputSTream.Free;
  128. FParentOutputStream.Free;
  129. FChildInputSTream.Free;
  130. FChildOutPutStream.Free;
  131. end;
  132. Function TProcess.GetExitStatus : Integer;
  133. begin
  134. {
  135. If FRunning then
  136. GetExitCodeProcess(Handle,@FExitCode);
  137. }
  138. Result:=FExitCode;
  139. end;
  140. Function TProcess.GetWindowRect : TRect;
  141. begin
  142. With Result do
  143. begin
  144. Left:=FWindowLeft;
  145. Top:=FWindowTop;
  146. Right:=FWindowLeft+FWindowWidth;
  147. Bottom:=FWindowTop+FWindowRows;
  148. end;
  149. end;
  150. Procedure TProcess.SetWindowRect (Value : classes.Trect);
  151. begin
  152. With Value do
  153. begin
  154. FWindowLeft:=Left;
  155. FWindowWidth:=Right-Left;
  156. FWindowTop:=Top;
  157. FWindowRows:=Bottom-top;
  158. end;
  159. end;
  160. end.
  161. {
  162. $Log$
  163. Revision 1.2 2000-07-13 11:33:00 michael
  164. + removed logs
  165. }