-
Notifications
You must be signed in to change notification settings - Fork 31
/
NtUtils.Console.pas
215 lines (173 loc) · 4.45 KB
/
NtUtils.Console.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
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
unit NtUtils.Console;
{
This module includes some functions that can help building console applications
}
interface
uses
NtUtils, DelphiApi.Reflection;
type
[NamingStyle(nsCamelCase, 'ch')]
TConsoleHostState = (
chUnknown,
chNone,
chInherited,
chCreated
);
[NamingStyle(nsCamelCase, 'cc')]
TConsoleColor = (
ccBlack,
ccDarkBlue,
ccDarkGreen,
ccDarkCyan,
ccDarkRed,
ccDarkMagenta,
ccDarkYellow,
ccGray,
ccDarkGray,
ccBlue,
ccGreen,
ccCyan,
ccRed,
ccMagenta,
ccYellow,
ccWhite,
ccUnchanged
);
var
// Allow using command-line parameters instead of user input
PreferParametersOverConsoleIO: Boolean = True;
// Do not immediately close the console if the app was invoked from GUI
UseSmartCloseOnExit: Boolean = True;
// Read a string input from the console
function ReadString(AllowEmpty: Boolean = True): String;
// Read a boolean choice from the console
function ReadBoolean: Boolean;
// Read an unsigned integer from the console
function ReadCardinal(
MinValue: Cardinal = 0;
MaxValue: Cardinal = Cardinal(-1)
): Cardinal;
// Change console output color and revert it back later
function RtlxSetConsoleColor(
Foreground: TConsoleColor;
Background: TConsoleColor = ccUnchanged
): IAutoReleasable;
// Determine whether the current process inherited or created the console
function RtlxConsoleHostState: TConsoleHostState;
implementation
uses
Ntapi.WinNt, Ntapi.ntpsapi, Ntapi.ConsoleApi, NtUtils.SysUtils,
NtUtils.Processes, NtUtils.Processes.Info;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
const
RETRY_MSG = 'Invalid input; try again: ';
{ Input }
var
OverrideIndex: Integer = 1;
function ReadString;
begin
// Apply I/O override
if PreferParametersOverConsoleIO then
begin
Result := ParamStr(OverrideIndex);
Inc(OverrideIndex);
if Result <> '' then
begin
writeln(Result);
Exit;
end
else
PreferParametersOverConsoleIO := False;
end;
repeat
readln(Result);
if not AllowEmpty and (Result = '') then
write(RETRY_MSG)
else
Break;
until False;
end;
function ReadBoolean;
begin
Result := RtlxCompareStrings(ReadString, 'y') = 0;
end;
function ReadCardinal;
begin
while not RtlxStrToUInt(ReadString(False), Result) or (Result > MaxValue) or
(Result < MinValue) do
begin
write(RETRY_MSG);
PreferParametersOverConsoleIO := False; // Failed to parse, need user interaction
end;
end;
{ Output }
function RtlxSetConsoleColor;
var
Info: TConsoleScreenBufferInfo;
hConsole: THandle;
NewAttributes: Word;
begin
if (Foreground = ccUnchanged) and (Background = ccUnchanged) then
Exit(nil);
hConsole := GetStdHandle(STD_OUTPUT_HANDLE);
if hConsole = INVALID_HANDLE_VALUE then
Exit(nil);
if not GetConsoleScreenBufferInfo(hConsole, Info) then
Exit(nil);
NewAttributes := Info.Attributes;
if Foreground <> ccUnchanged then
NewAttributes := (NewAttributes and $FFF0) or (Word(Foreground) and $F);
if Background <> ccUnchanged then
NewAttributes := (NewAttributes and $FF0F) or
((Word(Background) and $F) shl 4);
if not SetConsoleTextAttribute(hConsole, NewAttributes) then
Exit(nil);
Result := Auto.Delay(
procedure
var
hConsole: THandle;
begin
hConsole := GetStdHandle(STD_OUTPUT_HANDLE);
if hConsole <> INVALID_HANDLE_VALUE then
SetConsoleTextAttribute(hConsole, Info.Attributes);
end
);
end;
{ Console Host }
function RtlxConsoleHostState;
var
PID: TProcessId;
hxProcess: IHandle;
ConhostInfo, OurInfo: TKernelUserTimes;
begin
Result := chUnknown;
// Determine conhost's PID
if not NtxProcess.Query(NtCurrentProcess, ProcessConsoleHostProcess, PID)
.IsSuccess then
Exit;
if PID = 0 then
Exit(chNone);
// Query its and our creation time
if NtxOpenProcess(hxProcess, PID, PROCESS_QUERY_LIMITED_INFORMATION).IsSuccess
and NtxProcess.Query(hxProcess.Handle, ProcessTimes, ConhostInfo).IsSuccess
and NtxProcess.Query(NtCurrentProcess, ProcessTimes, OurInfo).IsSuccess then
begin
// Compare them
if ConhostInfo.CreateTime > OurInfo.CreateTime then
Result := chCreated
else
Result := chInherited;
end;
end;
initialization
finalization
{$IFDEF Console}
if UseSmartCloseOnExit and (RtlxConsoleHostState = chCreated) then
begin
write(#$D#$A'Press enter to exit...');
readln;
end;
{$ENDIF}
end.