forked from HemulGM/Components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HGM.WinAPI.pas
368 lines (329 loc) · 9.85 KB
/
HGM.WinAPI.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
unit HGM.WinAPI;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls,
Vcl.Forms, Winapi.Dwmapi, System.Math;
function AdvSelectDirectory(const Caption: string; const Root: WideString; var Directory: string; EditBox: Boolean = False; ShowFiles: Boolean = False; AllowCreateDirs: Boolean = True): Boolean;
function CompareFileTimeOwn(t1, t2: FILETIME): Int64;
function CPUUsage(var preIdleTime, preUserTime, preKrnlTime: TFileTime; ProcInterval: Integer): Extended;
function GetAeroColor: TColor;
function GetMachineName: string;
function GetUserName: string;
function GetAppData: string;
function SIDToString(ASID: PSID): string;
function GetComputerSID: string;
function GetFromConsole(Caption: string; var Text: string): Boolean;
function GetFileNameFromLink(LinkFileName: string): string;
function GetFileDescription(const FileName, ExceptText: string): string;
function ShowModalFor(Parent: TWinControl; Form: TForm): TModalResult;
function GetVersion: string;
function ConvertSidToStringSid(Sid:PSID; out StringSid:PChar):BOOL; stdcall; external 'ADVAPI32.DLL' name {$IFDEF UNICODE} 'ConvertSidToStringSidW'{$ELSE} 'ConvertSidToStringSidA'{$ENDIF};
implementation
uses
ShlObj, ComObj, ActiveX, HGM.Common.Utils;
function GetVersion: string;
var
Res: TResourceStream;
Info: PVSFixedFileInfo;
InfoLen: Cardinal;
begin
Result := '';
Res := TResourceStream.Create(HInstance, '#1', RT_VERSION);
try
if Res.Size > 0 then
begin
if VerQueryValue(Res.Memory, '\', Pointer(Info), InfoLen) then
begin
Result := Format('%d.%d.%d.%d', [Info.dwFileVersionMS shr $10, Info.dwFileVersionMS and $FFFF, Info.dwFileVersionLS shr $10, Info.dwFileVersionLS and $FFFF]);
end;
end;
finally
Res.Free;
end;
end;
function ShowModalFor(Parent: TWinControl; Form: TForm): TModalResult;
begin
Form.Show;
//Parent.Enabled := False;
try
SendMessage(Form.Handle, CM_ACTIVATE, 0, 0);
Form.ModalResult := 0;
Form.BringToFront;
repeat
Application.HandleMessage;
if Application.Terminated then
Form.ModalResult := mrCancel;
until Form.ModalResult <> 0;
Result := Form.ModalResult;
SendMessage(Form.Handle, CM_DEACTIVATE, 0, 0);
finally
Form.Hide;
//Parent.Enabled := True;
end;
end;
function GetFileDescription(const FileName, ExceptText: string): string;
type
TLangRec = array[0..1] of Word;
var
InfoSize, zero: Cardinal;
pbuff: Pointer;
pk: Pointer;
nk: Cardinal;
lang_hex_str: string;
LangID: Word;
LangCP: Word;
begin
pbuff := nil;
Result := '';
InfoSize := Winapi.Windows.GetFileVersionInfoSize(PChar(FileName), zero);
if InfoSize <> 0 then
try
GetMem(pbuff, InfoSize);
if Winapi.Windows.GetFileVersionInfo(PChar(FileName), 0, InfoSize, pbuff) then
begin
if VerQueryValue(pbuff, '\VarFileInfo\Translation', pk, nk) then
begin
LangID := TLangRec(pk^)[0];
LangCP := TLangRec(pk^)[1];
lang_hex_str := Format('%.4x', [LangID]) + Format('%.4x', [LangCP]); //FileDescription
if VerQueryValue(pbuff, PChar('\\StringFileInfo\\' + lang_hex_str + '\\FileDescription'), pk, nk) then
Result := string(PChar(pk))
else if VerQueryValue(pbuff, PChar('\\StringFileInfo\\' + lang_hex_str + '\\CompanyName'), pk, nk) then
Result := string(PChar(pk));
end;
end;
finally
if pbuff <> nil then
FreeMem(pbuff);
end;
if Result = '' then
if (ExceptText <> '') then
if (ExceptText <> '/') then
Result := ExceptText
else
Exit('')
else
Result := GetFileNameWoE(FileName);
end;
function GetAppData: string;
var
Path: array[0..MAX_PATH] of Char;
begin
if SHGetFolderPath(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, Path) = S_OK then
Result := IncludeTrailingPathDelimiter(Path)
else
Result := '';
end;
function GetFileNameFromLink(LinkFileName: string): string;
var
MyObject: IUnknown;
MySLink: IShellLink;
MyPFile: IPersistFile;
FileInfo: TWin32FINDDATA;
Buff: array[0..MAX_PATH] of Char;
begin
Result := '';
if not FileExists(LinkFileName) then
Exit;
MyObject := CreateComObject(CLSID_ShellLink);
MyPFile := MyObject as IPersistFile;
MySLink := MyObject as IShellLink;
MyPFile.Load(PWideChar(LinkFileName), STGM_READ);
MySLink.GetPath(Buff, MAX_PATH, FileInfo, SLGP_UNCPRIORITY);
Result := Buff;
end;
function GetFromConsole(Caption: string; var Text: string): Boolean;
begin
try
AllocConsole;
Write(Caption);
Readln(Text);
Result := True;
finally
FreeConsole;
end;
end;
function SIDToString(ASID: PSID): string;
var
StringSid: PChar;
begin
ConvertSidToStringSid(ASID, StringSid);
Result := string(StringSid);
end;
function GetComputerSID: string;
var
Sid: PSID;
cbSid: DWORD;
cbReferencedDomainName: DWORD;
ReferencedDomainName: string;
peUse: SID_NAME_USE;
Success: BOOL;
lpSystemName: string;
lpAccountName: string;
begin
Sid := nil;
try
lpSystemName := '';
lpAccountName := GetMachineName;
cbSid := 0;
cbReferencedDomainName := 0;
Success := LookupAccountName(PChar(lpSystemName), PChar(lpAccountName), nil, cbSid, nil, cbReferencedDomainName, peUse);
if (not Success) and (GetLastError = ERROR_INSUFFICIENT_BUFFER) then
begin
SetLength(ReferencedDomainName, cbReferencedDomainName);
Sid := AllocMem(cbSid);
Success := LookupAccountName(PChar(lpSystemName), PChar(lpAccountName), Sid, cbSid, PChar(ReferencedDomainName), cbReferencedDomainName, peUse);
if not Success then
begin
FreeMem(Sid);
Sid := nil;
RaiseLastOSError;
end
else
Result := SIDToString(Sid);
end
else
RaiseLastOSError;
finally
if Assigned(Sid) then
FreeMem(Sid);
end;
end;
function CPUUsage(var preIdleTime, preUserTime, preKrnlTime: TFileTime; ProcInterval: Integer): Extended;
var
idle, user, krnl: TFileTime;
i, u: int64;
begin
GetSystemTimes(idle, krnl, user);
i := Abs(CompareFileTimeOwn(idle, preIdleTime));
u := Abs(CompareFileTimeOwn(user, preUserTime)) + Abs(CompareFileTimeOwn(krnl, preKrnlTime));
Result := ((u - i) / ProcInterval) / 100;
Result := Max(0, Min(Result, 100));
preIdleTime := idle;
preUserTime := user;
preKrnlTime := krnl;
end;
function CompareFileTimeOwn(t1, t2: FILETIME): Int64;
begin
Result := ((t2.dwHighDateTime shl 32) or (t2.dwLowDateTime)) - ((t1.dwHighDateTime shl 32) or (t1.dwLowDateTime));
end;
function GetMachineName: string;
var
Size: Cardinal;
PRes: PChar;
BRes: Boolean;
begin
Result := 'Íå îïðåäåëåíî';
try
Size := MAX_COMPUTERNAME_LENGTH + 1;
PRes := StrAlloc(Size);
BRes := GetComputerName(PRes, Size);
if BRes then
Result := StrPas(PRes);
except
Exit;
end;
end;
function GetUserName: string;
var
a: array[0..254] of Char;
lenBuf: Cardinal;
begin
Result := 'Íå îïðåäåëåíî';
try
lenBuf := 255;
Winapi.Windows.GetUserName(a, lenBuf);
Result := StrPas(a);
except
Exit;
end;
end;
function AdvSelectDirectory(const Caption: string; const Root: WideString; var Directory: string; EditBox: Boolean = False; ShowFiles: Boolean = False; AllowCreateDirs: Boolean = True): Boolean;
function SelectDirCB(Wnd: HWND; uMsg: UINT; lParam, lpData: lParam): Integer; stdcall;
begin
case uMsg of
BFFM_INITIALIZED:
SendMessage(Wnd, BFFM_SETSELECTION, Ord(True), Integer(lpData));
end;
Result := 0;
end;
var
WindowList: Pointer;
BrowseInfo: TBrowseInfo;
Buffer: PChar;
RootItemIDList, ItemIDList: PItemIDList;
ShellMalloc: IMalloc;
IDesktopFolder: IShellFolder;
Eaten, Flags: LongWord;
const
BIF_USENEWUI = $0040;
BIF_NOCREATEDIRS = $0200;
begin
Result := False;
if not DirectoryExists(Directory) then
Directory := '';
FillChar(BrowseInfo, SizeOf(BrowseInfo), 0);
if (ShGetMalloc(ShellMalloc) = S_OK) and (ShellMalloc <> nil) then
begin
Buffer := ShellMalloc.Alloc(MAX_PATH);
try
RootItemIDList := nil;
if Root <> '' then
begin
SHGetDesktopFolder(IDesktopFolder);
IDesktopFolder.ParseDisplayName(Application.Handle, nil, POleStr(Root), Eaten, RootItemIDList, Flags);
end;
OleInitialize(nil);
with BrowseInfo do
begin
hwndOwner := Application.Handle;
pidlRoot := RootItemIDList;
pszDisplayName := Buffer;
lpszTitle := PChar(Caption);
ulFlags := BIF_RETURNONLYFSDIRS or BIF_USENEWUI or BIF_EDITBOX * Ord(EditBox) or BIF_BROWSEINCLUDEFILES * Ord(ShowFiles) or BIF_NOCREATEDIRS * Ord(not AllowCreateDirs);
lpfn := @SelectDirCB;
if Directory <> '' then
lParam := Integer(PChar(Directory));
end;
WindowList := DisableTaskWindows(0);
try
ItemIDList := ShBrowseForFolder(BrowseInfo);
finally
EnableTaskWindows(WindowList);
end;
Result := ItemIDList <> nil;
if Result then
begin
ShGetPathFromIDList(ItemIDList, Buffer);
ShellMalloc.Free(ItemIDList);
Directory := Buffer;
end;
finally
ShellMalloc.Free(Buffer);
end;
end;
end;
function GetAeroColor: TColor;
var
OpaqueBlend: Bool;
AColor: DWord;
A, R, G, B: Integer;
OSInfo: TOSVersionInfo;
begin
ZeroMemory(@OSInfo, SizeOf(OSInfo));
OSInfo.dwOSVersionInfoSize := SizeOf(TOSVERSIONINFO);
if (((not GetVersionEx(OSInfo)) and (OSInfo.dwPlatformId <> VER_PLATFORM_WIN32_NT) and (OSInfo.dwMajorVersion < 5))) or (Winapi.Dwmapi.DwmGetColorizationColor(AColor, OpaqueBlend) = S_FALSE) then
begin
Result := clNone;
Exit;
end;
A := (AColor and $FF000000) shr 24;
R := (AColor and $00FF0000) shr 16;
G := (AColor and $0000FF00) shr 8;
B := (AColor and $000000FF);
R := Max(0, Min(R + (255 - A - 40), 255));
G := Max(0, Min(G + (255 - A - 40), 255));
B := Max(0, Min(B + (255 - A - 40), 255));
Result := RGB(R, G, B);
end;
end.