-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathNtUiLib.Errors.Dialog.pas
109 lines (88 loc) · 2.61 KB
/
NtUiLib.Errors.Dialog.pas
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
unit NtUiLib.Errors.Dialog;
interface
uses
Ntapi.WinUser, NtUtils;
const
DEFAULT_CROSS_SESSION_MESSAGE_TIMEOUT = 60; // sec
// 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 NtxVerboseStatusMessageNoReflection(
const Status: TNtxStatus
): String;
var
// A custom status formatter (provided by NtUiLib.Exceptions.Dialog)
NtxVerboseStatusMessageFormatter: function (const Status: TNtxStatus): String;
implementation
uses
Ntapi.ntdef, NtUtils.SysUtils, NtUiLib.Errors, NtUiLib.TaskDialog;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
function NtxVerboseStatusMessageNoReflection;
begin
// LastCall: <function name>
Result := 'Last call: ' + RtlxStringOrDefault(Status.Location, '<unknown>');
if Status.LastCall.Parameter <> '' then
Result := Result + #$D#$A'Parameter: ' + Status.LastCall.Parameter;
// Result: <STATUS_*/ERROR_*>
Result := Result + #$D#$A'Result: ' + Status.Name;
// <textual description>
Result := Result + #$D#$A#$D#$A + Status.Description;
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';
if Assigned(NtxVerboseStatusMessageFormatter) then
Content := NtxVerboseStatusMessageFormatter(Status)
else
Content := NtxVerboseStatusMessageNoReflection(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.