Repository navigation
Expand file tree
/
Copy pathSystem.PSRWrapper.pas
More file actions
334 lines (279 loc) · 9.1 KB
/
Copy pathSystem.PSRWrapper.pas
File metadata and controls
334 lines (279 loc) · 9.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
unit System.PSRWrapper;
interface
{$REGION 'Info'}
{
psr.exe
[/start |/stop]
[/output ]
[/sc (0|1)]
[/maxsc ]
[/sketch (0|1)]
[/slides (0|1)]
[/gui (o|1)]
[/arcetl (0|1)]
[/arcxml (0|1)]
[/arcmht (0|1)]
[/stopevent ]
[/maxlogsize ]
[/recordpid ]
Command Line Parameters für PSR:
/start : Start Recording. (Outputpath flag SHOULD be specified) /stop : Stop Recording.
/sc : Capture screenshots for recorded steps.
/maxsc : Maximum number of recent screen captures.
/maxlogsize : Maximum log file size (in MB) before wrapping occurs.
/gui : Display control GUI.
/arcetl : Include raw ETW file in archive output.
/arcxml : Include MHT file in archive output.
/recordpid : Record all actions associated with given PID.
/sketch : Sketch UI if no screenshot was saved.
/slides : Create slide show HTML pages.
/output : Store output of record session in given path.
/stopevent : Event to signal after output files are generated.
PSR.EXE /START /OUTPUT "C:\Users\nt\AppData\Local\Temp\_nt.zip" /SC 1 /MAXSC 999 /GUI 1 /arcetl 1 /arcxml 1
PSR.EXE /START /OUTPUT "C:\Temp\002\_nt.zip" /SC 1 /MAXSC 999 /GUI 1 /arcetl 1 /arcxml 1 /slides 1 /sketch 1
PSR.EXE /START /OUTPUT "C:\Temp\002\_nt.zip" /SC 1 /MAXSC 999 /GUI 0
PSR.EXE /STOP
}
{$ENDREGION}
type
IPSRWrapper = interface(IInvokable)
['{606699DF-A2E2-4594-8E43-F9ED3E81F0FF}']
function CaptureScreenshotsForRecordedSteps(Value: Boolean): IPSRWrapper; overload;
function CaptureScreenshotsForRecordedSteps(): Boolean; overload;
function MaximumNumberOfRecentScreenCaptures(Value: Integer): IPSRWrapper; overload;
function MaximumNumberOfRecentScreenCaptures(): Integer; overload;
function DisplayControlGUI(Value: Boolean): IPSRWrapper; overload;
function DisplayControlGUI(): Boolean; overload;
function IncludeETWAndXML(Value: Boolean): IPSRWrapper; overload;
function IncludeETWAndXML(): Boolean; overload;
function Output(): string;
function Start(): IPSRWrapper;
function Stop(): IPSRWrapper;
function IsRecording: Boolean;
end;
TPSRWrapper = class(TInterfacedObject, IPSRWrapper)
strict private
// FProcessHandle: THandle;
FIsRecording: Boolean;
FCaptureScreenshotsForRecordedSteps: Boolean;
FMaximumNumberOfRecentScreenCaptures: Integer;
FDisplayControlGUI: Boolean;
FIncludeETWAndXML: Boolean;
FOutput: String;
private
function ProcessExists(): Boolean;
procedure ExecuteProgAndWait(const AParams: string);
procedure SleepWithoutFreezeApp(msec: Int64);
public
constructor Create();
destructor Destroy; override;
function CaptureScreenshotsForRecordedSteps(Value: Boolean): IPSRWrapper; overload;
function CaptureScreenshotsForRecordedSteps(): Boolean; overload;
function MaximumNumberOfRecentScreenCaptures(Value: Integer): IPSRWrapper; overload;
function MaximumNumberOfRecentScreenCaptures(): Integer; overload;
function DisplayControlGUI(Value: Boolean): IPSRWrapper; overload;
function DisplayControlGUI(): Boolean; overload;
function IncludeETWAndXML(Value: Boolean): IPSRWrapper; overload;
function IncludeETWAndXML(): Boolean; overload;
function Output(): string;
function Start(): IPSRWrapper;
function Stop(): IPSRWrapper;
function IsRecording: Boolean;
end;
implementation
uses
System.SysUtils,
System.StrUtils,
System.IOUtils
{$IFDEF MSWINDOWS}
,
VCL.Forms,
Winapi.Windows,
WinApi.ShellAPI,
Winapi.TlHelp32
{$ENDIF}
;
{ TPSRWrapper }
constructor TPSRWrapper.Create;
begin
inherited Create();
FCaptureScreenshotsForRecordedSteps := True;
FMaximumNumberOfRecentScreenCaptures := 999;
FDisplayControlGUI := False;
FOutput := String.Empty;
FIsRecording := False;
// FProcessHandle := 0;
end;
destructor TPSRWrapper.Destroy;
begin
//...
inherited;
end;
function TPSRWrapper.CaptureScreenshotsForRecordedSteps(Value: Boolean): IPSRWrapper;
begin
FCaptureScreenshotsForRecordedSteps := Value;
Result := Self;
end;
function TPSRWrapper.CaptureScreenshotsForRecordedSteps: Boolean;
begin
Result := FCaptureScreenshotsForRecordedSteps;
end;
function TPSRWrapper.DisplayControlGUI: Boolean;
begin
Result := FDisplayControlGUI;
end;
function TPSRWrapper.DisplayControlGUI(Value: Boolean): IPSRWrapper;
begin
FDisplayControlGUI := Value;
Result := Self;
end;
function TPSRWrapper.MaximumNumberOfRecentScreenCaptures: Integer;
begin
Result := FMaximumNumberOfRecentScreenCaptures;
end;
function TPSRWrapper.MaximumNumberOfRecentScreenCaptures(Value: Integer): IPSRWrapper;
begin
FMaximumNumberOfRecentScreenCaptures := Value;
Result := Self;
end;
function TPSRWrapper.Output: string;
begin
Result := FOutput;
end;
function TPSRWrapper.ProcessExists(): Boolean;
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
Result := False;
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) = 'PSR.EXE')
or (UpperCase(FProcessEntry32.szExeFile) = 'PSR.EXE')) then
begin
Exit(True);
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;
{$REGION 'Tests'}
//function TPSRWrapper.ExecuteProgAndWait(const AParam: string): Boolean;
//var
// SEInfo: TShellExecuteInfo;
// ExitCode: DWORD;
// Return: Boolean;
//begin
// FillChar(SEInfo, SizeOf(SEInfo), #0);
// SEInfo.LpVerb := 'open';
// SEInfo.cbSize := SizeOf(TShellExecuteInfo);
// SEInfo.fMask := SEE_MASK_NOCLOSEPROCESS; // Tell ShellExecuteEx to keep the process handle open
// // SEInfo.hProcess
// // SEInfo.Wnd := Application.Handle;
// // SEInfo.Wnd := Handle;
// SEInfo.LpFile := PChar('PSR.EXE');
// SEInfo.lpParameters := PChar(AParam);
// // SEInfo.nShow := SW_SHOWNORMAL;
// // SEInfo.LpDirectory := PChar(TPath.GetDirectoryName('PSR.EXE'));
// Return := WinApi.ShellAPI.ShellExecuteEx(@SEInfo);
//
//
// if (Return) and (SEInfo.HProcess <> 0) then
// begin
// WaitForSingleObject(SEInfo.HProcess, INFINITE);
// GetExitCodeProcess(SEInfo.HProcess, ExitCode);
// CloseHandle(SEInfo.HProcess);
// end;
//
//// if (Return and (SEInfo.HProcess <> 0)) then
//// begin
//// repeat
//// GetExitCodeProcess(SEInfo.hProcess, ExitCode);
//// until (ExitCode <> STILL_ACTIVE); // or Application.Terminated;
////
//// // FProcessHandle := SEInfo.HProcess;
//// CloseHandle(SEInfo.HProcess);
//// Exit(true);
//// end
//// else
//// begin
//// Exit(False);
//// end;
//end;
{$ENDREGION}
procedure TPSRWrapper.ExecuteProgAndWait(const AParams: string);
var
{$IFDEF MSWINDOWS} Handle: HWND; {$ENDIF}
Return: Cardinal;
begin
{$IFDEF MSWINDOWS}
Handle := Application.Handle;
if Assigned(Application.MainForm) then
Handle := Application.MainForm.Handle;
Return := WinApi.ShellAPI.ShellExecute(Handle, 'open', 'PSR.EXE', PChar(AParams), nil, SW_SHOWNORMAL);
if (Return <= 32) then
begin
RaiseLastOSError();
end;
{$ELSE}
raise ENotImplemented.Create('WinApi.ShellAPI.ShellExecute not implemented.');
{$ENDIF}
end;
function TPSRWrapper.IncludeETWAndXML: Boolean;
begin
Result := FIncludeETWAndXML;
end;
function TPSRWrapper.IncludeETWAndXML(Value: Boolean): IPSRWrapper;
begin
FIncludeETWAndXML := Value;
Result := Self;
end;
function TPSRWrapper.IsRecording: Boolean;
begin
Result := (FIsRecording or ProcessExists());
end;
procedure TPSRWrapper.SleepWithoutFreezeApp(msec: Int64);
var
StartingValue, Elapsed: DWORD;
begin
StartingValue := GetTickCount;
Elapsed := 0;
repeat
if (MsgWaitForMultipleObjects(0, Pointer(nil)^, FALSE, msec-Elapsed, QS_ALLINPUT) <> WAIT_OBJECT_0) then
Break;
Application.ProcessMessages;
Elapsed := (GetTickCount - StartingValue);
until (Elapsed >= msec);
end;
function TPSRWrapper.Start: IPSRWrapper;
begin
if (ProcessExists()) then
Exit;
var ParamPSR: String := '';
FOutput := TPath.ChangeExtension(TPath.GetTempFileName, '.zip');
ParamPSR := '/START '
+ ' /SC ' + FCaptureScreenshotsForRecordedSteps.ToInteger.ToString
+ ' /GUI ' + FDisplayControlGUI.ToInteger.ToString
+ ' /MAXSC ' + FMaximumNumberOfRecentScreenCaptures.ToString
+ ' /OUTPUT "' + FOutput + '"';
if FIncludeETWAndXML then
begin
ParamPSR := ParamPSR + ' /ARCETL 1 /ARCXML 1 ';
end;
ExecuteProgAndWait(ParamPSR);
FIsRecording := True;
end;
function TPSRWrapper.Stop: IPSRWrapper;
begin
ExecuteProgAndWait('/stop');
while ProcessExists() do
begin
SleepWithoutFreezeApp(1000);
end;
FIsRecording := False;
end;
end.