/[projects]/dao/DelphiScanner/Components/tpsystools_4.04/source/StSpawn.pas
ViewVC logotype

Contents of /dao/DelphiScanner/Components/tpsystools_4.04/source/StSpawn.pas

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2671 - (show annotations) (download)
Tue Aug 25 18:15:15 2015 UTC (8 years, 9 months ago) by torben
File size: 11976 byte(s)
Added tpsystools component
1 // Upgraded to Delphi 2009: Sebastian Zierer
2
3 (* ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is TurboPower SysTools
17 *
18 * The Initial Developer of the Original Code is
19 * TurboPower Software
20 *
21 * Portions created by the Initial Developer are Copyright (C) 1996-2002
22 * the Initial Developer. All Rights Reserved.
23 *
24 * Contributor(s):
25 *
26 * ***** END LICENSE BLOCK ***** *)
27
28 {*********************************************************}
29 {* SysTools: StSpawn.pas 4.04 *}
30 {*********************************************************}
31 {* SysTools: Component to spawn another application *}
32 {*********************************************************}
33
34 {$I StDefine.inc}
35
36 unit StSpawn;
37
38 interface
39
40 uses
41 SysUtils, Windows, ExtCtrls, Messages, Classes, ShellAPI,
42
43 StBase, StConst;
44
45 type
46
47 TStWaitThread = class(TThread)
48 protected
49 FTimeOut : Longint;
50 procedure Execute; override;
51 public
52 CancelWaitEvent : THandle;
53 WaitResult : DWORD;
54 WaitFors : PWOHandleArray; {!!.01}
55
56 constructor Create(aInst, CancelIt : THandle; ATimeOut : Longint);
57 destructor Destroy; override; {!!.01}
58 end;
59
60 TStSpawnCommand = (scOpen, scPrint, scOther);
61 TStShowState = (ssMinimized, ssMaximized, ssNormal, ssMinNotActive);
62
63 TStSpawnErrorEvent = procedure (Sender : TObject; Error : Word) of object;
64 TStSpawnCompletedEvent = procedure (Sender : TObject) of object;
65 TStSpawnTimeOutEvent = procedure (Sender : TObject) of object;
66
67 TStSpawnApplication = class(TStComponent)
68 protected {private}
69 { Private declarations }
70
71 FAllowChange : Boolean;
72 FCancelEvent : THandle;
73 FDefaultDir : String;
74 FFileName : String;
75 FInstance : THandle;
76 FNotifyWhenDone : Boolean;
77 FOnCompleted : TStSpawnCompletedEvent;
78 FOnSpawnError : TStSpawnErrorEvent;
79 FOnSpawnTimeOut : TStSpawnTimeOutEvent;
80 FRunParameters : String;
81 FShowState : TStShowState;
82 FSpawnCommand : TStSpawnCommand;
83 FTimer : TTimer;
84 FTimeOut : Longint;
85 FWaitResult : DWORD;
86 FWaitThread : TStWaitThread;
87 FSpawnCommandStr : String;
88
89 protected
90 { Protected declarations }
91
92 CountDownValue : Longint;
93 procedure DoOnThreadEnd(Sender : TObject);
94 procedure SetDefaultDir(const Value : String); {!!.02}
95 procedure SetFileName(const Value : String); {!!.02}
96 procedure SetOnCompleted(Value : TStSpawnCompletedEvent);
97 procedure SetOnSpawnError(Value : TStSpawnErrorEvent);
98 procedure SetNotifyWhenDone(Value : Boolean);
99 procedure SetRunParameters(const Value : String); {!!.02}
100 procedure SetShowState(Value : TStShowState);
101 procedure SetSpawnCommand(Value : TStSpawnCommand);
102 procedure SetSpawnTimeOut(Value : TStSpawnTimeOutEvent);
103 procedure SetTimeOut(Value : Longint);
104 public
105 { Public declarations }
106
107 constructor Create(AOwner : TComponent); override;
108 destructor Destroy; override;
109
110 procedure CancelWait;
111 function Execute : THandle;
112 published
113 { Published declarations }
114
115 property DefaultDir : String
116 read FDefaultDir write SetDefaultDir;
117
118 property FileName : String
119 read FFileName write SetFileName;
120
121 property NotifyWhenDone : Boolean
122 read FNotifyWhenDone write SetNotifyWhenDone default True;
123
124 property OnCompleted : TStSpawnCompletedEvent
125 read FOnCompleted write SetOnCompleted;
126
127 property OnSpawnError : TStSpawnErrorEvent
128 read FOnSpawnError write SetOnSpawnError;
129
130 property OnTimeOut : TStSpawnTimeOutEvent
131 read FOnSpawnTimeOut write SetSpawnTimeOut;
132
133 property RunParameters : String
134 read FRunParameters write SetRunParameters;
135
136 property ShowState : TStShowState
137 read FShowState write SetShowState default ssNormal;
138
139 property SpawnCommand : TStSpawnCommand
140 read FSpawnCommand write SetSpawnCommand default scOpen;
141
142 property TimeOut : Longint
143 read FTimeOut write SetTimeOut default 0;
144
145 property SpawnCommandStr : String
146 read FSpawnCommandStr write FSpawnCommandStr;
147
148 end;
149
150 implementation
151
152 {-----------------------------------------------------------------------------}
153 { WIN32 WAIT THREAD }
154 {-----------------------------------------------------------------------------}
155
156 const {!!.01}
157 WAIT_HANDLE_COUNT = 2; {!!.01}
158
159 constructor TStWaitThread.Create(aInst, CancelIt : THandle; ATimeOut : Longint);
160 begin
161 GetMem(WaitFors, WAIT_HANDLE_COUNT * SizeOf(THandle)); {!!.01}
162 WaitFors^[0] := aInst; {!!.01}
163 WaitFors^[1] := CancelIt; {!!.01}
164 FTimeOut := ATimeOut * 1000;
165 CancelWaitEvent := CancelIt;
166
167 inherited Create(True);
168 end;
169
170 {!!.01 - Added}
171 destructor TStWaitThread.Destroy;
172 begin
173 FreeMem(WaitFors, WAIT_HANDLE_COUNT * SizeOf(THandle));
174 inherited Destroy;
175 end;
176 {!!.01 - End Added}
177
178 procedure TStWaitThread.Execute;
179 begin
180 if (FTimeOut > 0) then
181 WaitResult := WaitForMultipleObjects(WAIT_HANDLE_COUNT, WaitFors, {!!.01}
182 False, FTimeOut) {!!.01}
183 else
184 WaitResult := WaitForMultipleObjects(WAIT_HANDLE_COUNT, WaitFors, {!!.01}
185 False, INFINITE); {!!.01}
186 end;
187
188
189
190 {-----------------------------------------------------------------------------}
191 { TStSpawnApplication }
192 {-----------------------------------------------------------------------------}
193
194 constructor TStSpawnApplication.Create(AOwner : TComponent);
195 begin
196 inherited Create(AOwner);
197
198 FAllowChange := True;
199 FDefaultDir := '';
200 FFileName := '';
201 FNotifyWhenDone := True;
202 FShowState := ssNormal;
203 FSpawnCommand := scOpen;
204 FSpawnCommandStr := '';
205 FTimer := nil;
206 FTimeOut := 0;
207
208 end;
209
210 destructor TStSpawnApplication.Destroy;
211 begin
212 FTimer.Free;
213 FTimer := nil;
214
215 inherited Destroy;
216 end;
217
218
219 procedure TStSpawnApplication.CancelWait;
220 begin
221 if (FCancelEvent <> 0) then
222 SetEvent(FWaitThread.CancelWaitEvent);
223 end;
224
225
226 procedure TStSpawnApplication.DoOnThreadEnd(Sender : TObject);
227 begin
228 FWaitResult := FWaitThread.WaitResult;
229
230 case FWaitResult of
231 WAIT_FAILED :
232 begin
233 if (Assigned(FOnSpawnError)) then
234 FOnSpawnError(Self, GetLastError);
235 end;
236
237 WAIT_TIMEOUT :
238 begin
239 if Assigned(FOnSpawnTimeOut) then
240 FOnSpawnTimeOut(Self);
241 end;
242
243 WAIT_OBJECT_0,
244 WAIT_OBJECT_0 + 1 :
245 begin
246 if (FNotifyWhenDone) and (Assigned(FOnCompleted)) then
247 FOnCompleted(Self);
248 end;
249 end;
250
251 if (FCancelEvent <> 0) then begin
252 SetEvent(FCancelEvent);
253 CloseHandle(FCancelEvent);
254 FCancelEvent := 0;
255 end;
256 end;
257
258
259 function TStSpawnApplication.Execute : THandle;
260 var
261 Cmd : String;
262 HowShow : integer;
263 Res : Bool;
264 Startup : TShellExecuteInfo;
265
266 begin
267 if (FileName = '') and (RunParameters > '') then
268 RaiseStError(EStSpawnError, stscInsufficientData);
269
270 case FSpawnCommand of
271 scOpen : Cmd := 'open';
272 scPrint: Cmd := 'print';
273 scOther: Cmd := FSpawnCommandStr;
274 end;
275
276 case FShowState of
277 ssNormal : HowShow := SW_NORMAL;
278 ssMinimized : HowShow := SW_MINIMIZE;
279 ssMaximized : HowShow := SW_SHOWMAXIMIZED;
280 ssMinNotActive : HowShow := SW_SHOWMINNOACTIVE;
281 else
282 HowShow := SW_NORMAL;
283 end;
284 FInstance := 0;
285
286 with Startup do begin
287 cbSize := SizeOf(Startup);
288 fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_NO_UI;
289 Wnd := 0;
290 lpVerb := Pointer(Cmd);
291 if (FFileName > '') then
292 lpFile := PChar(FFileName)
293 else
294 lpFile := nil;
295 if (FRunParameters > '') then
296 lpParameters := PChar(FRunParameters)
297 else
298 lpParameters := nil;
299 if (FDefaultDir > '') then
300 lpDirectory := PChar(FDefaultDir)
301 else
302 lpDirectory := nil;
303 nShow := HowShow;
304 hInstApp := 0;
305 end;
306
307 Res := ShellExecuteEx(@Startup);
308 FInstance := Startup.hProcess;
309
310 if (not Res) then begin
311 Result := 0;
312 if (Assigned(FOnSpawnError)) then begin
313 FOnSpawnError(Self, GetLastError);
314 end;
315 end else
316 Result := FInstance;
317
318 if (NotifyWhenDone) then begin
319 FTimer := nil;
320 FCancelEvent := CreateEvent(nil, False, False, PChar(FloatToStr(Now)));
321
322 FWaitThread := TStWaitThread.Create(FInstance, FCancelEvent, FTimeOut);
323 FWaitThread.OnTerminate := DoOnThreadEnd;
324 FWaitThread.FreeOnTerminate := True;
325 FWaitThread.Resume;
326 end;
327 end;
328
329 procedure TStSpawnApplication.SetDefaultDir(const Value : String); {!!.02}
330 begin
331 if (FAllowChange) or (csDesigning in ComponentState) then begin
332 if (Value <> FDefaultDir) then
333 FDefaultDir := Value;
334 end;
335 end;
336
337
338 procedure TStSpawnApplication.SetFileName(const Value : String); {!!.02}
339 begin
340 if (FAllowChange) or (csDesigning in ComponentState) then begin
341 if (Value <> FileName) then
342 FFileName := Value;
343 end;
344 end;
345
346
347 procedure TStSpawnApplication.SetNotifyWhenDone(Value : Boolean);
348 begin
349 if (FAllowChange) or (csDesigning in ComponentState) then begin
350 if (Value <> FNotifyWhenDone) then
351 FNotifyWhenDone := Value;
352 end;
353 end;
354
355
356 procedure TStSpawnApplication.SetRunParameters(const Value : String); {!!.02}
357 begin
358 if (FAllowChange) or (csDesigning in ComponentState) then begin
359 if (Value <> FRunParameters) then
360 FRunParameters := Value;
361 end;
362 end;
363
364
365 procedure TStSpawnApplication.SetOnCompleted(Value : TStSpawnCompletedEvent);
366 begin
367 if (FAllowChange) or (csDesigning in ComponentState) then
368 FOnCompleted := Value;
369 end;
370
371
372 procedure TStSpawnApplication.SetOnSpawnError(Value : TStSpawnErrorEvent);
373 begin
374 if (FAllowChange) or (csDesigning in ComponentState) then
375 FOnSpawnError := Value;
376 end;
377
378
379 procedure TStSpawnApplication.SetShowState(Value : TStShowState);
380 begin
381 if (FAllowChange) or (csDesigning in ComponentState) then begin
382 if (Value <> FShowState) then
383 FShowState := Value;
384 end;
385 end;
386
387
388 procedure TStSpawnApplication.SetSpawnCommand(Value : TStSpawnCommand);
389 begin
390 if (FAllowChange) or (csDesigning in ComponentState) then begin
391 if (Value <> FSpawnCommand) then
392 FSpawnCommand := Value;
393 end;
394 end;
395
396
397 procedure TStSpawnApplication.SetSpawnTimeOut(Value : TStSpawnTimeOutEvent);
398 begin
399 if (FAllowChange) or (csDesigning in ComponentState) then
400 FOnSpawnTimeOut := Value;
401 end;
402
403
404 procedure TStSpawnApplication.SetTimeOut(Value : Longint);
405 begin
406 if (FAllowChange) or (csDesigning in ComponentState) then begin
407 if (Value <> FTimeOut) and (Value >= 0) then
408 FTimeOut := Value;
409 end;
410 end;
411
412
413 end.

  ViewVC Help
Powered by ViewVC 1.1.20