-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDSiWin32.pas
More file actions
4027 lines (3738 loc) · 131 KB
/
Copy pathDSiWin32.pas
File metadata and controls
4027 lines (3738 loc) · 131 KB
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
(*:Collection of Win32 wrappers and helper functions.
@desc <pre>
Free for personal and commercial use. No rights reserved.
Maintainer : gabr
Contributors : ales, aoven, gabr, Lee_Nover, _MeSSiah_, Miha-R, Odisej, xtreme,
Brdaws, Gre-Gor, krho, Cavlji
Creation date : 2002-10-09
Last modification : 2006-12-07
Version : 1.22
</pre>*)(*
History:
1.22: 2006-12-07
- New function: DSiGetFileTime.
- Added Windows Vista detection to DSiGetWindowsVersion.
1.21: 2006-08-14
- New functions: DSiFileExistsW, DSiDirectoryExistsW, DSiFileSizeW,
DSiCompressFile, DSiUncompressFile, DSiIsFileCompressed, DSiIsFileCompressedW.
1.20: 2006-06-20
- New function: DSiGetShortcutInfo.
1.19: 2006-05-15
- New functions: DSiEnumFilesToSL, DSiGetLongPathName.
1.18: 2006-04-11
- New function: DSiSetDllDirectory.
1.17a: 2006-04-05
- Exit DSiProcessMessages when WM_QUIT is received.
1.17: 2006-03-14
- New function: DSiRegistryValueExists.
- Added 'working dir' parameter to the DSiCreateShortcut function.
1.16: 2006-01-23
- New DSiExecuteAndCapture implementation, contributed by matej.
1.15b: 2005-12-19
- TDSiRegistry.ReadInteger can now also read 4-byte binary values.
1.15a: 2005-08-09
- Removed StrNew from DSiGetComputerName because it caused the result never to
be released.
1.15: 2005-07-11
- New function: DSiWin32CheckNullHandle.
1.14: 2005-06-09
- New function: DSiGetEnvironmentVariable.
- DSiExecuteAndCapture modified to return exit code in a (newly added) parameter
and fixed to work on fast computers.
1.13a: 2005-03-15
- Make DSiGetTempFileName return empty string when GetTempFileName fails.
1.13: 2005-02-12
- New functions: DSiExitWindows, DSiGetSystemLanguage, DSiGetKeyboardLayouts.
- New methods: TDSiRegistry.ReadBinary (two overloaded versions),
TDSiRegistry.WriteBinary (two overloaded versions).
- Added OLE string processing to TDSiRegistry.ReadVariant and
TDSiRegistry.WriteVariant.
- Exported helper functions UTF8Encode and UTF8Decode for old Delphis (D5 and
below).
- Added Windows 2003 detection to DSiGetWindowsVersion.
- Modified DSiEnablePrivilege to return True without doint anything on 9x platform.
- Fixed handle leak in DSiSetProcessPriorityClass.
- Removed some dead code.
- Documented the Information segment.
1.12: 2004-09-21
- Added function DSiIncrementWorkingSet.
1.11: 2004-02-12
- Added functions DSiSetProcessPriorityClass, DSiGetProcessOwnerInfo (two
overloaded versions), DSiEnablePrivilege.
1.10: 2003-12-18
- Updated TDSiRegistry.ReadString to handle DWORD registry values too.
- Updated TDSiRegistry.ReadInteger to handle string registry values too.
1.09: 2003-11-14
- Added functions DSiValidateProcessAffinity, DSiValidateThreadAffinity,
DSiValidateProcessAffinityMask, DSiValidateThreadAffinityMask,
DSiGetSystemAffinityMask, DSiGetProcessAffinityMask,
DSiGetThreadAffinityMask, DSiAffinityMaskToString, and
DSiStringToAffinityMask.
1.08: 2003-11-12
- Added functions DSiCloseHandleAndInvalidate, DSiWin32CheckHandle,
DSiGetSystemAffinity, DSiGetProcessAffinity, DSiSetProcessAffinity,
DSiGetThreadAffinity, DSiSetThreadAffinity.
- Added types TDSiFileHandle, TDSiPipeHandle, TDSiMutexHandle, TDSiEventHandle,
TDSiSemaphoreHandle; all equivaled to THandle.
1.07a: 2003-10-18
- DSiuSecDelay was broken. Fixed.
1.07: 2003-10-09
- Added functions DSiGetUserNameEx, DSiIsDiskInDrive, DSiGetDiskLabel,
DSiGetMyDocumentsFolder, DSiGetSystemVersion, DSiRefreshDesktop,
DSiGetWindowsVersion, DSiRebuildDesktopIcons.
- Added TDSiRegistry methods ReadStrings and WriteStrings dealing with MULTI_SZ
registry format.
1.06a: 2003-09-03
- Typo fixed in DSiMsgWaitForTwoObjectsEx.
1.06: 2003-09-02
- New functions: DSiMsgWaitForTwoObjectsEx, DSiMsgWaitForThreeObjectsEx.
- Documented 'Handles' and 'Registry' sections.
- Bug fixed in DSiLoadLibrary.
1.05: 2003-09-02
- New functions: DSiMonitorOn, DSiMonitorOff, DSiMonitorStandby, DSiGetBootType,
DSiShareFolder, DSiUnshareFolder, DSiFileSize, DSiEnumFiles, DSiEnumFilesEx,
DSiGetDomain, DSiProcessMessages, DSiProcessThreadMessages, DSiLoadLibrary,
DSiGetProcAddress, DSiDisableX, DSiEnableX.
- Added dynamically loaded API forwarders: DSiNetApiBufferFree, DSiNetWkstaGetInfo,
DSiSHEmptyRecycleBin, DSiCreateProcessAsUser, DSiLogonUser,
DSiImpersonateLoggedOnUser, DSiRevertToSelf, DSiCloseServiceHandle,
DSiOpenSCManager, DSi9xNetShareAdd, DSi9xNetShareDel, DSiNTNetShareAdd,
DSiNTNetShareDel.
- DSiGetUserName could fail on Win9x. Fixed.
- Declared constants WAIT_OBJECT_1 (= WAIT_OBJECT_0+1) to WAIT_OBJECT_9
(=WAIT_OBJECT_0+9).
- All dynamically loaded functions are now available to the public (see new
{ DynaLoad } section).
- All functions using dynamically loaded API calls were modified to use new
DynaLoad methods.
- All string parameters turned into 'const' parameters.
- Various constants and type declarations moved to the 'interface' section.
1.04: 2003-05-27
- New functions: DSiLoadMedia, DSiEjectMedia.
1.03: 2003-05-24
- New functions: DSiExecuteAndCapture, DSiFreeMemAndNil.
1.02a: 2003-05-05
- Refuses to compile with Kylix.
- Removed platform-related warnings on Delphi 6&7.
1.02: 2002-12-29
- New function: DSiElapsedSince.
1.01: 2002-12-19
- Compiles with Delphi 6 and Delphi 7.
- New functions:
Files:
procedure DSiDeleteFiles(folder: string; fileMask: string);
procedure DSiDeleteTree(folder: string; removeSubdirsOnly: boolean);
procedure DSiEmptyFolder(folder: string);
procedure DSiEmptyRecycleBin;
procedure DSiRemoveFolder(folder: string);
procedure DSiuSecDelay(delay: word);
Processes:
function DSiExecuteAsUser(const commandLine, username, password: string;
const domain: string = '.'; visibility: integer = SW_SHOWDEFAULT;
workDir: string = ''; wait: boolean = false): cardinal;
function DSiImpersonateUser(const username, password, domain: string): boolean;
procedure DSiStopImpersonatingUser;
1.0: 2002-11-25
- Released.
*)
unit DSiWin32;
{$J+} // required!
interface
{$IFDEF Linux}{$MESSAGE FATAL 'This unit is for Windows only'}{$ENDIF Linux}
{$IFDEF MSWindows}{$WARN SYMBOL_PLATFORM OFF}{$WARN UNIT_PLATFORM OFF}{$ENDIF MSWindows}
{$DEFINE NeedUTF}
{$IFDEF ConditionalExpressions}{$UNDEF NeedUTF}{$ENDIF}
uses
Windows,
{$IFDEF ConditionalExpressions}
Variants,
{$ENDIF}
SysUtils,
ShlObj,
Classes,
Graphics,
Registry;
// TODO 3 -oPrimoz Gabrijelcic: Settlement APIs - TDM 86
// TODO 3 -oPrimoz Gabrijelcic: ForegrounWindow trick: WDJ
const
// pretty wrappers
WAIT_OBJECT_1 = WAIT_OBJECT_0+1;
WAIT_OBJECT_2 = WAIT_OBJECT_0+2;
WAIT_OBJECT_3 = WAIT_OBJECT_0+3;
WAIT_OBJECT_4 = WAIT_OBJECT_0+4;
WAIT_OBJECT_5 = WAIT_OBJECT_0+5;
WAIT_OBJECT_6 = WAIT_OBJECT_0+6;
WAIT_OBJECT_7 = WAIT_OBJECT_0+7;
WAIT_OBJECT_8 = WAIT_OBJECT_0+8;
WAIT_OBJECT_9 = WAIT_OBJECT_0+9;
// folder constants missing from ShellObj in Delphi 5
CSIDL_APPDATA = $001A; // Application Data, new for NT4
CSIDL_LOCAL_APPDATA = $001C; // non roaming, user\Local Settings\Application Data
CSIDL_INTERNET_CACHE = $0020;
CSIDL_COOKIES = $0021;
CSIDL_HISTORY = $0022;
CSIDL_COMMON_APPDATA = $0023; // All Users\Application Data
CSIDL_WINDOWS = $0024; // GetWindowsDirectory()
CSIDL_SYSTEM = $0025; // GetSystemDirectory()
CSIDL_PROGRAM_FILES = $0026; // C:\Program Files
CSIDL_MYPICTURES = $0027; // My Pictures, new for Win2K
CSIDL_PROGRAM_FILES_COMMON = $002b; // C:\Program Files\Common
CSIDL_COMMON_DOCUMENTS = $002e; // All Users\Documents
CSIDL_COMMON_ADMINTOOLS = $002f; // All Users\Start Menu\Programs\Administrative Tools
CSIDL_ADMINTOOLS = $0030; // <user name>\Start Menu\Programs\Administrative Tools
CSIDL_FLAG_CREATE = $8000; // new for Win2K, OR this in to force creation of folder
FILE_DEVICE_FILE_SYSTEM = 9;
FILE_DEVICE_MASS_STORAGE = $2D;
METHOD_BUFFERED = 0;
FILE_ANY_ACCESS = 0;
FILE_READ_ACCESS = 1;
FILE_WRITE_ACCESS = 2;
IOCTL_STORAGE_EJECT_MEDIA = (FILE_DEVICE_MASS_STORAGE shl 16) OR
(FILE_READ_ACCESS shl 14) OR
($202 shl 2) OR
(METHOD_BUFFERED);
IOCTL_STORAGE_LOAD_MEDIA = (FILE_DEVICE_MASS_STORAGE shl 16) OR
(FILE_READ_ACCESS shl 14) OR
($203 shl 2) OR
(METHOD_BUFFERED);
FSCTL_SET_COMPRESSION = (FILE_DEVICE_FILE_SYSTEM shl 16) OR
((FILE_READ_ACCESS OR FILE_WRITE_ACCESS) shl 14) OR
(16 shl 2) OR
(METHOD_BUFFERED);
COMPRESSION_FORMAT_NONE = 0;
COMPRESSION_FORMAT_DEFAULT = 1;
SPI_GETFOREGROUNDLOCKTIMEOUT = $2000;
SPI_SETFOREGROUNDLOCKTIMEOUT = $2001;
STYPE_DISKTREE = 0;
SHI50F_RDONLY = $0001;
SHI50F_FULL = $0002;
SHI50F_DEPENDSON = SHI50F_RDONLY or SHI50F_FULL;
SHI50F_ACCESSMASK = SHI50F_RDONLY or SHI50F_FULL;
// IPersisteFile GUID
IID_IPersistFile: TGUID = (
D1: $0000010B; D2: $0000; D3: $0000; D4: ($C0, $00, $00, $00, $00, $00, $00, $46));
// Extension for shortcut files
CLinkExt = '.lnk';
// ShEmptyRecycleBinA flags
SHERB_NOCONFIRMATION = $00000001;
SHERB_NOPROGRESSUI = $00000002;
SHERB_NOSOUND = $00000004;
// CurrentVersion registry key
DSiWinVerKey9x = '\Software\Microsoft\Windows\CurrentVersion';
DSiWinVerKeyNT = '\Software\Microsoft\Windows NT\CurrentVersion';
DSiWinVerKeys: array [boolean] of string = (DSiWinVerKey9x, DSiWinVerKeyNT);
// CPU IDs for the Affinity familiy of functions
DSiCPUIDs = '0123456789ABCDEFGHIJKLMNOPQRSTUV';
type
// API types not defined in Delphi 5
PWkstaInfo100 = ^TWkstaInfo100;
_WKSTA_INFO_100 = record
wki100_platform_id: DWORD;
wki100_computername: LPWSTR;
wki100_langroup: LPWSTR;
wki100_ver_major: DWORD;
wki100_ver_minor: DWORD;
end;
{$EXTERNALSYM _WKSTA_INFO_100}
TWkstaInfo100 = _WKSTA_INFO_100;
WKSTA_INFO_100 = _WKSTA_INFO_100;
{$EXTERNALSYM WKSTA_INFO_100}
SHARE_INFO_2_NT = record
shi2_netname: PWideChar;
shi2_type: Integer;
shi2_remark: PWideChar;
shi2_permissions: Integer;
shi2_max_uses: Integer;
shi2_current_uses: Integer;
shi2_path: PWideChar;
shi2_passwd: PWideChar;
end;
SHARE_INFO_50_9x = record
shi50_netname: array[1..13] of char;
shi50_type: byte;
shi50_flags: short;
shi50_remark: pchar;
shi50_path: pchar;
shi50_rw_password: array[1..9] of char;
shi50_ro_password: array[1..9] of char;
szWhatever: array[1..256] of char;
end;
// Service Controller handle
SC_HANDLE = THandle;
// DSiEnumFiles callback
TDSiEnumFilesCallback = procedure(const longFileName: string) of object;
// DSiEnumFilesEx callback
TDSiEnumFilesExCallback = procedure(const folder: string; S: TSearchRec;
isAFolder: boolean; var stopEnum: boolean) of object;
TDSiFileTime = (ftCreation, ftLastAccess, ftLastModification);
{ Handles }
// Pretty-print aliases
TDSiFileHandle = THandle;
TDSiPipeHandle = THandle;
TDSiMutexHandle = THandle;
TDSiEventHandle = THandle;
TDSiSemaphoreHandle = THandle;
procedure DSiCloseHandleAndInvalidate(var handle: THandle);
procedure DSiCloseHandleAndNull(var handle: THandle);
function DSiMsgWaitForThreeObjectsEx(obj0, obj1, obj2: THandle;
timeout: DWORD; wakeMask: DWORD; flags: DWORD): DWORD;
function DSiMsgWaitForTwoObjectsEx(obj0, obj1: THandle; timeout: DWORD;
wakeMask: DWORD; flags: DWORD): DWORD;
function DSiWaitForThreeObjects(obj0, obj1, obj2: THandle; waitAll: boolean;
timeout: DWORD): DWORD;
function DSiWaitForThreeObjectsEx(obj0, obj1, obj2: THandle; waitAll: boolean;
timeout: DWORD; alertable: boolean): DWORD;
function DSiWaitForTwoObjects(obj0, obj1: THandle; waitAll: boolean;
timeout: DWORD): DWORD;
function DSiWaitForTwoObjectsEx(obj0, obj1: THandle; waitAll: boolean;
timeout: DWORD; alertable: boolean): DWORD;
function DSiWin32CheckHandle(handle: THandle): THandle;
function DSiWin32CheckNullHandle(handle: THandle): THandle;
{ Registry }
type
TDSiRegistry = class(TRegistry)
function ReadBinary(const name, defval: string): string; overload;
function ReadBinary(const name: string; dataStream: TStream): boolean; overload;
function ReadBool(const name: string; defval: boolean): boolean;
function ReadDate(const name: string; defval: TDateTime): TDateTime;
function ReadFont(const name: string; font: TFont): boolean;
function ReadInt64(const name: string; defval: int64): int64;
function ReadInteger(const name: string; defval: integer): integer;
function ReadString(const name, defval: string): string;
procedure ReadStrings(const name: string; strings: TStrings);
function ReadVariant(const name: string; defval: variant): variant;
procedure WriteBinary(const name, data: string); overload;
procedure WriteBinary(const name: string; data: TStream); overload;
procedure WriteFont(const name: string; font: TFont);
procedure WriteInt64(const name: string; value: int64);
procedure WriteStrings(const name: string; strings: TStrings);
procedure WriteVariant(const name: string; value: variant);
end; { TDSiRegistry }
function DSiCreateRegistryKey(const registryKey: string;
root: HKEY = HKEY_CURRENT_USER): boolean;
function DSiKillRegistry(const registryKey: string;
root: HKEY = HKEY_CURRENT_USER): boolean;
function DSiReadRegistry(const registryKey, name: string;
defaultValue: Variant; root: HKEY = HKEY_CURRENT_USER): Variant; overload;
function DSiReadRegistry(const registryKey, name: string;
defaultValue: int64; root: HKEY = HKEY_CURRENT_USER): int64; overload;
function DSiRegistryKeyExists(const registryKey: string;
root: HKEY = HKEY_CURRENT_USER): boolean;
function DSiRegistryValueExists(const registryKey, name: string;
root: HKEY = HKEY_CURRENT_USER): boolean;
function DSiWriteRegistry(const registryKey, name: string; value: int64;
root: HKEY = HKEY_CURRENT_USER): boolean; overload;
function DSiWriteRegistry(const registryKey, name: string; value: Variant;
root: HKEY = HKEY_CURRENT_USER): boolean; overload;
{ Files }
function DSiCanWriteToFolder(const folderName: string): boolean;
function DSiCompressFile(fileHandle: THandle): boolean;
function DSiCreateTempFolder: string;
procedure DSiDeleteFiles(const folder, fileMask: string);
function DSiDeleteOnReboot(const fileName: string): boolean;
procedure DSiDeleteTree(const folder: string; removeSubdirsOnly: boolean);
function DSiDeleteWithBatch(const fileName: string; rmDir: boolean = false): boolean;
function DSiDirectoryExistsW(const directory: WideString): boolean;
function DSiEjectMedia(deviceLetter: char): boolean;
procedure DSiEmptyFolder(const folder: string);
function DSiEmptyRecycleBin: boolean;
function DSiEnumFiles(const fileMask: string; attr: integer;
enumCallback: TDSiEnumFilesCallback): integer;
function DSiEnumFilesEx(const fileMask: string; attr: integer;
enumSubfolders: boolean; enumCallback: TDSiEnumFilesExCallback): integer;
procedure DSiEnumFilesToSL(const fileMask: string; attr: integer; fileList: TStrings;
storeFullPath: boolean = false; enumSubfolders: boolean = false);
function DSiFileExistsW(const fileName: WideString): boolean;
function DSiFileSize(const fileName: string): int64;
function DSiFileSizeW(const fileName: WideString): int64;
function DSiGetFileTime(const fileName: string; whatTime: TDSiFileTime): TDateTime;
function DSiGetLongPathName(const fileName: string): string;
function DSiGetTempFileName(const prefix: string; const tempPath: string = ''): string;
function DSiGetTempPath: string;
function DSiGetUniqueFileName(const extension: string): string;
function DSiIsFileCompressed(const fileName: string): boolean;
function DSiIsFileCompressedW(const fileName: WideString): boolean;
function DSiKillFile(const fileName: string): boolean;
function DSiLoadMedia(deviceLetter: char): boolean;
function DSiMoveOnReboot(const srcName, destName: string): boolean;
procedure DSiRemoveFolder(const folder: string);
function DSiShareFolder(const folder, shareName, comment: string): boolean;
function DSiUncompressFile(fileHandle: THandle): boolean;
function DSiUnShareFolder(const shareName: string): boolean;
{ Processes }
function DSiAffinityMaskToString(affinityMask: DWORD): string;
function DSiEnablePrivilege(const privilegeName: string): boolean;
function DSiExecute(const commandLine: string;
visibility: integer = SW_SHOWDEFAULT; const workDir: string = '';
wait: boolean = false): cardinal;
function DSiExecuteAndCapture(const app: string; output: TStrings;
const workDir: string; var exitCode: longword): cardinal;
function DSiExecuteAsUser(const commandLine, username, password: string;
const domain: string = '.'; visibility: integer = SW_SHOWDEFAULT;
const workDir: string = ''; wait: boolean = false): cardinal;
function DSiGetProcessAffinity: string;
function DSiGetProcessAffinityMask: DWORD;
function DSiGetProcessID(const processName: string; var processID: DWORD): boolean;
function DSiGetProcessOwnerInfo(const processName: string; var user,
domain: string): boolean; overload;
function DSiGetProcessOwnerInfo(processID: DWORD; var user,
domain: string): boolean; overload;
function DSiGetSystemAffinity: string;
function DSiGetSystemAffinityMask: DWORD;
function DSiGetThreadAffinity: string;
function DSiGetThreadAffinityMask: DWORD;
function DSiImpersonateUser(const username, password: string;
const domain: string = '.'): boolean;
function DSiIncrementWorkingSet(incMinSize, incMaxSize: integer): boolean;
function DSiIsDebugged: boolean;
function DSiOpenURL(const URL: string; newBrowser: boolean = false): boolean;
procedure DSiProcessThreadMessages;
function DSiRealModuleName: string;
function DSiSetProcessAffinity(affinity: string): string;
function DSiSetProcessPriorityClass(const processName: string;
priority: DWORD): boolean;
function DSiSetThreadAffinity(affinity: string): string;
procedure DSiStopImpersonatingUser;
function DSiStringToAffinityMask(affinity: string): DWORD;
procedure DSiTrimWorkingSet;
function DSiValidateProcessAffinity(affinity: string): string;
function DSiValidateProcessAffinityMask(affinityMask: DWORD): DWORD;
function DSiValidateThreadAffinity(affinity: string): string;
function DSiValidateThreadAffinityMask(affinityMask: DWORD): DWORD;
{ Memory }
procedure DSiFreePidl(pidl: PItemIDList);
procedure DSiFreeMemAndNil(var mem: pointer);
{ Windows }
type
TDSiExitWindows = (ewLogOff, ewForcedLogOff, ewPowerOff, ewForcedPowerOff, ewReboot,
ewForcedReboot, ewShutdown, ewForcedShutdown);
procedure DSiDisableX(hwnd: THandle);
procedure DSiEnableX(hwnd: THandle);
function DSiExitWindows(exitType: TDSiExitWindows): boolean;
function DSiForceForegroundWindow(hwnd: THandle;
restoreFirst: boolean = true): boolean;
function DSiGetClassName(hwnd: THandle): string;
function DSiGetProcessWindow(targetProcessID: cardinal): HWND;
function DSiGetWindowText(hwnd: THandle): string;
procedure DSiProcessMessages(hwnd: THandle; waitForWMQuit: boolean = false);
procedure DSiRebuildDesktopIcons;
procedure DSiRefreshDesktop;
procedure DSiSetTopMost(hwnd: THandle; onTop: boolean = true;
activate: boolean = false);
{ Taskbar }
function DSiGetTaskBarPosition: integer;
{ Menus }
function DSiGetHotkey(const item: string): char;
function DSiGetMenuItem(menu: HMENU; item: integer): string;
{ Screen }
procedure DSiDisableScreenSaver(out currentlyActive: boolean);
procedure DSiEnableScreenSaver;
function DSiGetBitsPerPixel: integer;
function DSiGetBPP: integer;
function DSiGetDesktopSize: TRect;
function DSiIsFullScreen: boolean;
procedure DSiMonitorOff;
procedure DSiMonitorOn;
procedure DSiMonitorStandby;
function DSiSetScreenResolution(width, height: integer): longint;
{ Information }
type
TDSiBootType = (btNormal, btFailSafe, btFailSafeWithNetwork, btUnknown);
TDSiWindowsVersion = (wvUnknown, wvWin31, wvWin95, wvWin95OSR2, wvWin98,
wvWin98SE, wvWinME, wvWin9x, wvWinNT3, wvWinNT4, wvWin2000, wvWinXP,
wvWinNT, wvWinServer2003, wvWinVista);
const
CDSiWindowsVersionStr: array [TDSiWindowsVersion] of string = ('Unknown',
'Windows 3.1', 'Windows 95', 'Windows 95 OSR 2', 'Windows 98',
'Windows 98 SE', 'Windows Me', 'Windows 9x', 'Windows NT 3.5',
'Windows NT 4', 'Windows 2000', 'Windows XP', 'Windows NT', 'Windows Server 2003',
'Windows Vista');
function DSiGetBootType: TDSiBootType;
function DSiGetCompanyName: string;
function DSiGetComputerName: string;
function DSiGetDefaultBrowser: string;
function DSiGetDirectXVer: string;
function DSiGetDiskLabel(disk: char): string;
function DSiGetDiskSerial(disk: char): string;
function DSiGetDomain: string;
function DSiGetEnvironmentVariable(const envVarName: string): string;
function DSiGetFolderLocation(const CSIDL: integer): string;
procedure DSiGetKeyboardLayouts(layouts: TStrings);
function DSiGetMyDocumentsFolder: string;
function DSiGetProgramFilesFolder: string;
function DSiGetRegisteredOwner: string;
function DSiGetSystemFolder: string;
function DSiGetSystemLanguage: string;
function DSiGetSystemVersion: string;
function DSiGetUserName: string;
function DSiGetUserNameEx: string;
function DSiGetWindowsFolder: string;
function DSiGetWindowsVersion: TDSiWindowsVersion;
function DSiIsAdminLoggedOn: boolean;
function DSiIsDiskInDrive(disk: char): boolean;
function DSiIsWinNT: boolean;
{ Install }
function DSiAddUninstallInfo(const displayName, uninstallCommand, publisher,
URLInfoAbout, displayVersion, helpLink, URLUpdateInfo: string): boolean;
function DSiAutoRunApp(const applicationName, applicationPath: string;
enabled: boolean = true): boolean;
procedure DSiCreateShortcut(const fileName, displayName: string;
folder: integer = CSIDL_STARTUP; const workDir: string = '');
function DSiDeleteShortcut(const displayName: string;
folder: integer = CSIDL_STARTUP): boolean;
function DSiGetShortcutInfo(const lnkName: string; var fileName, filePath, workDir:
string): boolean;
function DSiGetUninstallInfo(const displayName: string;
out uninstallCommand: string): boolean;
function DSiIsAutoRunApp(const applicationname: string): boolean;
function DSiRegisterActiveX(const fileName: string; registerDLL: boolean): HRESULT;
procedure DSiRegisterRunOnce(const applicationName,
applicationPath: string);
procedure DSiRemoveRunOnce(const applicationName: string);
function DSiRemoveUninstallInfo(const displayName: string): boolean;
function DSiShortcutExists(const displayName: string;
folder: integer = CSIDL_STARTUP): boolean;
{ Time }
function DSiElapsedSince(midTime, startTime: int64): int64;
function DSiElapsedTime(startTime: int64): int64;
function DSiHasElapsed(startTime: int64; timeout: DWORD): boolean;
procedure DSiuSecDelay(delay: int64);
{ DynaLoad }
function DSi9xNetShareAdd(serverName: PChar; shareLevel: smallint;
buffer: pointer; size: word): integer; stdcall;
function DSi9xNetShareDel(serverName: PChar; netName: PChar;
reserved: word): integer; stdcall;
function DSiCloseServiceHandle(hSCObject: SC_HANDLE): BOOL; stdcall;
function DSiCreateProcessAsUser(hToken: THandle;
lpApplicationName: PAnsiChar; lpCommandLine: PAnsiChar; lpProcessAttributes,
lpThreadAttributes: PSecurityAttributes; bInheritHandles: BOOL;
dwCreationFlags: DWORD; lpEnvironment: pointer;
lpCurrentDirectory: PAnsiChar; const lpStartupInfo: TStartupInfo;
var lpProcessInformation: TProcessInformation): BOOL; stdcall;
function DSiImpersonateLoggedOnUser(hToken: THandle): BOOL; stdcall;
function DSiLogonUser(lpszUsername, lpszDomain, lpszPassword: LPCSTR;
dwLogonType, dwLogonProvider: DWORD; var phToken: THandle): BOOL; stdcall;
function DSiNetApiBufferFree(buffer: pointer): cardinal; stdcall;
function DSiNetWkstaGetInfo(servername: PChar; level: cardinal;
out bufptr: Pointer): cardinal; stdcall;
function DSiNTNetShareAdd(serverName: PChar; level: integer; buf: PChar;
var parm_err: integer): DWord; stdcall;
function DSiNTNetShareDel(serverName: PChar; netName: PWideChar;
reserved: integer): DWord; stdcall;
function DSiOpenSCManager(lpMachineName, lpDatabaseName: PChar;
dwDesiredAccess: DWORD): SC_HANDLE; stdcall;
function DSiRevertToSelf: BOOL; stdcall;
function DSiSetDllDirectory(path: PChar): boolean; stdcall;
function DSiSHEmptyRecycleBin(Wnd: HWND; pszRootPath: PChar;
dwFlags: DWORD): HRESULT; stdcall;
{ Helpers }
{$IFDEF NeedUTF}
// UTF <-> 16-bit conversion. Same signature as D7 functions but custom implementation
// (taken from http://gp.17slon.com/gp/gptextstream.htm with permission).
type
UTF8String = type string;
PUTF8String = ^UTF8String;
function UTF8Encode(const ws: WideString): UTF8String;
function UTF8Decode(const sUtf: UTF8String): WideString;
{$ENDIF NeedUTF}
implementation
uses
Messages,
ShellAPI,
ComObj,
ActiveX,
FileCtrl,
{$IFDEF CONDITIONALCOMPILATION}
Variants,
{$ENDIF}
TLHelp32;
type
T9xNetShareAdd = function(serverName: PChar; shareLevel: smallint;
buffer: pointer; size: word): integer; stdcall;
T9xNetShareDel = function(serverName: PChar; netName: PChar;
reserved: word): integer; stdcall;
TCloseServiceHandle = function(hSCObject: SC_HANDLE): BOOL; stdcall;
TCreateProcessAsUser = function(hToken: THandle;
lpApplicationName: PAnsiChar; lpCommandLine: PAnsiChar; lpProcessAttributes,
lpThreadAttributes: PSecurityAttributes; bInheritHandles: BOOL;
dwCreationFlags: DWORD; lpEnvironment: pointer;
lpCurrentDirectory: PAnsiChar; const lpStartupInfo: TStartupInfo;
var lpProcessInformation: TProcessInformation): BOOL; stdcall;
TGetLongPathName = function(lpszShortPath, lpszLongPath: PChar;
cchBuffer: DWORD): DWORD; stdcall;
TImpersonateLoggedOnUser = function(hToken: THandle): BOOL; stdcall;
TLogonUser = function(lpszUsername, lpszDomain, lpszPassword: LPCSTR;
dwLogonType, dwLogonProvider: DWORD; var phToken: THandle): BOOL; stdcall;
TNetApiBufferFree = function(buffer: pointer): cardinal; stdcall;
TNetWkstaGetInfo = function(servername: PChar; level: cardinal;
out bufptr: Pointer): cardinal; stdcall;
TNTNetShareAdd = function(serverName: PChar; level: integer; buf: PChar;
var parm_err: integer): DWord; stdcall;
TNTNetShareDel = function(serverName: PChar; netName: PWideChar;
reserved: integer): DWord; stdcall;
TOpenSCManager = function(lpMachineName, lpDatabaseName: PChar;
dwDesiredAccess: DWORD): SC_HANDLE; stdcall;
TRevertToSelf = function: BOOL; stdcall;
TSHEmptyRecycleBin = function(wnd: HWND; pszRootPath: PChar;
dwFlags: DWORD): HRESULT; stdcall;
TSetDllDirectory = function(path: PChar): boolean; stdcall;
const
G9xNetShareAdd: T9xNetShareAdd = nil;
G9xNetShareDel: T9xNetShareDel = nil;
GCloseServiceHandle: TCloseServiceHandle = nil;
GCreateProcessAsUser: TCreateProcessAsUser = nil;
GGetLongPathName: TGetLongPathName = nil;
GImpersonateLoggedOnUser: TImpersonateLoggedOnUser = nil;
GLogonUser: TLogonUser = nil;
GNetApiBufferFree: TNetApiBufferFree = nil;
GNetWkstaGetInfo: TNetWkstaGetInfo = nil;
GNTNetShareAdd: TNTNetShareAdd = nil;
GNTNetShareDel: TNTNetShareDel = nil;
GOpenSCManager: TOpenSCManager = nil;
GRevertToSelf: TRevertToSelf = nil;
GSetDllDirectory: TSetDllDirectory = nil;
GSHEmptyRecycleBin: TSHEmptyRecycleBin = nil;
function DSiGetProcAddress(const libFileName, procName: string): FARPROC; forward;
{ Helpers }
function FileOpenSafe(fileName: string; var fileHandle: textfile;
diskRetryDelay, diskRetryCount: integer): boolean;
var
dum: integer;
begin
Assign (fileHandle, fileName);
{$I-}
repeat
if FileExists(fileName) then
Reset(fileHandle)
else
Rewrite (fileHandle);
dum := IOResult;
if (dum in [ERROR_SHARING_VIOLATION, ERROR_LOCK_VIOLATION]) and
(diskRetryDelay > 0) then
begin
Sleep (diskRetryDelay);
if diskRetryCount > 0 then Dec(diskRetryCount);
end;
until (not (dum in [ERROR_SHARING_VIOLATION, ERROR_LOCK_VIOLATION])) or
(diskRetryCount = 0);
{$I+}
Result := (dum = 0);
end; { FileOpenSafe }
{$IFDEF NeedUTF}
{:Convers buffer of WideChars into UTF-8 encoded form. Target buffer must be
pre-allocated and large enough (each WideChar will use at most three bytes
in UTF-8 encoding). <br>
RFC 2279 (http://www.ietf.org/rfc/rfc2279.txt) describes the conversion: <br>
$0000..$007F => $00..$7F <br>
$0080..$07FF => 110[bit10..bit6] 10[bit5..bit0] <br>
$0800..$FFFF => 1110[bit15..bit12] 10[bit11..bit6] 10[bit5..bit0]
@param unicodeBuf Buffer of WideChars.
@param uniByteCount Size of unicodeBuf, in bytes.
@param utf8Buf Pre-allocated buffer for UTF-8 encoded result.
@returns Number of bytes used in utf8Buf buffer.
@since 2.01
}
function WideCharBufToUTF8Buf(const unicodeBuf; uniByteCount: integer;
var utf8Buf): integer;
var
iwc: integer;
pch: PChar;
pwc: PWideChar;
wc : word;
procedure AddByte(b: byte);
begin
pch^ := char(b);
Inc(pch);
end; { AddByte }
begin { WideCharBufToUTF8Buf }
pwc := @unicodeBuf;
pch := @utf8Buf;
for iwc := 1 to uniByteCount div SizeOf(WideChar) do begin
wc := Ord(pwc^);
Inc(pwc);
if (wc >= $0001) and (wc <= $007F) then begin
AddByte(wc AND $7F);
end
else if (wc >= $0080) and (wc <= $07FF) then begin
AddByte($C0 OR ((wc SHR 6) AND $1F));
AddByte($80 OR (wc AND $3F));
end
else begin // (wc >= $0800) and (wc <= $FFFF)
AddByte($E0 OR ((wc SHR 12) AND $0F));
AddByte($80 OR ((wc SHR 6) AND $3F));
AddByte($80 OR (wc AND $3F));
end;
end; //for
Result := integer(pch)-integer(@utf8Buf);
end; { WideCharBufToUTF8Buf }
{:Converts UTF-8 encoded buffer into WideChars. Target buffer must be
pre-allocated and large enough (at most utfByteCount number of WideChars will
be generated). <br>
RFC 2279 (http://www.ietf.org/rfc/rfc2279.txt) describes the conversion: <br>
$00..$7F => $0000..$007F <br>
110[bit10..bit6] 10[bit5..bit0] => $0080..$07FF <br>
1110[bit15..bit12] 10[bit11..bit6] 10[bit5..bit0] => $0800..$FFFF
@param utf8Buf UTF-8 encoded buffer.
@param utfByteCount Size of utf8Buf, in bytes.
@param unicodeBuf Pre-allocated buffer for WideChars.
@param leftUTF8 Number of bytes left in utf8Buf after conversion (0, 1,
or 2).
@returns Number of bytes used in unicodeBuf buffer.
@since 2.01
}
function UTF8BufToWideCharBuf(const utf8Buf; utfByteCount: integer;
var unicodeBuf; var leftUTF8: integer): integer;
var
c1 : byte;
c2 : byte;
ch : byte;
pch: PChar;
pwc: PWideChar;
begin
pch := @utf8Buf;
pwc := @unicodeBuf;
leftUTF8 := utfByteCount;
while leftUTF8 > 0 do begin
ch := byte(pch^);
Inc(pch);
if (ch AND $80) = 0 then begin // 1-byte code
word(pwc^) := ch;
Inc(pwc);
Dec(leftUTF8);
end
else if (ch AND $E0) = $C0 then begin // 2-byte code
if leftUTF8 < 2 then
break;
c1 := byte(pch^);
Inc(pch);
word(pwc^) := (word(ch AND $1F) SHL 6) OR (c1 AND $3F);
Inc(pwc);
Dec(leftUTF8,2);
end
else begin // 3-byte code
if leftUTF8 < 3 then
break;
c1 := byte(pch^);
Inc(pch);
c2 := byte(pch^);
Inc(pch);
word(pwc^) :=
(word(ch AND $0F) SHL 12) OR
(word(c1 AND $3F) SHL 6) OR
(c2 AND $3F);
Inc(pwc);
Dec(leftUTF8,3);
end;
end; //while
Result := integer(pwc)-integer(@unicodeBuf);
end; { UTF8BufToWideCharBuf }
function UTF8Encode(const ws: WideString): UTF8String;
begin
if ws = '' then
Result := ''
else begin
SetLength(Result, Length(ws)*3); // worst case - 3 bytes per character
SetLength(Result, WideCharBufToUTF8Buf(ws[1], Length(ws)*SizeOf(WideChar),
Result[1]));
end;
end; { UTF8Encode }
function UTF8Decode(const sUtf: UTF8String): WideString;
var
leftUtf: integer;
begin
if sUtf = '' then
Result := ''
else begin
SetLength(Result, Length(sUtf)); // worst case - 1 widechar per character
SetLength(Result, UTF8BufToWideCharBuf(sUtf[1], Length(sUtf), Result[1], leftUtf)
div SizeOf(WideChar));
end;
end; { UTF8Decode }
{$ENDIF NeedUTF}
{ Handles }
{:Closes handle (if it is not already INVALID_HANDLE_VALUE) and sets it to
INVALID_HANDLE_VALUE.
@author gabr
@since 2002-11-25
}
procedure DSiCloseHandleAndInvalidate(var handle: THandle);
begin
if handle <> INVALID_HANDLE_VALUE then begin
CloseHandle(handle);
handle := INVALID_HANDLE_VALUE;
end;
end; { DSiCloseHandleAndInvalidate }
{:Closes handle (if it is not already 0) and sets it to 0.
@author gabr
@since 2002-11-25
}
procedure DSiCloseHandleAndNull(var handle: THandle);
begin
if handle <> 0 then begin
CloseHandle(handle);
handle := 0;
end;
end; { DSiCloseHandleAndNull }
{:Shortcut for WaitForMultipleObjects with two objects.
@author gabr
@since 2002-11-25
}
function DSiWaitForTwoObjects(obj0, obj1: THandle; waitAll: boolean;
timeout: DWORD): DWORD;
var
handles: array [0..1] of THandle;
begin
handles[0] := obj0;
handles[1] := obj1;
Result := WaitForMultipleObjects(2, @handles, waitAll, timeout);
end; { DSiWaitForTwoObjects }
{:Shortcut for WaitForMultipleObjectsEx with two objects.
@author gabr
@since 2002-11-25
}
function DSiWaitForTwoObjectsEx(obj0, obj1: THandle; waitAll: boolean;
timeout: DWORD; alertable: boolean): DWORD;
var
handles: array [0..1] of THandle;
begin
handles[0] := obj0;
handles[1] := obj1;
Result := WaitForMultipleObjectsEx(2, @handles, waitAll, timeout, alertable);
end; { DSiWaitForTwoObjectsEx }
{:As Win32Check, only used for file handles.
@author gabr
@since 2003-11-12
}
function DSiWin32CheckHandle(handle: THandle): THandle;
begin
Win32Check(handle <> INVALID_HANDLE_VALUE);
Result := handle;
end; { TDSiRegistry.DSiWin32CheckHandle }
{:As Win32Check, only used for various handles.
@author gabr
@since 2005-07-11
}
function DSiWin32CheckNullHandle(handle: THandle): THandle;
begin
Win32Check(handle <> 0);
Result := handle;
end; { TDSiRegistry.DSiWin32CheckNullHandle }
{:Shortcut for MsgWaitForMultipleObjects with two objects.
@author gabr
@since 2002-11-25
}
function DSiMsgWaitForTwoObjectsEx(obj0, obj1: THandle; timeout: DWORD;
wakeMask: DWORD; flags: DWORD): DWORD;
var
handles: array [0..1] of THandle;
begin
handles[0] := obj0;
handles[1] := obj1;
Result := MsgWaitForMultipleObjectsEx(2, handles, timeout, wakeMask, flags);
end; { DSiWaitForThreeObjects }
{:Shortcut for WaitForMultipleObjects with three objects.
@author gabr
@since 2002-11-25
}
function DSiWaitForThreeObjects(obj0, obj1, obj2: THandle; waitAll: boolean;
timeout: DWORD): DWORD;
var
handles: array [0..2] of THandle;
begin
handles[0] := obj0;
handles[1] := obj1;
handles[2] := obj2;
Result := WaitForMultipleObjects(3, @handles, waitAll, timeout);
end; { DSiWaitForThreeObjects }
{:Shortcut for WaitForMultipleObjectsEx with three objects.
@author gabr
@since 2002-11-25
}
function DSiWaitForThreeObjectsEx(obj0, obj1, obj2: THandle; waitAll: boolean;
timeout: DWORD; alertable: boolean): DWORD;
var
handles: array [0..2] of THandle;
begin
handles[0] := obj0;
handles[1] := obj1;
handles[2] := obj2;
Result := WaitForMultipleObjectsEx(3, @handles, waitAll, timeout, alertable);
end; { DSiWaitForThreeObjectsEx }
{:Shortcut for MsgWaitForMultipleObjectsEx with three objects.
@author gabr
@since 2002-11-25
}
function DSiMsgWaitForThreeObjectsEx(obj0, obj1, obj2: THandle;
timeout: DWORD; wakeMask: DWORD; flags: DWORD): DWORD;
var
handles: array [0..2] of THandle;
begin
handles[0] := obj0;
handles[1] := obj1;
handles[2] := obj2;
Result := MsgWaitForMultipleObjectsEx(3, handles, timeout, wakeMask, flags);
end; { DSiWaitForThreeObjectsEx }
{ Registry }
{:Reads binary from the registry returning default value if name doesn't exist in the
open key. Includes special handling for integer and string keys.
@author Lee_Nover
@since 2004-11-29
}
function TDSiRegistry.ReadBinary(const name, defval: string): string;
begin
try
if GetDataSize(name) < 0 then
Abort; // D4 does not generate an exception!
case GetDataType(name) of
rdInteger:
Result := IntToStr(inherited ReadInteger(name));
rdBinary:
begin
SetLength(Result, GetDataSize(name));
SetLength(Result, ReadBinaryData(name, Pointer(Result)^, Length(Result)));
end; //rdBinary
else
Result := inherited ReadString(name);
end;
except ReadBinary := defval; end;
end; { TDSiRegistry.ReadBinary }
{:Reads binary from the registry. Overwrites 'dataStream'. Keeps data stream unchanged
if value is not found in the registry.
Includes special handling for integer and string keys.
@author gabr
@returns True if value exists in the registry.
@since 2005-02-13
}
function TDSiRegistry.ReadBinary(const name: string; dataStream: TStream): boolean;
var
i: integer;
s: string;
begin