-
Notifications
You must be signed in to change notification settings - Fork 7
/
CMW.Utils.pas
2687 lines (2428 loc) · 71.7 KB
/
CMW.Utils.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit CMW.Utils;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Dialogs,
ExtCtrls, ComCtrls, taskSchd, taskSchdXP, TLHelp32, PSAPI, ShlObj, ValEdit,
Vcl.StdCtrls, Vcl.ImgList, Registry,
//
CMW.OSInfo;
//
type
TMUIType = (mtNone, mtIcon, mtString);
TFunctionStopping = function: Boolean;
TCPUUseFunct = function: Double;
TVersionInfo = record
CompanyName: WideString;
FileDescription: WideString;
FileVersion: WideString;
InternalName: WideString;
LegalCopyright: WideString;
LegalTradeMarks: WideString;
OriginalFilename: WideString;
ProductName: WideString;
ProductVersion: WideString;
Comments: WideString;
Language: WideString;
Translation: WideString;
FileVersionMajor: Word;
FileVersionMinor: Word;
FileVersionRelease: Word;
FileVersionBuild: Word;
ProductVersionMajor: Word;
ProductVersionMinor: Word;
ProductVersionRelease: Word;
ProductVersionBuild: Word;
Debug: Boolean;
Patched: Boolean;
PreRelease: Boolean;
PrivateBuild: Boolean;
SpecialBuild: Boolean;
end;
TProcessMonitor = class
private
FPID: Cardinal;
FStopping: Boolean;
FWorking: Boolean;
FExecuting: Boolean;
function FindChildProcess(PID: Cardinal; var CPID: Cardinal): Boolean;
public
procedure Stop;
function WaitStop(PID: Cardinal): Boolean;
function Execute(cmdLine: string): Boolean;
function ExecuteAndWait(cmdLine: string): Boolean;
property Executing: Boolean read FExecuting;
property ExePID: Cardinal read FPID;
end;
TIconSize = (is16, is32);
TMessageLevel = (mlInfo, mlWarning, mlError);
SYSTEM_INFORMATION_CLASS = (SystemBasicInformation, SystemProcessorInformation, SystemPerformanceInformation, SystemTimeOfDayInformation, SystemNotImplemented1, SystemProcessesAndThreadsInformation, SystemCallCounts, SystemConfigurationInformation, SystemProcessorTimes, SystemGlobalFlag, SystemNotImplemented2, SystemModuleInformation, SystemLockInformation, SystemNotImplemented3, SystemNotImplemented4, SystemNotImplemented5, SystemHandleInformation, SystemObjectInformation, SystemPagefileInformation,
SystemInstructionEmulationCounts, SystemInvalidInfoClass1, SystemCacheInformation, SystemPoolTagInformation, SystemProcessorStatistics, SystemDpcInformation, SystemNotImplemented6, SystemLoadImage, SystemUnloadImage, SystemTimeAdjustment, SystemNotImplemented7, SystemNotImplemented8, SystemNotImplemented9, SystemCrashDumpInformation, SystemExceptionInformation, SystemCrashDumpStateInformation, SystemKernelDebuggerInformation, SystemContextSwitchInformation, SystemRegistryQuotaInformation,
SystemLoadAndCallImage, SystemPrioritySeparation, SystemNotImplemented10, SystemNotImplemented11, SystemInvalidInfoClass2, SystemInvalidInfoClass3, SystemTimeZoneInformation, SystemLookasideInformation, SystemSetTimeSlipEvent, SystemCreateSession, SystemDeleteSession, SystemInvalidInfoClass4, SystemRangeStartInformation, SystemVerifierInformation, SystemAddVerifier, SystemSessionProcessesInformation);
LPVOID = Pointer;
EWin32Exception = class(Exception)
FErrorCode: LongInt;
public
property ErrorCode: LongInt read FErrorCode write FErrorCode;
end;
// òèï - ñïèñîê òåãîâ èíôîðìàöèè î âåðñèè ôàéëà (MSDN 6.0)
TFviTags = (fviComments, fviCompanyName, fviFileDescription, fviFileVersion, fviInternalName, fviLegalCopyright, fviLegalTrademarks, fviOriginalFilename, fviPrivateBuild, fviProductName, fviProductVersion, fviSpecialBuild);
TFileVersionInfoRecord = record
LangID: Word; // Windows language identifier
LangCP: Word; // Code page for the language
LangName: array[0..255] of Char; // Îòîáðàæàåìîå Windows èìÿ ÿçûêà
FieldDef: array[TFviTags] of string; // èìÿ ïàðàìåòðà ïî-àíãëèéñêè
FieldRus: array[TFviTags] of string; // èìÿ ïàðàìåòðà ïî-ðóññêè
Value: array[TFviTags] of string; // çíà÷åíèå ïàðàìåòðà
FileVer: string; // ÿçûêî-íåçàâèñèìîå çíà÷åíèå âåðñèè ôàéëà
ProductVer: string; // ÿçûêî-íåçàâèñèìîå çíà÷åíèå âåðñèè ïðîäóêòà
BuildType: string; // ÿçûêî-íåçàâèñèìîå - òèï ñáîðêè
FileType: string; // ÿçûêî-íåçàâèñèìîå - òèï ïðîäóêòà
end;
const // Èìåíà ïîëåé (òåãîâ) ïî-àíãëèéñêè:
cFviFieldsDef: array[TFviTags] of string = ('Comments', 'CompanyName', 'FileDescription', 'FileVersion', 'InternalName', 'LegalCopyright', 'LegalTrademarks', 'OriginalFilename', 'PrivateBuild', 'ProductName', 'ProductVersion', 'SpecialBuild');
const // Èìåíà ïîëåé (òåãîâ) ïî-ðóññêè:
cFviFieldsRus: array[TFviTags] of string = ('Êîììåíòàðèé', 'Ïðîèçâîäèòåëü', 'Îïèñàíèå', 'Âåðñèÿ ôàéëà', 'Âíóòðåííåå èìÿ', 'Àâòîðñêèå ïðàâà', 'Òîðãîâûå çíàêè', 'Èñõîäíîå èìÿ ôàéëà', 'Ïðèâàòíàÿ âåðñèÿ', 'Íàçâàíèå ïðîäóêòà', 'Âåðñèÿ ïðîäóêòà', 'Îñîáàÿ âåðñèÿ');
const
RusLangID = $0419;
const
OFASI_EDIT = $0001;
OFASI_OPENDESKTOP = $0002;
Shell32 = 'Shell32.dll';
DONT_RESOLVE_DLL_REFERENCES = $00000001;
{$EXTERNALSYM DONT_RESOLVE_DLL_REFERENCES}
LOAD_IGNORE_CODE_AUTHZ_LEVEL = $00000010;
{$EXTERNALSYM LOAD_IGNORE_CODE_AUTHZ_LEVEL}
LOAD_LIBRARY_AS_DATAFILE = $00000002;
{$EXTERNALSYM LOAD_LIBRARY_AS_DATAFILE}
LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = $00000040;
{$EXTERNALSYM LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE}
LOAD_LIBRARY_AS_IMAGE_RESOURCE = $00000020;
{$EXTERNALSYM LOAD_LIBRARY_AS_IMAGE_RESOURCE}
LOAD_LIBRARY_SEARCH_APPLICATION_DIR = $00000200;
{$EXTERNALSYM LOAD_LIBRARY_SEARCH_APPLICATION_DIR}
LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = $00001000;
{$EXTERNALSYM LOAD_LIBRARY_SEARCH_DEFAULT_DIRS}
LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = $00000100;
{$EXTERNALSYM LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR}
LOAD_LIBRARY_SEARCH_SYSTEM32 = $00000800;
{$EXTERNALSYM LOAD_LIBRARY_SEARCH_SYSTEM32}
LOAD_LIBRARY_SEARCH_USER_DIRS = $00000400;
{$EXTERNALSYM LOAD_LIBRARY_SEARCH_USER_DIRS}
LOAD_WITH_ALTERED_SEARCH_PATH = $00000008;
{$EXTERNALSYM LOAD_WITH_ALTERED_SEARCH_PATH}
var
LogBuf: string;
ThreadLogID: DWORD;
LogFile: TextFile;
SLog: TStrings;
NotUseLog: Boolean;
LangH: THandle;
LogList: ^TMemo;
ProcessMonitor: TProcessMonitor;
preIdleTime, preUserTime, preKrnlTime: TFileTime;
function GetSpacedInt(AText: string): string;
procedure NormFileName(var FN: string);
function NormFileNameF(FN: string): string;
function GetDirectores(Dir: string): TStringList;
function NormTime(Value: Cardinal): string;
procedure ScanDir(StartDir: string; Mask: string; List: TStrings);
procedure ScanDirFiles(StartDir: string; Mask, FileMask: string; List: TStrings);
function CustomStrSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
function CustomDateSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
function CustomIntSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
function RootKeyToStr(RK: HKEY): string;
function StrKeyToRoot(RK: string): HKEY;
function GetDateForTask(Value: TDate): string;
procedure GetTasks(Folder: ITaskFolder; AllFolder: Boolean; var TL: TTasksList);
procedure GetTasksXP(const TaskSched: TTaskScheduleOld; var TL: TTasksListXP);
function GetFileNameWoE(FileName: TFileName): string;
procedure Log(Value: array of const);
procedure Logging;
function GetNetInfo(ServerName: PWideChar; Level: DWORD; Bufptr: Pointer): DWORD; stdcall; external 'netapi32.dll' name 'NetWkstaGetInfo';
function GetGroup(LV: TListView; GroupName: string; Expand: Boolean): Word;
//function CheckEventSel(EventStr:string):Boolean;
function GetFileDateChg(FileName: string): TDateTime;
procedure Wait(Seconds: Cardinal);
function WinExec(lpCmdLine: string; uCmdShow: UINT): UINT; overload;
function StrToPAnsi(Str: string): PAnsiChar;
function DelFLSpace(str: string): string;
function DelFLDSpace(str: string): string;
function LoadString(sID: Cardinal): string; overload;
function LoadString(h: THandle; sID: Cardinal): string; overload;
function LangText(ID: Integer; Text: string): string;
function ByteToHexStr(Data: Pointer; Len: Integer): string;
function WordToHexStr(Data: Pointer; Len: Integer): string;
function GetHDDrives: string;
procedure RepVar(var Dest: string; Indent, VarInd: string);
function ILCreateFromPath(pszPath: PChar): PItemIDList stdcall; external shell32 name 'ILCreateFromPathW';
procedure ILFree(pidl: PItemIDList) stdcall; external shell32;
function SHOpenFolderAndSelectItems(pidlFolder: PItemIDList; cidl: Cardinal; apidl: pointer; dwFlags: DWORD): HRESULT; stdcall; external shell32;
function OpenFolderAndSelectFile(const FileName: string): boolean;
function OpenFolderAndOrSelectFile(const FileName: string): boolean;
function ReplaceSysVarF(Src: string): string;
procedure ReplaceSysVar(var Src: string);
procedure AddToValueEdit(VE: TValueListEditor; Key, Value, ValueBU: string);
function OccupiedFile(FN: string): Boolean;
procedure AddToLogList(Text: string);
function GetItemCount(LV: TListView; GID: Integer): Cardinal;
function ForceRemoveDir(sDir: string): Boolean;
//function GetListLogicalDrives:TStrings;
function BoolToLang(Value: Boolean): string;
function DeleteStrQM(Value: string): string;
procedure Unload;
function InstallDateToNorm(InstDate: string; const Def: TDateTime): TDateTime;
function GetFileNameFromLink(LinkFileName: string): string;
function DeleteForceFile(const FileName: string): Boolean;
function CustomUniSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
function FileTimeToDateTime(FileTime: TFileTime): TDateTime;
function ExistsFile(FileName: string): Boolean;
function CPUUsage: Extended;
function MixColors(FG, BG: TColor; T: byte): TColor;
procedure WuLine(ABitmap: TBitmap; Point1, Point2: TPoint; AColor: TColor);
function BoolStr(Value: Boolean; const VTrue, VFalse: string): string; overload;
function BoolStr(Value: Boolean): string; overload;
procedure CreateSubItems(var LI: TListItem; const Count: Word);
function GetUsersPaths(DefaultUP: string): TStrings;
function AddToListWOR(Item: string; List: TStrings): Boolean;
function AddToListW(Item: string; List: TStrings): Boolean;
function GetTempDir: string;
function GetFullPath(const ShortPath: string): string;
function ReadRegString(KEY: HKEY; Path, Item: string): string;
function SetPrivilege(aPrivilegeName: string; aEnabled: boolean): boolean;
function GetEnvironmentStrings1: string;
function CompareFileTimeOwn(t1, t2: FILETIME): Int64;
function FileTimeToInt(Value: TFileTime): Int64;
function GetFileIcon(const FileName: TFileName; Size: TIconSize; var destIcon: TIcon): Integer; overload;
function GetFileIcon(const FileName: TFileName; Size: TIconSize; var destIcon: HICON): Integer; overload;
function GetFileIcon(const FileName: TFileName; Size: TIconSize; IL: TCustomImageList): Integer; overload;
function GetFileIcon(const FileName: TFileName; Size: TIconSize; IL: TCustomImageList; var II: Word): Integer; overload;
procedure CreateMessage(Text: string; Level: TMessageLevel);
function CurOSIsNewerXP: Boolean;
function MUILoad(MUI: string; var Icon: TIcon; var Str: string): TMUIType;
function UnixDateTimeToDelphiDateTime(UnixDateTime: LongInt): TDateTime;
function GetRegValue(ARootKey: HKEY; AKey, Value: string): string;
function GetAccountName(const SID: PSID): string;
procedure RaiseWin32Error(Code: LongInt);
function GetDllVersion(FileName: string): Integer;
function ReadStringList(Roll: TRegistry; const name: string): string;
procedure ShowPropertiesDialog(FName: string);
function GetFileInfo(const strFilename: string): string;
function GetFileDescription(const FileName, ExceptText: string): string;
function GetFileTypeName(const strFilename: string): string;
procedure GetPathAndID(Input: string; var Path: string);
procedure GetPathID(Input: string; var Path: string; var ID: Cardinal);
//function GetIcons(FileName: String; Image32: TImageList):Integer;
function CalcChecked(LV: TListView): Integer;
procedure CaptureConsoleOutput(const ACommand, AParameters: string; AMemo: TMemo);
function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
implementation
uses
CMW.Main, Forms, ShellAPI, CMW.ModuleStruct, Vcl.FileCtrl, System.Win.ComObj,
Winapi.ActiveX;
type
TRGBTripleArray = array[0..1000] of TRGBTriple;
PRGBTripleArray = ^TRGBTripleArray;
function GetDosOutput(CommandLine: string; Work: string = 'C:\'): string;
var
SecAtrrs: TSecurityAttributes;
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
StdOutPipeRead, StdOutPipeWrite: THandle;
WasOK: Boolean;
pCommandLine: array[0..255] of AnsiChar;
BytesRead: Cardinal;
Handle: Boolean;
begin
Result := '';
with SecAtrrs do
begin
nLength := SizeOf(SecAtrrs);
bInheritHandle := True;
lpSecurityDescriptor := nil;
end;
CreatePipe(StdOutPipeRead, StdOutPipeWrite, @SecAtrrs, 0);
try
with StartupInfo do
begin
FillChar(StartupInfo, SizeOf(StartupInfo), 0);
cb := SizeOf(StartupInfo);
dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
wShowWindow := SW_HIDE;
hStdInput := GetStdHandle(STD_INPUT_HANDLE); // don't redirect stdin
hStdOutput := StdOutPipeWrite;
hStdError := StdOutPipeWrite;
end;
Handle := CreateProcess(nil, PChar('cmd.exe /C ' + CommandLine), nil, nil, True, 0, nil, PChar(Work), StartupInfo, ProcessInfo);
CloseHandle(StdOutPipeWrite);
if Handle then
try
repeat
WasOK := windows.ReadFile(StdOutPipeRead, pCommandLine, 255, BytesRead, nil);
if BytesRead > 0 then
begin
pCommandLine[BytesRead] := #0;
OemToAnsi(pCommandLine, pCommandLine);
Result := Result + pCommandLine;
end;
until not WasOK or (BytesRead = 0);
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
finally
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
end;
finally
CloseHandle(StdOutPipeRead);
end;
end;
procedure CaptureConsoleOutput(const ACommand, AParameters: string; AMemo: TMemo);
const
CReadBuffer = 2400;
var
saSecurity: TSecurityAttributes;
hRead: THandle;
hWrite: THandle;
suiStartup, si: TStartupInfo;
piProcess: TProcessInformation;
pBuffer: array[0..CReadBuffer] of AnsiChar;
dRead: DWord;
dRunning: DWord;
begin
saSecurity.nLength := SizeOf(TSecurityAttributes);
saSecurity.bInheritHandle := True;
saSecurity.lpSecurityDescriptor := nil;
if CreatePipe(hRead, hWrite, @saSecurity, 0) then
begin
FillChar(suiStartup, SizeOf(TStartupInfo), #0);
suiStartup.cb := SizeOf(TStartupInfo);
suiStartup.hStdInput := hRead;
suiStartup.hStdOutput := hWrite;
suiStartup.hStdError := hWrite;
suiStartup.dwFlags := STARTF_USESTDHANDLES or STARTF_USESHOWWINDOW;
suiStartup.wShowWindow := SW_SHOWNORMAL;
if CreateProcess(nil, PChar(ACommand + ' ' + AParameters), @saSecurity, @saSecurity, True, NORMAL_PRIORITY_CLASS, nil, nil, suiStartup, piProcess) then
begin
repeat
dRunning := WaitForSingleObject(piProcess.hProcess, 100);
Application.ProcessMessages();
repeat
dRead := 0;
ReadFile(hRead, pBuffer[0], CReadBuffer, dRead, nil);
pBuffer[dRead] := #0;
OemToAnsi(pBuffer, pBuffer);
AMemo.Lines.Add(string(pBuffer));
until (dRead < CReadBuffer);
until (dRunning <> WAIT_TIMEOUT);
CloseHandle(piProcess.hProcess);
CloseHandle(piProcess.hThread);
end;
CloseHandle(hRead);
CloseHandle(hWrite);
end;
end;
function CalcChecked(LV: TListView): Integer;
var
i: Integer;
begin
Result := 0;
for i := 0 to LV.Items.Count - 1 do
if LV.Items[i].Checked then
Inc(Result);
end;
function CurOSIsNewerXP: Boolean;
begin
Result := Win32MajorVersion > 5;
end;
function MUILoad(MUI: string; var Icon: TIcon; var Str: string): TMUIType;
var
hResModule: HMODULE;
Path: string;
ID: Cardinal;
buffer: array[0..1023] of Char;
ls: integer;
begin
Result := mtNone;
GetPathID(MUI, Path, ID);
NormFileName(Path);
hResModule := LoadLibraryEx(PWideChar(Path), 0, LOAD_LIBRARY_AS_DATAFILE or LOAD_LIBRARY_AS_IMAGE_RESOURCE);
if hResModule <> 0 then
begin
ls := LoadStringW(hResModule, ID, buffer, SizeOf(buffer));
if ls > 0 then
begin
Str := StrPas(buffer);
Result := mtString;
end
else
begin
//;
//if Integer(ExtractIconEx(PWideChar(Path), ID, Icon32, Icon16, 1)) > 0 then
begin
Icon := TIcon.Create;
Icon.Handle := LoadImage(hResModule, MakeIntResource(ID), IMAGE_ICON, 16, 16, LR_COPYFROMRESOURCE);
if Icon.Handle <> 0 then
Result := mtIcon;
end;
end;
FreeLibrary(hResModule);
end;
//DisposeStr(@buffer[1]);
{
if MUI.Length > 0 then
if MUI[1] = '@' then
begin
GetPathAndID(MUI, Tmp);
ReplaceSysVar(Tmp);
if FileExists(Tmp) then Dir:=nil
else
begin
NormFileName(Tmp);
Tmp:=ExtractFilePath(Tmp);
Dir:=PWideChar(Tmp);
end;
NM:=Caption;
OSize:=0;
OBuf:=#0;
MUIRes:=RegLoadMUIString(FRoll.CurrentKey,
PWideChar(NM),
@OBuf,
SizeOf(OBuf),
@OSize,
0,
Dir);
if MUIRes = ERROR_SUCCESS then
begin
RegLoadMUIString(FRoll.CurrentKey,
PWideChar(NM),
@OBuf,
OSize,
@OSize,
0,
Dir);
s:=Trim(StrPas(OBuf))+' ('+s+')';
end;
end; }
end;
procedure CreateMessage(Text: string; Level: TMessageLevel);
var
Cap: string;
Icon: Integer;
begin
case Level of
mlInfo:
begin
Cap := 'Èíôîðìàöèÿ';
Icon := MB_ICONINFORMATION;
end;
mlWarning:
begin
Cap := 'Âíèìàíèå';
Icon := MB_ICONWARNING;
end;
mlError:
begin
Cap := 'Îøèáêà';
Icon := MB_ICONERROR;
end;
end;
Log([Cap + ':', Text, SysErrorMessage(GetLastError)]);
MessageBox(Application.Handle, PWideChar(Text), PWideChar(Cap), MB_OK or Icon);
end;
function GetFileIcon(const FileName: TFileName; Size: TIconSize; var destIcon: TIcon): Integer;
var
Icon32, Icon16, IcEx: HICON;
i, II: word;
begin
Result := -1;
try
II := 0;
IcEx := ExtractAssociatedIconEx(0, PChar(FileName), II, i);
if IcEx > 0 then
begin
destIcon := TIcon.Create;
if Integer(ExtractIconEx(PWideChar(FileName), i, Icon32, Icon16, 1)) > 0 then
case Size of
is16:
IcEx := Icon16;
is32:
IcEx := Icon32;
end;
destIcon.Handle := IcEx;
Result := IcEx;
end;
except
on E: Exception do
Exit;
end;
end;
function GetFileIcon(const FileName: TFileName; Size: TIconSize; var destIcon: HICON): Integer;
var
Icon32, Icon16, IcEx: HICON;
i: word;
begin
Result := -1;
try
IcEx := ExtractAssociatedIcon(0, PChar(FileName), i);
if IcEx > 0 then
begin
if Integer(ExtractIconEx(PWideChar(FileName), i, Icon32, Icon16, 1)) > 0 then
case Size of
is16:
IcEx := Icon16;
is32:
IcEx := Icon32;
end;
destIcon := IcEx;
Result := IcEx;
end;
except
on E: Exception do
Exit;
end;
end;
function GetFileIcon(const FileName: TFileName; Size: TIconSize; IL: TCustomImageList): Integer;
var
Icon: TIcon;
Icon32, Icon16, IcEx: HICON;
i: word;
begin
Result := -1;
try
IcEx := ExtractAssociatedIcon(0, PChar(FileName), i);
if IcEx > 0 then
begin
Icon := TIcon.Create;
if Integer(ExtractIconEx(PWideChar(FileName), i, Icon32, Icon16, 1)) > 0 then
case Size of
is16:
IcEx := Icon16;
is32:
IcEx := Icon32;
end;
Icon.Handle := IcEx;
Result := IL.AddIcon(Icon);
FreeAndNil(Icon);
end;
except
on E: Exception do
Exit;
end;
end;
function GetFileIcon(const FileName: TFileName; Size: TIconSize; IL: TCustomImageList; var II: Word): Integer;
var
Icon: TIcon;
Icon32, Icon16, IcEx: HICON;
i: word;
begin
Result := -1;
try
IcEx := ExtractAssociatedIcon(0, PChar(FileName), i);
if IcEx > 0 then
begin
Icon := TIcon.Create;
if Integer(ExtractIconEx(PWideChar(FileName), i, Icon32, Icon16, 1)) > 0 then
case Size of
is16:
IcEx := Icon16;
is32:
IcEx := Icon32;
end;
Icon.Handle := IcEx;
II := IL.AddIcon(Icon);
Result := II;
Icon.Free;
end;
except
Exit;
end;
end;
function GetFullPath(const ShortPath: string): string;
begin
Result := ExpandFileName(ShortPath);
end;
function GetEnvironmentStrings1: string;
{Ïåðåìåííûå ñðåäû}
var
ptr: PChar;
s: string;
Done: boolean;
begin
s := '';
Result := '';
Done := FALSE;
ptr := windows.GetEnvironmentStrings;
while Done = false do
begin
if ptr^ = #0 then
begin
inc(ptr);
if ptr^ = #0 then
Done := TRUE
else
Result := Result + s + #13#10;
s := ptr^;
end
else
s := s + ptr^;
inc(ptr);
end;
end;
function SetPrivilege(aPrivilegeName: string; aEnabled: boolean): boolean;
var
TPPrev, TP: TTokenPrivileges;
Token: THandle;
dwRetLen: DWord;
begin
Result := False;
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, Token);
TP.PrivilegeCount := 1;
if (LookupPrivilegeValue(nil, PChar(aPrivilegeName), TP.Privileges[0].LUID)) then
begin
if (aEnabled) then
TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
TP.Privileges[0].Attributes := 0;
dwRetLen := 0;
Result := AdjustTokenPrivileges(Token, False, TP, SizeOf(TPPrev), TPPrev, dwRetLen);
end;
CloseHandle(Token);
end;
function ReadRegString(KEY: HKEY; Path, Item: string): string;
var
Roll: TRegistry;
begin
try
Roll := TRegistry.Create(KEY_READ);
Roll.RootKey := KEY;
if Roll.OpenKeyReadOnly(Path) then
try
Result := Roll.ReadString(Item);
except
begin
Log(['Íåñìîã ïðî÷åñòü', Path, 'èç', Item]);
Exit('');
end;
end;
finally
FreeAndNil(Roll);
end;
end;
function GetTempDir: string;
var
len: Cardinal;
begin
SetLength(Result, MAX_PATH + 1);
len := GetTempPath(MAX_PATH, PWideChar(Result));
SetLength(Result, len);
end;
function AddToListWOR(Item: string; List: TStrings): Boolean;
var
i: Word;
tmp: string;
begin
if not Assigned(List) then
Exit(False);
if List.Count <= 0 then
begin
try
List.Add(Item);
Result := True;
except
Result := False;
end;
Exit;
end;
tmp := AnsiLowerCase(Item);
for i := 0 to List.Count - 1 do
if tmp = AnsiLowerCase(List.Strings[i]) then
Exit(False);
try
List.Add(Item);
Result := True;
except
Result := False;
end;
end;
function AddToListW(Item: string; List: TStrings): Boolean;
begin
if not Assigned(List) then
Exit(False);
begin
try
List.Add(Item);
Result := True;
except
Result := False;
end;
Exit;
end;
end;
function GetUsersPaths(DefaultUP: string): TStrings;
var
Roll: TRegistry;
TMP: TStrings;
ProfPath: string;
i: Word;
begin
Result := GetDirectores(DefaultUP);
try
Roll := TRegistry.Create(KEY_READ);
Roll.RootKey := HKEY_LOCAL_MACHINE;
if Roll.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList') then
begin
TMP := TStringList.Create;
Roll.GetKeyNames(TMP);
if TMP.Count > 0 then
for i := 0 to TMP.Count - 1 do
begin
Roll.CloseKey;
if Roll.OpenKeyReadOnly('SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\' + TMP.Strings[i]) then
begin
ProfPath := ReplaceSysVarF(Roll.ReadString('ProfileImagePath'));
AddToListWOR(ProfPath, Result);
end;
end;
end;
finally
Roll.Free;
TMP.Free;
end;
end;
procedure CreateSubItems(var LI: TListItem; const Count: Word);
var
i: Word;
begin
if Count <= 0 then
Exit;
for i := 1 to Count do
LI.SubItems.Add('');
end;
function BoolStr(Value: Boolean; const VTrue, VFalse: string): string;
begin
if Value then
Exit(VTrue)
else
Exit(VFalse);
end;
function BoolStr(Value: Boolean): string;
begin
Result := BoolStr(Value, 'Äà', 'Íåò');
end;
procedure AlphaBlendPixel(ABitmap: TBitmap; X, Y: integer; R, G, B: byte; ARatio: Real);
var
LBack, LNew: TRGBTriple;
LMinusRatio: Real;
LScan: PRGBTripleArray;
begin
if (X < 0) or (X > ABitmap.Width - 1) or (Y < 0) or (Y > ABitmap.Height - 1) then
Exit; // clipping
LScan := ABitmap.Scanline[Y];
LMinusRatio := 1 - ARatio;
LBack := LScan[X];
LNew.rgbtBlue := round(B * ARatio + LBack.rgbtBlue * LMinusRatio);
LNew.rgbtGreen := round(G * ARatio + LBack.rgbtGreen * LMinusRatio);
LNew.rgbtRed := round(R * ARatio + LBack.rgbtRed * LMinusRatio);
LScan[X] := LNew;
end;
procedure WuLine(ABitmap: TBitmap; Point1, Point2: TPoint; AColor: TColor);
var
deltax, deltay, loop, start, finish: integer;
dx, dy, dydx: single; // fractional parts
LR, LG, LB: byte;
x1, x2, y1, y2: integer;
begin
x1 := Point1.X;
y1 := Point1.Y;
x2 := Point2.X;
y2 := Point2.Y;
deltax := abs(x2 - x1); // Calculate deltax and deltay for initialisation
deltay := abs(y2 - y1);
if (deltax = 0) or (deltay = 0) then
begin // straight lines
{ABitmap.Canvas.Pen.Color := AColor;
ABitmap.Canvas.MoveTo(x1, y1);
ABitmap.Canvas.LineTo(x2, y2); MixColors(clRed, clLime, PT);
exit;}
deltax := 1;
end; {
LR := (AColor and $000000FF);
LG := (AColor and $0000FF00) shr 8;
LB := (AColor and $00FF0000) shr 16; }
if deltax > deltay then
begin // horizontal or vertical
if y2 > y1 then
dydx := -(deltay / deltax)
else
dydx := deltay / deltax;
if x2 < x1 then
begin
start := x2; // right to left
finish := x1;
dy := y2;
end
else
begin
start := x1; // left to right
finish := x2;
dy := y1;
dydx := -dydx; // inverse slope
end;
for loop := start to finish do
begin
AColor := MixColors(clLime, clRed, Round((trunc(dy) * 100) / ABitmap.Canvas.ClipRect.Height));
LR := (AColor and $000000FF);
LG := (AColor and $0000FF00) shr 8;
LB := (AColor and $00FF0000) shr 16;
AlphaBlendPixel(ABitmap, loop, trunc(dy), LR, LG, LB, 1 - frac(dy));
AlphaBlendPixel(ABitmap, loop, trunc(dy) + 1, LR, LG, LB, frac(dy));
dy := dy + dydx; // next point
end;
end
else
begin
if x2 > x1 then
dydx := -(deltax / deltay)
else
dydx := deltax / deltay;
if y2 < y1 then
begin
start := y2; // right to left
finish := y1;
dx := x2;
end
else
begin
start := y1; // left to right
finish := y2;
dx := x1;
dydx := -dydx; // inverse slope
end;
for loop := start to finish do
begin
AColor := MixColors(clLime, clRed, Round((loop * 100) / ABitmap.Canvas.ClipRect.Height));
LR := (AColor and $000000FF);
LG := (AColor and $0000FF00) shr 8;
LB := (AColor and $00FF0000) shr 16;
AlphaBlendPixel(ABitmap, trunc(dx), loop, LR, LG, LB, 1 - frac(dx));
AlphaBlendPixel(ABitmap, trunc(dx) + 1, loop, LR, LG, LB, frac(dx));
dx := dx + dydx; // next point
end;
end;
end;
function MixBytes(FG, BG, TRANS: byte): byte;
begin
Result := Round(BG + (FG - BG) / 255 * TRANS);
end;
function MixColors(FG, BG: TColor; T: byte): TColor;
var
r, g, b: byte;
begin
T := Round((255 / 100) * T);
if T = 0 then
T := 1;
r := MixBytes(FG and 255, BG and 255, T); // extracting and mixing Red
g := MixBytes((FG shr 8) and 255, (BG shr 8) and 255, T); // the same with green
b := MixBytes((FG shr 16) and 255, (BG shr 16) and 255, T); // and blue, of course
Result := r + g * 256 + b * 65536; // finishing with combining all channels together
end;
function FileTimeToInt(Value: TFileTime): Int64;
begin
Result := (Value.dwHighDateTime shl 32) or (Value.dwLowDateTime);
end;
function CompareFileTimeOwn(t1, t2: FILETIME): Int64;
var
a, b: Int64;
begin
a := (t1.dwHighDateTime shl 32) or (t1.dwLowDateTime);
b := (t2.dwHighDateTime shl 32) or (t2.dwLowDateTime);
Result := b - a;
end;
function CPUUsage: Extended;
var
idle, user, krnl: TFileTime;
i, u, k: int64;
begin
GetSystemTimes(idle, krnl, user);
i := CompareFileTimeOwn(idle, preIdleTime);
u := CompareFileTimeOwn(user, preUserTime);
k := CompareFileTimeOwn(krnl, preKrnlTime);
Result := (k + u - i) * 100 / (k + u + 0.00001);
if Result > 100 then
Result := 100