forked from Sovos-Compliance/convey-public-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
/
CnvSysUtils.pas
379 lines (340 loc) · 8.86 KB
/
CnvSysUtils.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
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
unit CnvSysUtils;
interface
uses
Classes, Windows;
type
TSystemState = class
private
KeyState: TKeyboardState;
FDisableGetState: Boolean;
function GetControlPressed: Boolean;
function GetAltPressed: Boolean;
function GetShiftPressed: Boolean;
function GetLButtonPressed: Boolean;
function GetMButtonPressed: Boolean;
function GetRButtonPressed: Boolean;
function GetShiftState(State : TShiftState): Boolean;
function GetInsert: Boolean;
function GetCapsLock: Boolean;
function GetNumLock: Boolean;
function GetScrollLock: Boolean;
function GetFullState: Word;
public
property ControlPressed: Boolean read GetControlPressed;
property AltPressed: Boolean read GetAltPressed;
property ShiftPressed: Boolean read GetShiftPressed;
property LButtonPressed: Boolean read GetLButtonPressed;
property MButtonPressed: Boolean read GetMButtonPressed;
property RButtonPressed: Boolean read GetRButtonPressed;
property Insert: Boolean read GetInsert;
property CapsLock: Boolean read GetCapsLock;
property NumLock: Boolean read GetNumLock;
property ScrollLock: Boolean read GetScrollLock;
property FullState: Word read GetFullState;
end;
procedure GetAllIPs(IPs: TStrings);
function GetFullyQualifiedDomainName : AnsiString;
function GetComputerName: string;
function GetUserName(const ANameFormat: integer = -1): string;
function GetWindowsDomain: string;
function IsForegroundTask: Boolean;
function GetProcessIDOfHInstance(AHInstance : Cardinal): Cardinal; // This FN doesn't seam to work on NT based systems. Don't use
var
SystemState : TSystemState;
implementation
uses
SysUtils, Forms, Winsock;
type
PCheckTaskInfo = ^TCheckTaskInfo;
TCheckTaskInfo = record
FocusWnd: HWnd;
Found: Boolean;
end;
function GetUserNameEx(NameFormat: Integer; lpNameBuffer: PChar; var lpnSize: Cardinal): BOOL; stdcall;
external 'Secur32.dll' name {$IFDEF UNICODE}'GetUserNameExW'{$ELSE}'GetUserNameExA'{$ENDIF};
procedure GetAllIPs(IPs: TStrings);
type
TaPInAddr = array [0..20] of PInAddr;
PaPInAddr = ^TaPInAddr;
var
phe : PHostEnt;
pptr : PaPInAddr;
Buffer : array [0..MAX_PATH] of AnsiChar;
I : Integer;
GInitData : TWSADATA;
begin
WSAStartup($101, GInitData);
try
GetHostName(Buffer, SizeOf(Buffer));
phe := GetHostByName(buffer);
if phe = nil
then Exit;
pptr := PaPInAddr(Phe^.h_addr_list);
I := 0;
while pptr^[I] <> nil do
begin
IPs.add(StrPas(inet_ntoa(pptr^[I]^)));
Inc(I);
end;
finally
WSACleanup;
end;
end;
function GetFullyQualifiedDomainName: AnsiString;
var
HostEnt: PHostEnt;
szHostname: array[0..MAX_PATH] of AnsiChar;
begin
Result:= '';
if GetHostName (szHostname, MAX_PATH) = 0
then
begin
HostEnt:= GetHostByName (szHostname);
if Assigned (HostEnt)
then Result := StrPas (HostEnt^.h_name);
end;
end;
// Delphi declares MAX_COMPUTERNAME_LENGTH as 15, but really it should be 16
function GetComputerName: string;
var lpBuffer: array[0..MAX_COMPUTERNAME_LENGTH + 1] of char;
dwSize: DWORD;
begin
dwSize:= MAX_COMPUTERNAME_LENGTH + 1;
if not Windows.GetComputerName(@lpBuffer, dwSize) then
raise Exception.Create(SysErrorMessage(GetLastError));
Result:= StrPas(lpBuffer);
end;
function GetUserName(const ANameFormat: integer = -1): string;
var lpBuffer: array[0..MAX_PATH] of char;
dwSize: DWORD;
begin
dwSize:= MAX_PATH;
if ANameFormat > -1 then
if not GetUserNameEx(ANameFormat, lpBuffer, dwSize) then
raise Exception.Create(SysErrorMessage(GetLastError()))
else
else
if not windows.GetUserName(lpBuffer, dwSize) then
raise Exception.Create(SysErrorMessage(GetLastError()));
Result:= StrPas(lpBuffer);
end;
function GetWindowsDomain: string;
var
psnuType:DWord;
lpszDomain:Array[0..MAX_PATH] Of Char;
UserSID:Array[0..1024] Of Char;
dwDomainLength:DWORD;
dwSIDBuffSize:DWORD;
lpszUserName:Array[0..MAX_PATH] Of Char;
begin
dwDomainLength:=MAX_PATH;
dwSIDBuffSize:=1024;
StrPCopy(lpszUserName, GetUserName);
If Not LookupAccountName (nil,lpszUserName,@UserSID,dwSIDBuffSize,lpszDomain,dwDomainLength,psnuType)
then Result := ''
else
begin
Result := StrPas (lpszDomain);
end;
end;
function CheckTaskWindow(Window: HWnd; Data: Longint): WordBool;
{$IFDEF MSWINDOWS} stdcall {$ELSE} export {$ENDIF};
begin
Result := True;
if PCheckTaskInfo(Data)^.FocusWnd = Window then begin
Result := False;
PCheckTaskInfo(Data)^.Found := True;
end;
end;
function IsForegroundTask: Boolean;
var
Info: TCheckTaskInfo;
{$IFNDEF MSWINDOWS}
Proc: TFarProc;
{$ENDIF}
begin
Info.FocusWnd := GetActiveWindow;
Info.Found := False;
{$IFDEF MSWINDOWS}
EnumThreadWindows(GetCurrentThreadID, @CheckTaskWindow, Longint(@Info));
{$ELSE}
Proc := MakeProcInstance(@CheckTaskWindow, HInstance);
try
EnumTaskWindows(GetCurrentTask, Proc, Longint(@Info));
finally
FreeProcInstance(Proc);
end;
{$ENDIF}
Result := Info.Found;
end;
type
PHANDLE_TABLE_ENTRY = ^HANDLE_TABLE_ENTRY;
HANDLE_TABLE_ENTRY = record
flags : Cardinal;
pObject : Cardinal;
end;
PHANDLE_TABLE = ^HANDLE_TABLE;
HANDLE_TABLE = record
cEntries : Cardinal;
handles : array [0..0] of HANDLE_TABLE_ENTRY;
end;
threadvar
Magic : Cardinal;
function InitMagic : Cardinal; forward;
function GetProcessIDOfHInstance(AHInstance : Cardinal): Cardinal;
type
PCardinal = ^cardinal;
var
ProcessID : Cardinal;
dw : Cardinal;
pdw : PCardinal;
phTable : PHANDLE_TABLE;
hEntry : PHANDLE_TABLE_ENTRY;
begin
if Magic = 0
then Magic := InitMagic;
ProcessID := GetCurrentProcessId; // Process Id of the calling process
dw := ProcessID xor Magic;
pdw := PCardinal (dw + $44); // Offset to the handle table that belongs to the calling process
phTable := PHANDLE_TABLE (pdw^);
hEntry := @phTable^.handles;
hEntry := PHANDLE_TABLE_ENTRY (cardinal (hEntry) + AHInstance); // Index into the handle table
ProcessID := hEntry^.pObject xor Magic;
Result := ProcessId;
end;
function TSystemState.GetControlPressed: Boolean;
begin
Result := GetShiftState ([ssCtrl]);
end;
function TSystemState.GetAltPressed: Boolean;
begin
Result := GetShiftState ([ssAlt]);
end;
function TSystemState.GetShiftPressed: Boolean;
begin
Result := GetShiftState ([ssShift]);
end;
function TSystemState.GetLButtonPressed: Boolean;
begin
Result := GetShiftState ([ssLeft]);
end;
function TSystemState.GetMButtonPressed: Boolean;
begin
Result := GetShiftState ([ssMiddle]);
end;
function TSystemState.GetRButtonPressed: Boolean;
begin
Result := GetShiftState ([ssRight]);
end;
function TSystemState.GetShiftState(State : TShiftState): Boolean;
var
ShiftState : TShiftState;
begin
if not FDisableGetState
then GetKeyboardState(KeyState);
ShiftState := KeyboardStateToShiftState (KeyState);
Result := State * ShiftState = State;
end;
function TSystemState.GetInsert: Boolean;
begin
if not FDisableGetState
then GetKeyboardState(KeyState);
Result := KeyState [VK_INSERT] = 1;
end;
function TSystemState.GetCapsLock: Boolean;
begin
if not FDisableGetState
then GetKeyboardState(KeyState);
Result := KeyState [VK_CAPITAL] = 1;
end;
function TSystemState.GetNumLock: Boolean;
begin
if not FDisableGetState
then GetKeyboardState(KeyState);
Result := KeyState [VK_NUMLOCK] = 1;
end;
function TSystemState.GetScrollLock: Boolean;
begin
if not FDisableGetState
then GetKeyboardState(KeyState);
Result := KeyState [VK_SCROLL] = 1;
end;
function TSystemState.GetFullState: Word;
begin
Result := 0;
if ControlPressed
then Inc (Result, 1);
FDisableGetState := True;
try
if AltPressed
then Inc (Result, 2);
if ShiftPressed
then Inc (Result, 4);
if LButtonPressed
then Inc (Result, 8);
if MButtonPressed
then Inc (Result, 16);
if RButtonPressed
then Inc (Result, 32);
if Insert
then Inc (Result, 64);
if CapsLock
then Inc (Result, 128);
if NumLock
then Inc (Result, 256);
if ScrollLock
then Inc (Result, 512);
finally
FDisableGetState := False;
end;
end;
{$IFDEF MSWINDOWS}
{$IFDEF WIN32}
function InitMagic : Cardinal;
var
tid : cardinal;
AMagic : Cardinal;
begin
tid := GetCurrentThreadId;
asm
push ax
push es
push fs
mov ax, fs
mov es, ax
mov eax, 18h
mov eax, es:[eax]
sub eax, 10h
xor eax, [tid]
mov [AMagic], eax
pop fs
pop es
pop ax
end;
Result := AMagic;
end;
{$ENDIF}
{$IFDEF WIN64}
function InitMagic : Cardinal;
var
tid : cardinal;
AMagic : Cardinal;
asm
call GetCurrentThreadId
mov tid,eax
push gs
mov eax, 18h
mov eax, gs:[eax]
sub eax, 10h
xor eax, [tid]
mov [AMagic], eax
pop gs
mov eax, [AMagic]
end;
{$ENDIF}
{$ENDIF}
initialization
SystemState := TSystemState.Create;
finalization
FreeAndNil (SystemState);
end.