forked from rofl0r/KOL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkolregistry.pas
1017 lines (934 loc) · 27.2 KB
/
kolregistry.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 kolregistry;
// purpose: a KOL Registry object that mimics almost exactly VCL registry unit
// behaveour.
// author: KOL version © 2004, Thaddy de Koning
// Portions © 1995-1997 Borland Software Corporation
// Based on the Delphi3 professional sourcecode as distributed free with
// the Dutch magazine PC-Active in 2000.
{.$DEFINE USE_ERR} // If you want true exception handling
interface
uses
windows, messages,kol{$IFDEF USE_ERR},err{$ENDIF};
type
TSysLocale = packed record
DefaultLCID: Integer;
PriLangID: Integer;
SubLangID: Integer;
FarEast: Boolean;
MiddleEast: Boolean;
end;
TRegKeyInfo = record
NumSubKeys: Integer;
MaxSubKeyLen: Integer;
NumValues: Integer;
MaxValueLen: Integer;
MaxDataLen: Integer;
FileTime: TFileTime;
end;
TRegDataType = (rdUnknown, rdString, rdExpandString, rdInteger, rdBinary);
TRegDataInfo = record
RegData: TRegDataType;
DataSize: Integer;
end;
PRegistry=^TRegistry;
TRegistry = object(TObj)
private
FCurrentKey: HKEY;
FRootKey: HKEY;
FLazyWrite: Boolean;
FCurrentPath: string;
FCloseRootKey: Boolean;
FAccess: LongWord;
procedure SetRootKey(Value: HKEY);
protected
procedure ChangeKey(Value: HKey; const Path: string);
function GetBaseKey(Relative: Boolean): HKey;
function GetData(const Name: string; Buffer: Pointer;
BufSize: Integer; var RegData: TRegDataType): Integer;
function GetKey(const Key: string): HKEY;
procedure PutData(const Name: string; Buffer: Pointer; BufSize: Integer; RegData: TRegDataType);
procedure SetCurrentKey(Value: HKEY);
public
destructor Destroy; virtual;
procedure CloseKey;
function CreateKey(const Key: string): Boolean;
function DeleteKey(const Key: string): Boolean;
function DeleteValue(const Name: string): Boolean;
function GetDataInfo(const ValueName: string; var Value: TRegDataInfo): Boolean;
function GetDataSize(const ValueName: string): Integer;
function GetDataType(const ValueName: string): TRegDataType;
function GetKeyInfo(var Value: TRegKeyInfo): Boolean;
procedure GetKeyNames(Strings: PStrList);
procedure GetValueNames(Strings: PStrList);
function HasSubKeys: Boolean;
function KeyExists(const Key: string): Boolean;
function LoadKey(const Key, FileName: string): Boolean;
procedure MoveKey(const OldName, NewName: string; Delete: Boolean);
function OpenKey(const Key: string; CanCreate: Boolean): Boolean;
function OpenKeyReadOnly(const Key: String): Boolean;
function ReadCurrency(const Name: string): Currency;
function ReadBinaryData(const Name: string; var Buffer; BufSize: Integer): Integer;
function ReadBool(const Name: string): Boolean;
function ReadDate(const Name: string): TDateTime;
function ReadDateTime(const Name: string): TDateTime;
function ReadFloat(const Name: string): Double;
function ReadInteger(const Name: string): Integer;
function ReadString(const Name: string): string;
function ReadTime(const Name: string): TDateTime;
function RegistryConnect(const UNCName: string): Boolean;
procedure RenameValue(const OldName, NewName: string);
function ReplaceKey(const Key, FileName, BackUpFileName: string): Boolean;
function RestoreKey(const Key, FileName: string): Boolean;
function SaveKey(const Key, FileName: string): Boolean;
function UnLoadKey(const Key: string): Boolean;
function ValueExists(const Name: string): Boolean;
procedure WriteCurrency(const Name: string; Value: Currency);
procedure WriteBinaryData(const Name: string; var Buffer; BufSize: Integer);
procedure WriteBool(const Name: string; Value: Boolean);
procedure WriteDate(const Name: string; Value: TDateTime);
procedure WriteDateTime(const Name: string; Value: TDateTime);
procedure WriteFloat(const Name: string; Value: Double);
procedure WriteInteger(const Name: string; Value: Integer);
procedure WriteString(const Name, Value: string);
procedure WriteExpandString(const Name, Value: string);
procedure WriteTime(const Name: string; Value: TDateTime);
property CurrentKey: HKEY read FCurrentKey;
property CurrentPath: string read FCurrentPath;
property LazyWrite: Boolean read FLazyWrite write FLazyWrite;
property RootKey: HKEY read FRootKey write SetRootKey;
property Access: LongWord read FAccess write FAccess;
end;
function NewRegistry:PRegistry;overload;
function NewRegistry(AAccess: LongWord):Pregistry;overload;
function Str2IntDef(const S: string; Default: Integer): Integer;
var
SysLocale:TSysLocale;
LeadBytes: set of Char = [];
implementation
const
SInvalidRegType = 'Invalid data type for ''%s''';
SRegCreateFailed = 'Failed to create key %s';
SRegGetDataFailed = 'Failed to get data for ''%s''';
SRegSetDataFailed = 'Failed to set data for ''%s''';
procedure ReadError(const Name: string);
begin
{$IFDEF USE_ERR}
raise Exception.CreateFmt(e_Custom,SInvalidRegType, [Name]);
{$ELSE}
MsgOk(Format(SInvalidRegType, [Name]));
{$ENDIF}
end;
function IsRelative(const Value: string): Boolean;
begin
Result := not ((Value <> '') and (Value[1] = '\'));
end;
function RegDataToDataType(Value: TRegDataType): Integer;
begin
case Value of
rdString: Result := REG_SZ;
rdExpandString: Result := REG_EXPAND_SZ;
rdInteger: Result := REG_DWORD;
rdBinary: Result := REG_BINARY;
else
Result := REG_NONE;
end;
end;
function DataTypeToRegData(Value: Integer): TRegDataType;
begin
if Value = REG_SZ then Result := rdString
else if Value = REG_EXPAND_SZ then Result := rdExpandString
else if Value = REG_DWORD then Result := rdInteger
else if Value = REG_BINARY then Result := rdBinary
else Result := rdUnknown;
end;
function NewRegistry:PRegistry;overload;
begin
New(Result,Create);
with Result^ do
begin
RootKey := HKEY_CURRENT_USER;
FAccess := KEY_ALL_ACCESS;
LazyWrite := True;
end;
end;
function NewRegistry(AAccess: LongWord):Pregistry;overload;
begin
Result:=NewRegistry;
Result.FAccess := AAccess;
end;
destructor TRegistry.Destroy;
begin
CloseKey;
inherited;
end;
procedure TRegistry.CloseKey;
begin
if CurrentKey <> 0 then
begin
if LazyWrite then
RegCloseKey(CurrentKey) else
RegFlushKey(CurrentKey);
FCurrentKey := 0;
FCurrentPath := '';
end;
end;
procedure TRegistry.SetRootKey(Value: HKEY);
begin
if RootKey <> Value then
begin
if FCloseRootKey then
begin
RegCloseKey(RootKey);
FCloseRootKey := False;
end;
FRootKey := Value;
CloseKey;
end;
end;
procedure TRegistry.ChangeKey(Value: HKey; const Path: string);
begin
CloseKey;
FCurrentKey := Value;
FCurrentPath := Path;
end;
function TRegistry.GetBaseKey(Relative: Boolean): HKey;
begin
if (CurrentKey = 0) or not Relative then
Result := RootKey else
Result := CurrentKey;
end;
procedure TRegistry.SetCurrentKey(Value: HKEY);
begin
FCurrentKey := Value;
end;
function TRegistry.CreateKey(const Key: string): Boolean;
var
TempKey: HKey;
S: string;
Disposition: Integer;
Relative: Boolean;
begin
TempKey := 0;
S := Key;
Relative := IsRelative(S);
if not Relative then Delete(S, 1, 1);
Result := RegCreateKeyEx(GetBaseKey(Relative), PChar(S), 0, nil,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, TempKey, @Disposition) = ERROR_SUCCESS;
if Result then RegCloseKey(TempKey)
else
{$IFDEF USE_ERR}
raise Exception.CreateFmt(e_Custom,SRegCreateFailed, [Key]);
{$ELSE}
MsgOk(Format(SRegCreateFailed, [Key]));
{$ENDIF}
end;
function TRegistry.OpenKey(const Key: String; Cancreate: boolean): Boolean;
var
TempKey: HKey;
S: string;
Disposition: Integer;
Relative: Boolean;
begin
S := Key;
Relative := IsRelative(S);
if not Relative then Delete(S, 1, 1);
TempKey := 0;
if not CanCreate or (S = '') then
begin
Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0,
FAccess, TempKey) = ERROR_SUCCESS;
end else
Result := RegCreateKeyEx(GetBaseKey(Relative), PChar(S), 0, nil,
REG_OPTION_NON_VOLATILE, FAccess, nil, TempKey, @Disposition) = ERROR_SUCCESS;
if Result then
begin
if (CurrentKey <> 0) and Relative then S := CurrentPath + '\' + S;
ChangeKey(TempKey, S);
end;
end;
function TRegistry.OpenKeyReadOnly(const Key: String): Boolean;
var
TempKey: HKey;
S: string;
Relative: Boolean;
begin
S := Key;
Relative := IsRelative(S);
if not Relative then Delete(S, 1, 1);
TempKey := 0;
Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0,
KEY_READ, TempKey) = ERROR_SUCCESS;
if Result then
begin
FAccess := KEY_READ;
if (CurrentKey <> 0) and Relative then S := CurrentPath + '\' + S;
ChangeKey(TempKey, S);
end
else
begin
Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0,
STANDARD_RIGHTS_READ or KEY_QUERY_VALUE or KEY_ENUMERATE_SUB_KEYS,
TempKey) = ERROR_SUCCESS;
if Result then
begin
FAccess := STANDARD_RIGHTS_READ or KEY_QUERY_VALUE or KEY_ENUMERATE_SUB_KEYS;
if (CurrentKey <> 0) and Relative then S := CurrentPath + '\' + S;
ChangeKey(TempKey, S);
end
else
begin
Result := RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0,
KEY_QUERY_VALUE, TempKey) = ERROR_SUCCESS;
if Result then
begin
FAccess := KEY_QUERY_VALUE;
if (CurrentKey <> 0) and Relative then S := CurrentPath + '\' + S;
ChangeKey(TempKey, S);
end
end;
end;
end;
function TRegistry.DeleteKey(const Key: string): Boolean;
var
Len: DWORD;
I: Integer;
Relative: Boolean;
S, KeyName: string;
OldKey, aDeleteKey: HKEY;
Info: TRegKeyInfo;
begin
S := Key;
Relative := IsRelative(S);
if not Relative then Delete(S, 1, 1);
OldKey := CurrentKey;
aDeleteKey := GetKey(Key);
if aDeleteKey <> 0 then
try
SetCurrentKey(aDeleteKey);
if GetKeyInfo(Info) then
begin
SetString(KeyName, nil, Info.MaxSubKeyLen + 1);
for I := Info.NumSubKeys - 1 downto 0 do
begin
Len := Info.MaxSubKeyLen + 1;
if RegEnumKeyEx(aDeleteKey, DWORD(I), PChar(KeyName), Len, nil, nil, nil,
nil) = ERROR_SUCCESS then
Self.DeleteKey(PChar(KeyName));
end;
end;
finally
SetCurrentKey(OldKey);
RegCloseKey(aDeleteKey);
end;
Result := RegDeleteKey(GetBaseKey(Relative), PChar(S)) = ERROR_SUCCESS;
end;
function TRegistry.DeleteValue(const Name: string): Boolean;
begin
Result := RegDeleteValue(CurrentKey, PChar(Name)) = ERROR_SUCCESS;
end;
function TRegistry.GetKeyInfo(var Value: TRegKeyInfo): Boolean;
begin
FillChar(Value, SizeOf(TRegKeyInfo), 0);
Result := RegQueryInfoKey(CurrentKey, nil, nil, nil, @Value.NumSubKeys,
@Value.MaxSubKeyLen, nil, @Value.NumValues, @Value.MaxValueLen,
@Value.MaxDataLen, nil, @Value.FileTime) = ERROR_SUCCESS;
if SysLocale.FarEast and (WinVer >= WvNT) then
with Value do
begin
Inc(MaxSubKeyLen, MaxSubKeyLen);
Inc(MaxValueLen, MaxValueLen);
end;
end;
procedure TRegistry.GetKeyNames(Strings: PStrList);
var
Len: DWORD;
I: Integer;
Info: TRegKeyInfo;
S: string;
begin
Strings.Clear;
if GetKeyInfo(Info) then
begin
SetString(S, nil, Info.MaxSubKeyLen + 1);
for I := 0 to Info.NumSubKeys - 1 do
begin
Len := Info.MaxSubKeyLen + 1;
RegEnumKeyEx(CurrentKey, I, PChar(S), Len, nil, nil, nil, nil);
Strings.Add(PChar(S));
end;
end;
end;
procedure TRegistry.GetValueNames(Strings: PStrList);
var
Len: DWORD;
I: Integer;
Info: TRegKeyInfo;
S: string;
begin
Strings.Clear;
if GetKeyInfo(Info) then
begin
SetString(S, nil, Info.MaxValueLen + 1);
for I := 0 to Info.NumValues - 1 do
begin
Len := Info.MaxValueLen + 1;
RegEnumValue(CurrentKey, I, PChar(S), Len, nil, nil, nil, nil);
Strings.Add(PChar(S));
end;
end;
end;
function TRegistry.GetDataInfo(const ValueName: string; var Value: TRegDataInfo): Boolean;
var
DataType: Integer;
begin
FillChar(Value, SizeOf(TRegDataInfo), 0);
Result := RegQueryValueEx(CurrentKey, PChar(ValueName), nil, @DataType, nil,
@Value.DataSize) = ERROR_SUCCESS;
Value.RegData := DataTypeToRegData(DataType);
end;
function TRegistry.GetDataSize(const ValueName: string): Integer;
var
Info: TRegDataInfo;
begin
if GetDataInfo(ValueName, Info) then
Result := Info.DataSize else
Result := -1;
end;
function TRegistry.GetDataType(const ValueName: string): TRegDataType;
var
Info: TRegDataInfo;
begin
if GetDataInfo(ValueName, Info) then
Result := Info.RegData else
Result := rdUnknown;
end;
procedure TRegistry.WriteString(const Name, Value: string);
begin
PutData(Name, PChar(Value), Length(Value)+1, rdString);
end;
procedure TRegistry.WriteExpandString(const Name, Value: string);
begin
PutData(Name, PChar(Value), Length(Value)+1, rdExpandString);
end;
function TRegistry.ReadString(const Name: string): string;
var
Len: Integer;
RegData: TRegDataType;
begin
Len := GetDataSize(Name);
if Len > 0 then
begin
SetString(Result, nil, Len);
GetData(Name, PChar(Result), Len, RegData);
if (RegData = rdString) or (RegData = rdExpandString) then
SetLength(Result, StrLen(PChar(Result)))
else ReadError(Name);
end
else Result := '';
end;
procedure TRegistry.WriteInteger(const Name: string; Value: Integer);
begin
PutData(Name, @Value, SizeOf(Integer), rdInteger);
end;
function TRegistry.ReadInteger(const Name: string): Integer;
var
RegData: TRegDataType;
begin
GetData(Name, @Result, SizeOf(Integer), RegData);
if RegData <> rdInteger then ReadError(Name);
end;
procedure TRegistry.WriteBool(const Name: string; Value: Boolean);
begin
WriteInteger(Name, Ord(Value));
end;
function TRegistry.ReadBool(const Name: string): Boolean;
begin
Result := ReadInteger(Name) <> 0;
end;
procedure TRegistry.WriteFloat(const Name: string; Value: Double);
begin
PutData(Name, @Value, SizeOf(Double), rdBinary);
end;
function TRegistry.ReadFloat(const Name: string): Double;
var
Len: Integer;
RegData: TRegDataType;
begin
Len := GetData(Name, @Result, SizeOf(Double), RegData);
if (RegData <> rdBinary) or (Len <> SizeOf(Double)) then
ReadError(Name);
end;
procedure TRegistry.WriteCurrency(const Name: string; Value: Currency);
begin
PutData(Name, @Value, SizeOf(Currency), rdBinary);
end;
function TRegistry.ReadCurrency(const Name: string): Currency;
var
Len: Integer;
RegData: TRegDataType;
begin
Len := GetData(Name, @Result, SizeOf(Currency), RegData);
if (RegData <> rdBinary) or (Len <> SizeOf(Currency)) then
ReadError(Name);
end;
procedure TRegistry.WriteDateTime(const Name: string; Value: TDateTime);
begin
PutData(Name, @Value, SizeOf(TDateTime), rdBinary);
end;
function TRegistry.ReadDateTime(const Name: string): TDateTime;
var
Len: Integer;
RegData: TRegDataType;
begin
Len := GetData(Name, @Result, SizeOf(TDateTime), RegData);
if (RegData <> rdBinary) or (Len <> SizeOf(TDateTime)) then
ReadError(Name);
end;
procedure TRegistry.WriteDate(const Name: string; Value: TDateTime);
begin
WriteDateTime(Name, Value);
end;
function TRegistry.ReadDate(const Name: string): TDateTime;
begin
Result := ReadDateTime(Name);
end;
procedure TRegistry.WriteTime(const Name: string; Value: TDateTime);
begin
WriteDateTime(Name, Value);
end;
function TRegistry.ReadTime(const Name: string): TDateTime;
begin
Result := ReadDateTime(Name);
end;
procedure TRegistry.WriteBinaryData(const Name: string; var Buffer; BufSize: Integer);
begin
PutData(Name, @Buffer, BufSize, rdBinary);
end;
function TRegistry.ReadBinaryData(const Name: string; var Buffer; BufSize: Integer): Integer;
var
RegData: TRegDataType;
Info: TRegDataInfo;
begin
if GetDataInfo(Name, Info) then
begin
Result := Info.DataSize;
RegData := Info.RegData;
if ((RegData = rdBinary) or (RegData = rdUnknown)) and (Result <= BufSize) then
GetData(Name, @Buffer, Result, RegData)
else ReadError(Name);
end else
Result := 0;
end;
procedure TRegistry.PutData(const Name: string; Buffer: Pointer;
BufSize: Integer; RegData: TRegDataType);
var
DataType: Integer;
begin
DataType := RegDataToDataType(RegData);
if RegSetValueEx(CurrentKey, PChar(Name), 0, DataType, Buffer,
BufSize) <> ERROR_SUCCESS then
{$IFDEF USE_ERR}
raise Exception.CreateFmt(e_Custom,SRegSetDataFailed, [Name]);
{$ELSE}
MsgOk(Format(SRegSetDataFailed, [Name]));
{$ENDIF}
end;
function TRegistry.GetData(const Name: string; Buffer: Pointer;
BufSize: Integer; var RegData: TRegDataType): Integer;
var
DataType: Integer;
begin
DataType := REG_NONE;
if RegQueryValueEx(CurrentKey, PChar(Name), nil, @DataType, PByte(Buffer),
@BufSize) <> ERROR_SUCCESS then
{$IFDEF USE_ERR}
raise Exception.CreateFmt(e_Custom,SRegGetDataFailed, [Name]);
{$ELSE}
begin
Result:=0;
MsgOk(Format(SRegGetDataFailed, [Name]));
end else
{$ENDIF}
Result := BufSize;
RegData := DataTypeToRegData(DataType);
end;
function TRegistry.HasSubKeys: Boolean;
var
Info: TRegKeyInfo;
begin
Result := GetKeyInfo(Info) and (Info.NumSubKeys > 0);
end;
function TRegistry.ValueExists(const Name: string): Boolean;
var
Info: TRegDataInfo;
begin
Result := GetDataInfo(Name, Info);
end;
function TRegistry.GetKey(const Key: string): HKEY;
var
S: string;
Relative: Boolean;
begin
S := Key;
Relative := IsRelative(S);
if not Relative then Delete(S, 1, 1);
Result := 0;
RegOpenKeyEx(GetBaseKey(Relative), PChar(S), 0, FAccess, Result);
end;
function TRegistry.RegistryConnect(const UNCName: string): Boolean;
var
TempKey: HKEY;
begin
Result := RegConnectRegistry(PChar(UNCname), RootKey, TempKey) = ERROR_SUCCESS;
if Result then
begin
RootKey := TempKey;
FCloseRootKey := True;
end;
end;
function TRegistry.LoadKey(const Key, FileName: string): Boolean;
var
S: string;
begin
S := Key;
if not IsRelative(S) then Delete(S, 1, 1);
Result := RegLoadKey(RootKey, PChar(S), PChar(FileName)) = ERROR_SUCCESS;
end;
function TRegistry.UnLoadKey(const Key: string): Boolean;
var
S: string;
begin
S := Key;
if not IsRelative(S) then Delete(S, 1, 1);
Result := RegUnLoadKey(RootKey, PChar(S)) = ERROR_SUCCESS;
end;
function TRegistry.RestoreKey(const Key, FileName: string): Boolean;
var
aRestoreKey: HKEY;
begin
Result := False;
aRestoreKey := GetKey(Key);
if aRestoreKey <> 0 then
try
Result := RegRestoreKey(aRestoreKey, PChar(FileName), 0) = ERROR_SUCCESS;
finally
RegCloseKey(aRestoreKey);
end;
end;
function TRegistry.ReplaceKey(const Key, FileName, BackUpFileName: string): Boolean;
var
S: string;
Relative: Boolean;
begin
S := Key;
Relative := IsRelative(S);
if not Relative then Delete(S, 1, 1);
Result := RegReplaceKey(GetBaseKey(Relative), PChar(S),
PChar(FileName), PChar(BackUpFileName)) = ERROR_SUCCESS;
end;
function TRegistry.SaveKey(const Key, FileName: string): Boolean;
var
aSaveKey: HKEY;
begin
Result := False;
aSaveKey := GetKey(Key);
if aSaveKey <> 0 then
try
Result := RegSaveKey(aSaveKey, PChar(FileName), nil) = ERROR_SUCCESS;
finally
RegCloseKey(aSaveKey);
end;
end;
function TRegistry.KeyExists(const Key: string): Boolean;
var
TempKey: HKEY;
OldAccess: Longword;
begin
OldAccess := FAccess;
try
FAccess := STANDARD_RIGHTS_READ or KEY_QUERY_VALUE or KEY_ENUMERATE_SUB_KEYS;
TempKey := GetKey(Key);
if TempKey <> 0 then RegCloseKey(TempKey);
Result := TempKey <> 0;
finally
FAccess := OldAccess;
end;
end;
procedure TRegistry.RenameValue(const OldName, NewName: string);
var
Len: Integer;
RegData: TRegDataType;
Buffer: PChar;
begin
if ValueExists(OldName) and not ValueExists(NewName) then
begin
Len := GetDataSize(OldName);
if Len > 0 then
begin
Buffer := AllocMem(Len);
try
Len := GetData(OldName, Buffer, Len, RegData);
DeleteValue(OldName);
PutData(NewName, Buffer, Len, RegData);
finally
FreeMem(Buffer);
end;
end;
end;
end;
procedure TRegistry.MoveKey(const OldName, NewName: string; Delete: Boolean);
var
SrcKey, DestKey: HKEY;
procedure MoveValue(SrcKey, DestKey: HKEY; const Name: string);
var
Len: Integer;
OldKey, PrevKey: HKEY;
Buffer: PChar;
RegData: TRegDataType;
begin
OldKey := CurrentKey;
SetCurrentKey(SrcKey);
try
Len := GetDataSize(Name);
if Len > 0 then
begin
Buffer := AllocMem(Len);
try
Len := GetData(Name, Buffer, Len, RegData);
PrevKey := CurrentKey;
SetCurrentKey(DestKey);
try
PutData(Name, Buffer, Len, RegData);
finally
SetCurrentKey(PrevKey);
end;
finally
FreeMem(Buffer);
end;
end;
finally
SetCurrentKey(OldKey);
end;
end;
procedure CopyValues(SrcKey, DestKey: HKEY);
var
Len: DWORD;
I: Integer;
KeyInfo: TRegKeyInfo;
S: string;
OldKey: HKEY;
begin
OldKey := CurrentKey;
SetCurrentKey(SrcKey);
try
if GetKeyInfo(KeyInfo) then
begin
MoveValue(SrcKey, DestKey, '');
SetString(S, nil, KeyInfo.MaxValueLen + 1);
for I := 0 to KeyInfo.NumValues - 1 do
begin
Len := KeyInfo.MaxValueLen + 1;
if RegEnumValue(SrcKey, I, PChar(S), Len, nil, nil, nil, nil) = ERROR_SUCCESS then
MoveValue(SrcKey, DestKey, PChar(S));
end;
end;
finally
SetCurrentKey(OldKey);
end;
end;
procedure CopyKeys(SrcKey, DestKey: HKEY);
var
Len: DWORD;
I: Integer;
Info: TRegKeyInfo;
S: string;
OldKey, PrevKey, NewSrc, NewDest: HKEY;
begin
OldKey := CurrentKey;
SetCurrentKey(SrcKey);
try
if GetKeyInfo(Info) then
begin
SetString(S, nil, Info.MaxSubKeyLen + 1);
for I := 0 to Info.NumSubKeys - 1 do
begin
Len := Info.MaxSubKeyLen + 1;
if RegEnumKeyEx(SrcKey, I, PChar(S), Len, nil, nil, nil, nil) = ERROR_SUCCESS then
begin
NewSrc := GetKey(PChar(S));
if NewSrc <> 0 then
try
PrevKey := CurrentKey;
SetCurrentKey(DestKey);
try
CreateKey(PChar(S));
NewDest := GetKey(PChar(S));
try
CopyValues(NewSrc, NewDest);
CopyKeys(NewSrc, NewDest);
finally
RegCloseKey(NewDest);
end;
finally
SetCurrentKey(PrevKey);
end;
finally
RegCloseKey(NewSrc);
end;
end;
end;
end;
finally
SetCurrentKey(OldKey);
end;
end;
begin
if KeyExists(OldName) and not KeyExists(NewName) then
begin
SrcKey := GetKey(OldName);
if SrcKey <> 0 then
try
CreateKey(NewName);
DestKey := GetKey(NewName);
if DestKey <> 0 then
try
CopyValues(SrcKey, DestKey);
CopyKeys(SrcKey, DestKey);
if Delete then DeleteKey(OldName);
finally
RegCloseKey(DestKey);
end;
finally
RegCloseKey(SrcKey);
end;
end;
end;
function Str2IntDef(const S: string; Default: Integer): Integer;
var
E: Integer;
begin
Val(S, Result, E);
if E <> 0 then Result := Default;
end;
function LCID2CodePage(ALcid: LCID): Integer;
var
Buffer: array [0..6] of Char;
begin
GetLocaleInfo(ALcid, LOCALE_IDEFAULTANSICODEPAGE, Buffer, SizeOf(Buffer));
Result:= Str2IntDef(Buffer, GetACP);
end;
procedure InitSysLocale;
var
DefaultLCID: LCID;
DefaultLangID: LANGID;
AnsiCPInfo: TCPInfo;
I: Integer;
BufferA: array [128..255] of Char;
BufferW: array [128..256] of Word;
PCharA: PChar;
procedure InitLeadBytes;
var
I: Integer;
J: Byte;
begin
GetCPInfo(LCID2CodePage(SysLocale.DefaultLCID), AnsiCPInfo);
with AnsiCPInfo do
begin
I := 0;
while (I < MAX_LEADBYTES) and ((LeadByte[I] or LeadByte[I + 1]) <> 0) do
begin
for J := LeadByte[I] to LeadByte[I + 1] do
Include(LeadBytes, Char(J));
Inc(I, 2);
end;
end;
end;
function IsWesternGroup: Boolean;
type
TLangGroup = $00..$1D;
TLangGroups = set of TLangGroup;
const
lgNeutral = TLangGroup($00);
lgDanish = TLangGroup($06);
lgDutch = TLangGroup($13);
lgEnglish = TLangGroup($09);
lgFinnish = TLangGroup($0B);
lgFrench = TLangGroup($0C);
lgGerman = TLangGroup($07);
lgItalian = TLangGroup($10);
lgNorwegian = TLangGroup($14);
lgPortuguese = TLangGroup($16);
lgSpanish = TLangGroup($0A);
lgSwedish = TLangGroup($1D);
WesternGroups: TLangGroups = [
lgNeutral,
lgDanish,
lgDutch,
lgEnglish,
lgFinnish,
lgFrench,
lgGerman,
lgItalian,
lgNorwegian,
lgPortuguese,
lgSpanish,
lgSwedish
];
begin
Result := SysLocale.PriLangID in WesternGroups;
end;
begin
{ Set default to English (US). }
SysLocale.DefaultLCID := $0409;
SysLocale.PriLangID := LANG_ENGLISH;
SysLocale.SubLangID := SUBLANG_ENGLISH_US;
DefaultLCID := GetThreadLocale;
if DefaultLCID <> 0 then SysLocale.DefaultLCID := DefaultLCID;
DefaultLangID := Word(DefaultLCID);
if DefaultLangID <> 0 then
begin
SysLocale.PriLangID := DefaultLangID and $3ff;
SysLocale.SubLangID := DefaultLangID shr 10;
end;
LeadBytes := [];
if WinVer >= wvNT then
begin
if IsWesternGroup then
begin
SysLocale.MiddleEast := False;
SysLocale.FarEast := False;
end
else
begin
{ Far East (aka MBCS)? - }
InitLeadBytes;
SysLocale.FarEast := LeadBytes <> [];
if SysLocale.FarEast then
begin
SysLocale.MiddleEast := False;
Exit;
end;
{ Middle East? }
for I := Low(BufferA) to High(BufferA) do
BufferA[I] := Char(I);
PCharA := @BufferA; { not null terminated: include length in GetStringTypeExA call }
GetStringTypeEx(SysLocale.DefaultLCID, CT_CTYPE2, PCharA, High(BufferA) - Low(BufferA) + 1, BufferW);
for I := Low(BufferA) to High(BufferA) do
begin