-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHStringVisualizer.pas
More file actions
341 lines (285 loc) · 10.3 KB
/
Copy pathHStringVisualizer.pas
File metadata and controls
341 lines (285 loc) · 10.3 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
335
336
337
338
339
340
341
unit HStringVisualizer;
{
Delphi Debug Visualizer for HSTRING (Windows Runtime string handle).
Created by DelphiCoder, 2026
Made with the help of Claude AI, GExperts' and CnPack's debug visualizer source code
Published under the MIT License
https://github.com/TheDelphiCoder/HSTRING-Debug-Visualizer
}
interface
uses
System.SysUtils,
System.Classes,
System.SyncObjs,
ToolsAPI;
type
THStringVisualizer = class(TInterfacedObject,
IOTADebuggerVisualizer,
IOTADebuggerVisualizer250,
IOTADebuggerVisualizerValueReplacer,
IOTAThreadNotifier,
IOTAThreadNotifier160)
private
type
TDeferredRec = record
Result: string;
{$IF CompilerVersion >= 23.0} // Delphi XE2 introduced TOTAAddress
ResultAddress: TOTAAddress;
{$ELSE}
ResultAddress: LongWord;
{$IFEND}
ResultSize: LongWord;
end;
private
FCompleted: Boolean;
FDeferred: TDeferredRec;
FLastKnownRawValue: string;
public
constructor Create;
destructor Destroy; override;
{ IOTADebuggerVisualizer }
function GetSupportedTypeCount: Integer;
procedure GetSupportedType(Index: Integer; var TypeName: string; var AllDescendants: Boolean); overload;
function GetVisualizerIdentifier: string;
function GetVisualizerName: string;
function GetVisualizerDescription: string;
{ IOTADebuggerVisualizer250 — adds generic/template support }
procedure GetSupportedType(Index: Integer; var TypeName: string; var AllDescendants: Boolean; var IsGeneric: Boolean); overload;
{ IOTADebuggerVisualizerValueReplacer }
function GetReplacementValue(const Expression, TypeName, EvalResult: string): string;
{ IOTAThreadNotifier }
procedure EvaluteComplete(const ExprStr, ResultStr: string; CanModify: Boolean;
ResultAddress, ResultSize: LongWord; ReturnCode: Integer);
procedure ModifyComplete(const ExprStr, ResultStr: string; ReturnCode: Integer);
procedure ThreadNotify(Reason: TOTANotifyReason);
procedure AfterSave;
procedure BeforeSave;
procedure Destroyed;
procedure Modified;
{ IOTAThreadNotifier160 — 64-bit address overload }
{$IF CompilerVersion >= 23.0} // Delphi XE2 introduced IOTAThreadNotifier160
{ ---- IOTAThreadNotifier160 ---- }
///<sumary>
/// This is called when an evaluate call that returned erDeferred completes.
/// That happens e.g. when the evaluation involves a function call.
/// Sets the FDeferred fields.
/// @param ReturnCode <> 0 if error </summary>
/// @NOTE: Evaluate is called in the TryEvaluate method. TryEvaluate also waits for
/// EvaluateComplete to be called and handles the FDeferred fields.
procedure EvaluateComplete(const ExprStr, ResultStr: string; CanModify: Boolean;
ResultAddress: TOTAAddress; ResultSize: LongWord; ReturnCode: Integer); overload;
{$IFEND}
///<summary>
/// This is called when an evaluate that returned erDeferred completes.
/// Calls the overloaded EvaluateComplete method of IOTAThreadNotifier160,
/// passing on the parameters and converting the ResultAddress to TOTAAddress
/// @param ReturnCode <> 0 if error </summary>
procedure EvaluateComplete(const ExprStr, ResultStr: string; CanModify: Boolean;
ResultAddress, ResultSize: Cardinal; ReturnCode: Integer); overload;
end;
procedure Register;
implementation
// ---------------------------------------------------------------------------
// Package registration
// ---------------------------------------------------------------------------
procedure Register;
begin
// Components are not registered here; the visualizer registers itself
// in the initialization section via the ToolsAPI.
end;
// ---------------------------------------------------------------------------
// THStringVisualizer
// ---------------------------------------------------------------------------
constructor THStringVisualizer.Create;
begin
inherited;
end;
destructor THStringVisualizer.Destroy;
begin
inherited;
end;
// -- IOTADebuggerVisualizer --------------------------------------------------
function THStringVisualizer.GetSupportedTypeCount: Integer;
begin
Result := 2;
end;
procedure THStringVisualizer.GetSupportedType(Index: Integer;
var TypeName: string; var AllDescendants: Boolean);
begin
AllDescendants := False;
case Index of
0: TypeName := 'HSTRING';
1: TypeName := '__HSTRING__';
end;
end;
function THStringVisualizer.GetVisualizerIdentifier: string;
begin
Result := Classname;
end;
function THStringVisualizer.GetVisualizerName: string;
begin
Result := 'HSTRING Visualizer by DelphiCoder';
end;
function THStringVisualizer.GetVisualizerDescription: string;
begin
Result := 'Displays the content of a Windows Runtime HSTRING handle';
end;
// -- IOTADebuggerVisualizer250 -----------------------------------------------
procedure THStringVisualizer.GetSupportedType(Index: Integer;
var TypeName: string; var AllDescendants, IsGeneric: Boolean);
begin
IsGeneric := False;
GetSupportedType(Index, TypeName, AllDescendants); // delegate to v1
end;
// -- IOTADebuggerVisualizerValueReplacer -------------------------------------
function THStringVisualizer.GetReplacementValue(const Expression, TypeName, EvalResult: string): string;
var
CurProcess: IOTAProcess;
CurThread: IOTAThread;
NotifierIndex: Integer;
CanModify: Boolean;
ResultBuf: array[0..4095] of Char;
ResultAddr: TOTAAddress;
ResultSize: LongWord;
ResultVal: LongWord;
DebugSvcs: IOTADebuggerServices;
EvalRes: TOTAEvaluateResult;
Expr: string;
begin
FLastKnownRawValue := EvalResult;
Result := EvalResult; // safe fallback — show the raw handle value
if (EvalResult = '0')
or (EvalResult = 'nil')
or (EvalResult = '$0')
or (EvalResult = '$00000000')
or (EvalResult = '$0000000000000000')
then
begin
Result := '(empty HSTRING)';
Exit;
end;
if not Supports(BorlandIDEServices, IOTADebuggerServices, DebugSvcs) then
Exit;
CurProcess := DebugSvcs.CurrentProcess;
if CurProcess = nil then
Exit;
CurThread := CurProcess.CurrentThread;
if CurThread = nil then
Exit;
// EvalResult contains the handle value as a hex string e.g. '$1A2B3C4D'.
// We use the raw handle value from EvalResult, not Expression, because
// Expression may be a complex lvalue that the evaluator re-evaluates.
Expr := Format('PWideChar(WindowsGetStringRawBuffer(HSTRING(%s), nil))', [EvalResult]);
FCompleted := False;
FDeferred.Result := '';
// Register the notifier BEFORE calling Evaluate so the callback
// cannot be missed if evaluation completes synchronously.
NotifierIndex := CurThread.AddNotifier(Self);
try
FillChar(ResultBuf, SizeOf(ResultBuf), 0);
EvalRes := CurThread.Evaluate(
Expr,
@ResultBuf[0],
Length(ResultBuf),
CanModify,
eseAll,
'',
ResultAddr,
ResultSize,
ResultVal,
'',
0);
case EvalRes of
erOK:
begin
// Result already in the buffer — no need to wait
if ResultBuf[0] <> #0 then
Result := string(PChar(@ResultBuf[0]))
else
Result := '(empty)';
end;
erDeferred:
begin
// Block until EvaluateComplete fires
while not FCompleted do
DebugSvcs.ProcessDebugEvents;
if FDeferred.Result <> '' then
Result := FDeferred.Result
else
Result := Format('HSTRING($%s) <deferred eval returned empty>', [EvalResult]);
end;
erError:
Result := Format('HSTRING($%s) <eval error>', [EvalResult]);
end;
finally
CurThread.RemoveNotifier(NotifierIndex);
end;
end;
// -- IOTAThreadNotifier ------------------------------------------------------
// Note: 'EvaluteComplete' is the original spelling in the ToolsAPI interface
// (a typo that Embarcadero kept for backward compatibility). Newer versions
// also expose 'EvaluateComplete' (correctly spelled) via IOTAThreadNotifier160.
procedure THStringVisualizer.EvaluteComplete(const ExprStr, ResultStr: string; CanModify: Boolean;
ResultAddress, ResultSize: LongWord; ReturnCode: Integer);
begin
EvaluateComplete(ExprStr, ResultStr, CanModify, ResultAddress, ResultSize, ReturnCode);
end;
procedure THStringVisualizer.EvaluateComplete(const ExprStr, ResultStr: string; CanModify: Boolean;
ResultAddress, ResultSize: Cardinal; ReturnCode: Integer);
begin
{$IF CompilerVersion >= 23.0}
EvaluateComplete(ExprStr, ResultStr, CanModify, TOTAAddress(ResultAddress), LongWord(ResultSize), ReturnCode);
end;
procedure THStringVisualizer.EvaluateComplete(const ExprStr, ResultStr: string; CanModify: Boolean;
ResultAddress: TOTAAddress; ResultSize: LongWord; ReturnCode: Integer);
begin
{$IFEND}
if ReturnCode = 0 then
begin
FDeferred.Result := ResultStr;
FDeferred.ResultAddress := ResultAddress;
FDeferred.ResultSize := ResultSize;
end
else
begin
FDeferred.ResultAddress := 0;
FDeferred.ResultSize := 0;
end;
FCompleted := True;
end;
procedure THStringVisualizer.ModifyComplete(const ExprStr, ResultStr: string; ReturnCode: Integer);
begin
end;
procedure THStringVisualizer.ThreadNotify(Reason: TOTANotifyReason);
begin
end;
procedure THStringVisualizer.AfterSave;
begin
end;
procedure THStringVisualizer.BeforeSave;
begin
end;
procedure THStringVisualizer.Destroyed;
begin
end;
procedure THStringVisualizer.Modified;
begin
end;
// ---------------------------------------------------------------------------
// Package lifecycle
// ---------------------------------------------------------------------------
var
GVisualizer: IOTADebuggerVisualizer;
initialization
if Supports(BorlandIDEServices, IOTADebuggerServices) then
begin
GVisualizer := THStringVisualizer.Create;
(BorlandIDEServices as IOTADebuggerServices).RegisterDebugVisualizer(GVisualizer);
end;
finalization
if Supports(BorlandIDEServices, IOTADebuggerServices) then
begin
(BorlandIDEServices as IOTADebuggerServices).UnregisterDebugVisualizer(GVisualizer);
GVisualizer := nil;
end;
end.