-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathNtUiLib.Errors.Dialog.pas
More file actions
169 lines (138 loc) · 4.78 KB
/
NtUiLib.Errors.Dialog.pas
File metadata and controls
169 lines (138 loc) · 4.78 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
unit NtUiLib.Errors.Dialog;
interface
uses
Ntapi.WinUser, NtUtils;
const
DEFAULT_CROSS_SESSION_MESSAGE_TIMEOUT = 60; // sec
var
// Display stack traces in the error dialog, when available
DisplayStackTraces: Boolean = False;
// Show a modal error message dialog
function ShowNtxStatus(
ParentWnd: THwnd;
const Status: TNtxStatus
): TNtxStatus;
// Show a error message dialog to the interactive user
function ShowNtxStatusAlwaysInteractive(
const Status: TNtxStatus;
TimeoutSeconds: Cardinal = DEFAULT_CROSS_SESSION_MESSAGE_TIMEOUT
): TNtxStatus;
// Format a status message (without using reflection)
function UiLibVerboseStatusMessage(
const Status: TNtxStatus
): String;
implementation
uses
Ntapi.ntdef, NtUtils.SysUtils, NtUiLib.Errors, NtUiLib.TaskDialog,
NtUtils.DbgHelp, DelphiApi.Reflection,
DelphiUiLib.LiteReflection, Ntapi.ntstatus, Ntapi.WinError, Ntapi.ntseapi;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function UiLibVerboseStatusMessage;
var
i: Integer;
TypeFormatter: IRttixTypeFormatter;
begin
// LastCall: <function name>
Result := 'Last call: ' + RtlxStringOrDefault(Status.Location, '<unknown>');
if Status.LastCall.Parameter <> '' then
Result := Result + #$D#$A'Parameter: ' + Status.LastCall.Parameter;
case Status.LastCall.CallType of
// Desired access: <mask>
lcOpenCall:
if Assigned(Status.LastCall.AccessMaskType) then
begin
TypeFormatter := RttixMakeTypeFormatter(Status.LastCall.AccessMaskType);
Result := Result + #$D#$A'Desired ' +
RtlxStringOrDefault(TypeFormatter.RttixType.FriendlyName, 'object') +
' access: ' + TypeFormatter.FormatText(Status.LastCall.AccessMask);
end;
// Information class: <name>
lcQuerySetCall:
if Assigned(Status.LastCall.InfoClassType) then
begin
TypeFormatter := RttixMakeTypeFormatter(Status.LastCall.InfoClassType,
RttixPreserveEnumCase);
Result := Result + #$D#$A'Information class: ' +
TypeFormatter.FormatText(Status.LastCall.InfoClass);
end;
end;
// Expected <type> access: <mask>
if (Status.Status = STATUS_ACCESS_DENIED) or (Status.IsWin32 and
(Status.Win32Error = ERROR_ACCESS_DENIED)) then
for i := 0 to High(Status.LastCall.ExpectedAccess) do
if Assigned(Status.LastCall.ExpectedAccess[i].AccessMaskType) then
begin
TypeFormatter := RttixMakeTypeFormatter(
Status.LastCall.ExpectedAccess[i].AccessMaskType);
Result := Result + #$D#$A'Expected ' + RtlxStringOrDefault(
TypeFormatter.RttixType.FriendlyName, 'object') + ' access: ' +
TypeFormatter.FormatText(
Status.LastCall.ExpectedAccess[i].AccessMask);
end;
// Result: <STATUS_*/ERROR_*>
Result := Result + #$D#$A'Result: ' + Status.Name;
// <textual description>
Result := Result + #$D#$A#$D#$A + Status.Description;
// <privilege name>
if (Status.Status = STATUS_PRIVILEGE_NOT_HELD) or
(Status.IsWin32 and (Status.Win32Error = ERROR_PRIVILEGE_NOT_HELD)) then
if (Status.LastCall.ExpectedPrivilege >= SE_CREATE_TOKEN_PRIVILEGE) and
(Status.LastCall.ExpectedPrivilege <= High(TSeWellKnownPrivilege)) then
begin
TypeFormatter := RttixMakeTypeFormatter(TypeInfo(TSeWellKnownPrivilege));
RtlxSuffixStripString('.', Result, True);
Result := Result + ': "' + TypeFormatter.FormatText(
Status.LastCall.ExpectedPrivilege) + '"';
end;
// Stack trace
if DisplayStackTraces and (Length(Status.LastCall.StackTrace) > 0) then
Result := Result + #$D#$A#$D#$A'Stack Trace:'#$D#$A + SymxFormatStackTrace(
Status.LastCall.StackTrace);
end;
procedure RtlxpPrepareStatusMessage(
const Status: TNtxStatus;
out Icon: TDialogIcon;
out Title: String;
out Summary: String;
out Content: String
);
begin
if Status.IsHResult or NT_ERROR(Status.Status) then
begin
Icon := diError;
Title := 'Error';
end
else
begin
Icon := diWarning;
Title := 'Warning';
end;
// Make a pretty header
Summary := Status.Summary;
if Summary = '' then
Summary := 'System error';
Content := UiLibVerboseStatusMessage(Status);
end;
function ShowNtxStatus;
var
Icon: TDialogIcon;
Title, Summary, Content: String;
Response: TMessageResponse;
begin
RtlxpPrepareStatusMessage(Status, Icon, Title, Summary, Content);
Result := UsrxShowTaskDialogWithStatus(Response, ParentWnd, Title, Summary,
Content, Icon, dbOk, IDOK);
end;
function ShowNtxStatusAlwaysInteractive;
var
Icon: TDialogIcon;
Title, Summary, Content: String;
Response: TMessageResponse;
begin
RtlxpPrepareStatusMessage(Status, Icon, Title, Summary, Content);
Result := UsrxShowMessageAlwaysInteractiveWithStatus(Response, Title, Summary,
Content, Icon, dbOk, IDOK, TimeoutSeconds);
end;
end.