-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoftReg.pas
More file actions
1015 lines (944 loc) · 29.5 KB
/
SoftReg.pas
File metadata and controls
1015 lines (944 loc) · 29.5 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
unit SoftReg; {PB}
{$B-}
interface
uses
Classes, ComCtrls, Controls, ExtCtrls, Forms, StdCtrls, SysUtils, Windows, BccRegistryControls;
const
SViewKey = 'View';
type
ERegistryError = class(EWin32Error);
TRootKey = (rkNone, rkLocal, rkUser);
TKeyInfo = record
NumSubKeys: Integer;
MaxSubKeyLen: Integer;
NumValues: Integer;
MaxValueLen: Integer;
MaxDataLen: Integer;
FileTime: TFileTime;
end;
TOpenRootKeyEvent = procedure(Sender: TObject; ARootKey: TRootKey;
var Key: HKEY; var DoDefault: Boolean) of object;
TSoftReg = class
private
fCompanyName: string;
fOnOpenRootKey: TOpenRootKeyEvent;
fProductName: string;
fVersion: string;
fRootKey: TRootKey;
fpValue: pByte;
fValueSize: DWORD;
fValueType: DWORD;
procedure SetCompanyName(Value: string);
procedure SetProductName(Value: string);
procedure SetVersion(Value: string);
procedure CheckKey(var Key: HKEY);
function CreateKey(BaseKey: HKEY; Name: string): HKEY;
procedure CreateValues(SourceKey : HKEY; Subkey : string; DestinationKey : HKEY);
function TempKeyName : string;
protected
fHKeyRoot: HKEY;
function DoOpenRootKey(ARootKey: TRootKey): HKEY; virtual;
public
constructor Create; virtual;
destructor Destroy; override;
procedure CloseKey(Key: HKEY);
procedure CloseRootKey;
function ComponentValueName(Component: TComponent): string;
function DeleteKey(Key: HKEY; SubKey: string): Boolean;
function DeleteValue(Key: HKEY; Name: string): Boolean;
procedure GetKeyNames(Key: HKEY; Strings: TStrings);
procedure GetValueNames(Key: HKEY; Strings: TStrings);
procedure ClearKey(Key: HKEY);
procedure SaveKey(Key: HKEY; Filename: string);
procedure RestoreKey(Key: HKEY; Filename: string);
procedure CopyKey(SourceKey, DestinationKey: HKEY);
function KeyInfo(Key: HKEY): TKeyInfo;
function OpenKey(BaseKey: HKEY; Name: string; CanCreate: Boolean): HKEY;
procedure OpenRootKey(ARootKey: TRootKey); virtual;
function ReadBinary(Key: HKEY; Name: string; var Value; Size: DWORD): Boolean;
function ReadBinaryEx(Key: HKEY; Name: string; var Value; Size: DWORD; Initialize: Boolean): Boolean;
procedure WriteBinary(Key: HKEY; Name: string; const Value; Size: DWORD);
function ReadComponent(Key: HKEY; Name: string; Component: TComponent; Clear: Boolean): Boolean;
procedure WriteComponent(Key: HKEY; Name: string; Component: TComponent);
function ReadHandle(Key: HKEY; Name: string): THandle;
procedure WriteHandle(Key: HKEY; Name: string; Value: THandle);
function ReadInteger(Key: HKEY; Name: string; Default: Integer): Integer;
procedure WriteInteger(Key: HKEY; Name: string; Value: Integer);
function ReadBoolean(Key: HKEY; Name: string; Default: Boolean): Boolean;
procedure WriteBoolean(Key: HKEY; Name: string; Value: Boolean);
function ReadPath(Key: HKEY; Name: string; Default: string; CanCreate: Boolean): string;
procedure WritePath(Key: HKEY; Name: string; Value: string);
function ReadString(Key: HKEY; Name: string; Default: string): string;
procedure WriteString(Key: HKEY; Name: string; Value: string);
function ReadStrings(Key: HKEY; Name: string; Strings: TStrings): Boolean;
procedure WriteStrings(Key: HKEY; Name: string; Strings: TStrings);
function ReadWindowText(Key: HKEY; Control: TWinControl): string;
procedure WriteWindowText(Key: HKEY; Control: TWinControl);
function ReadForm(Key: HKEY; Form: TForm): Boolean; overload;
function ReadFormEx(Key: HKEY; Form: TForm; Maximized, NoMaximize: Boolean): Boolean;
function ReadForm(Key: HKEY; Form: TForm; Options: TRegWindowOptions): Boolean; overload; {CR 9829 - PB}
procedure WriteForm(Key: HKEY; Form: TForm);
function ReadListViewWidths(Key: HKEY; Wnd: HWND; ValueName: string): Boolean; overload; {CR 9706 - PB}
function ReadListViewWidths(Key: HKEY; ListView: TListView; ValueName: string): Boolean; overload; {CR 9631 - PB}
function ReadListViewWidths(Key: HKEY; ListView: TListView): Boolean; overload;
procedure WriteListViewWidths(Key: HKEY; Wnd: HWND; ValueName: string); overload; {CR 9706 - PB}
procedure WriteListViewWidths(Key: HKEY; ListView: TListView; ValueName: string); overload; {CR 9631 - PB}
procedure WriteListViewWidths(Key: HKEY; ListView: TListView); overload;
procedure WriteVariant(Key: HKEY; Name: string; Value: Variant);
function ReadVariant(Key: HKEY; Name: string; VariantType : Integer; Default: Variant): Variant;
property RootKey: TRootKey read fRootKey;
property OnOpenRootKey: TOpenRootKeyEvent read fOnOpenRootKey write fOnOpenRootKey;
published
property CompanyName: string read fCompanyName write SetCompanyName;
property ProductName: string read fProductName write SetProductName;
property Version: string read fVersion write SetVersion;
end;
implementation
uses
CommCtrl, Registry, BCCSoftwareDecl,
BccControlUtils, BccExceptions, BCCPersistObject, BccRegistryComCtrls,
BccSoftwareConst, BccUtils, Files, Variants;
const
REGSTR_MAX_VALUE_LENGTH = 256;
HEAP_GENERATE_EXCEPTIONS = 4;
HEAP_ZERO_MEMORY = 8;
SErrorSavingKey = 'Error saving registry key to the file, %s';
SErrorLoadingKey = 'Error loading registry key from the file, %s';
SErrorCopyingKey = 'Error copying registry key';
SCreateValuesError = 'Error creating registry values';
constructor TSoftReg.Create;
begin
fCompanyName:= SCompanyName;
fProductName:= BCCProductNameKey; // Application.Title;
end;
destructor TSoftReg.Destroy;
begin
CloseRootKey;
end;
procedure TSoftReg.CloseKey(Key: HKEY);
begin
if Key<>0 then
RegCloseKey(Key);
end;
procedure TSoftReg.CloseRootKey;
begin
if fHKeyRoot<>0 then
RegCloseKey(fHKeyRoot);
fHKeyRoot:= 0;
fRootKey:= rkNone;
end;
function TSoftReg.ComponentValueName(Component: TComponent): string;
function ComponentName(Component: TComponent; IsOwner: Boolean): string;
var
ClassName: string;
begin
if not Assigned(Component) then
Result:= ''
else
begin
ClassName:= Component.ClassName;
if Copy(ClassName, 1, 1)='T' then
ClassName:= Copy(ClassName, 2, Length(ClassName));
if IsOwner or (Component is TCustomForm) then
Result:= ClassName
else
begin
Result:= Component.Name;
if Result='' then
Result:= ClassName
else
if (Pos(ClassName, Result)=1) and (Length(ClassName)<Length(Result)) then
Result:= Copy(Result, Succ(Length(ClassName)), Length(Result));
end;
end;
end;
begin
Result:= ComponentName(Component, False);
if ComponentName(Component.Owner, True)<>'' then
Result:= ComponentName(Component.Owner, True)+'.'+Result;
end;
function TSoftReg.DeleteKey(Key: HKEY; SubKey: string): Boolean;
var
Index: Integer;
HSubKey: HKEY;
KeyNames: TStrings;
begin
Result:= False;
CheckKey(Key);
HSubKey:= OpenKey(Key, SubKey, False);
if HSubKey<>0 then
try
KeyNames:= TStringList.Create;
try
Result:= True;
GetKeyNames(HSubKey, KeyNames);
with KeyNames do
for Index:= 0 to Pred(Count) do
Result:= Result and DeleteKey(HSubKey, Strings[Index]);
finally
KeyNames.Free;
end;
finally
CloseKey(HSubKey);
end;
Result:= Result and (RegDeleteKey(Key, pChar(SubKey))=ERROR_SUCCESS);
end;
function TSoftReg.DeleteValue(Key: HKEY; Name: string): Boolean;
begin
CheckKey(Key);
Result:= RegDeleteValue(Key, pChar(Name))=ERROR_SUCCESS;
end;
procedure TSoftReg.GetKeyNames(Key: HKEY; Strings: TStrings);
var
Index: Integer;
Len: DWORD;
S: string;
begin
with Strings do
begin
BeginUpdate;
try
Clear;
CheckKey(Key);
with KeyInfo(Key) do
begin
SetString(S, nil, Succ(MaxSubKeyLen));
for Index:= 0 to Pred(NumSubKeys) do
begin
Len:= Succ(MaxSubKeyLen);
RegEnumKeyEx(Key, Index, pChar(S), Len, nil, nil, nil, nil);
Add(pChar(S));
end;
end;
finally
EndUpdate;
end;
end;
end;
procedure TSoftReg.GetValueNames(Key: HKEY; Strings: TStrings);
var
Index: Integer;
Len: DWORD;
S: string;
begin
with Strings do
begin
Clear;
BeginUpdate;
try
CheckKey(Key);
with KeyInfo(Key) do
begin
SetString(S, nil, Succ(MaxValueLen));
for Index:= 0 to Pred(NumValues) do
begin
Len:= Succ(MaxValueLen);
RegEnumValue(Key, Index, pChar(S), Len, nil, nil, nil, nil);
Add(pChar(S));
end;
end;
finally
EndUpdate;
end;
end;
end;
function TSoftReg.KeyInfo(Key: HKEY): TKeyInfo;
begin
FillChar(Result, SizeOf(Result), 0);
CheckKey(Key);
with Result do
RegQueryInfoKey(Key, nil, nil, nil, @NumSubKeys, @MaxSubKeyLen, nil, @NumValues, @MaxValueLen, @MaxDataLen, nil, @FileTime);
end;
function TSoftReg.OpenKey(BaseKey: HKEY; Name: string; CanCreate: Boolean): HKEY;
begin
Result:= 0;
if CanCreate then
Result:= CreateKey(BaseKey, Name)
else
begin
CheckKey(BaseKey);
SetLastError(RegOpenKey(BaseKey, pChar(Name), Result));
end;
end;
procedure TSoftReg.OpenRootKey(ARootKey: TRootKey);
begin
if fRootKey<>ARootKey then
begin
CloseRootKey;
fHKeyRoot:= DoOpenRootKey(ARootKey);
fRootKey:= ARootKey;
end;
end;
function TSoftReg.DoOpenRootKey(ARootKey: TRootKey): HKEY;
var
BaseKey: HKEY;
RootName: string;
DoDefault: Boolean;
begin
Result:= 0;
DoDefault:= True;
if Assigned(OnOpenRootKey) then
OnOpenRootKey(Self, ARootKey, Result, DoDefault);
if DoDefault then
begin
case ARootKey of
rkLocal: BaseKey:= HKEY_LOCAL_MACHINE;
rkUser: BaseKey:= HKEY_CURRENT_USER;
else
BaseKey:= 0;
end;
if BaseKey<>0 then
begin
RootName:= 'Software\'+CompanyName;
if ProductName<>'' then
RootName:= RootName+'\'+ProductName;
if Version<>'' then
RootName:= RootName+'\'+Version;
Result:= CreateKey(BaseKey, RootName);
end;
end;
end;
procedure TSoftReg.SetCompanyName(Value: string);
begin
if fCompanyName<>Value then
begin
CloseRootKey;
fCompanyName:= Value;
end;
end;
procedure TSoftReg.SetProductName(Value: string);
begin
if fProductName<>Value then
begin
CloseRootKey;
fProductName:= Value;
end;
end;
procedure TSoftReg.SetVersion(Value: string);
begin
if fVersion<>Value then
begin
CloseRootKey;
fVersion:= Value;
end;
end;
procedure TSoftReg.CheckKey(var Key: HKEY);
begin
if Key=0 then
Key:= fHKeyRoot;
end;
function TSoftReg.CreateKey(BaseKey: HKEY; Name: string): HKEY;
begin
Result:= 0;
CheckKey(BaseKey);
SetLastError(RegCreateKey(BaseKey, pChar(Name), Result));
end;
function TSoftReg.ReadBinary(Key: HKEY; Name: string; var Value;
Size: DWORD): Boolean;
begin
Result:= ReadBinaryEx(Key, Name, Value, Size, True);
end;
function TSoftReg.ReadBinaryEx(Key: HKEY; Name: string; var Value; Size: DWORD;
Initialize: Boolean): Boolean;
var
Handle: THandle;
begin
Result:= False;
if Initialize then
FillChar(Value, Size, 0);
Handle:= ReadHandle(Key, Name);
if Handle<>0 then
try
fpValue:= GlobalLock(Handle);
if Assigned(fpValue) then
try
if GlobalSize(Handle)>Size then
MoveMemory(@Value, fpValue, Size)
else
MoveMemory(@Value, fpValue, GlobalSize(Handle));
Result:= True;
finally
GlobalUnlock(Handle);
end;
finally
GlobalFree(Handle);
end;
end;
procedure TSoftReg.WriteBinary(Key: HKEY; Name: string; const Value;
Size: DWORD);
begin
CheckKey(Key);
RegSetValueEx(Key, pChar(Name), 0, REG_BINARY, @Value, Size);
end;
function TSoftReg.ReadComponent(Key: HKEY; Name: string;
Component: TComponent; Clear: Boolean): Boolean;
var
Stream: TMemoryStream;
begin
Result:= False;
CheckKey(Key);
fValueSize:= 0;
if (RegQueryValueEx(Key, pChar(Name), nil,
@fValueType, nil, @fValueSize)=ERROR_SUCCESS) and (fValueType=REG_BINARY) then
begin
Stream:= TMemoryStream.Create;
try
Stream.Size:= fValueSize;
Result:= RegQueryValueEx(Key, pChar(Name), nil, @fValueType, Stream.Memory, @fValueSize)=ERROR_SUCCESS;
if Result then
try
BCCReadComponent(Stream, Component, Clear);
except
on EStreamError do;
else
raise;
end;
finally
Stream.Free;
end;
end;
end;
procedure TSoftReg.WriteComponent(Key: HKEY; Name: string; Component: TComponent);
begin
with TMemoryStream.Create do
try
WriteComponent(Component);
WriteBinary(Key, Name, pChar(Memory)^, Size);
finally
Free;
end;
end;
function TSoftReg.ReadHandle(Key: HKEY; Name: string): THandle;
begin
Result:= 0;
CheckKey(Key);
if (RegQueryValueEx(Key, pChar(Name), nil,
@fValueType, nil, @fValueSize)=ERROR_SUCCESS) and (fValueType=REG_BINARY) then
begin
Result:= GlobalAlloc(GHND, fValueSize);
if Result<>0 then
try
fpValue:= GlobalLock(Result);
if Assigned(fpValue) then
try
RegQueryValueEx(Key, pChar(Name), nil, @fValueType, fpValue, @fValueSize);
finally
GlobalUnlock(Result);
end;
except
GlobalFree(Result);
raise;
end;
end;
end;
procedure TSoftReg.WriteHandle(Key: HKEY; Name: string; Value: THandle);
begin
fpValue:= GlobalLock(Value);
if Assigned(fpValue) then
try
WriteBinary(Key, Name, fpValue, GlobalSize(Value));
finally
GlobalUnlock(Value);
end;
end;
function TSoftReg.ReadInteger(Key: HKEY; Name: string; Default: Integer): Integer;
begin
CheckKey(Key);
fValueSize:= SizeOf(Result);
if not((RegQueryValueEx(Key, pChar(Name), nil, @fValueType, @Result, @fValueSize)=ERROR_SUCCESS) and (fValueType=REG_DWORD)) then
Result:= Default;
end;
procedure TSoftReg.WriteInteger(Key: HKEY; Name: string; Value: Integer);
begin
CheckKey(Key);
RegSetValueEx(Key, pChar(Name), 0, REG_DWORD, @Value, SizeOf(DWORD));
end;
function TSoftReg.ReadBoolean(Key: HKEY; Name: string; Default: Boolean): Boolean;
begin
Result:= Boolean(ReadInteger(Key, Name, Ord(Default)));
end;
procedure TSoftReg.WriteBoolean(Key: HKEY; Name: string; Value: Boolean);
begin
WriteInteger(Key, Name, Integer(Value));
end;
function TSoftReg.ReadPath(Key: HKEY; Name: string; Default: string;
CanCreate: Boolean): string;
begin
Result:= LongPathName(ReadString(Key, Name, Default));
if Result<>'' then
begin
Result:= IncludeTrailingBackslash(Result);
if CanCreate then
BccCreateDirectories(Result);
WritePath(Key, Name, Result);
end;
end;
procedure TSoftReg.WritePath(Key: HKEY; Name: string; Value: string);
begin
WriteString(Key, Name, Value);
end;
function TSoftReg.ReadString(Key: HKEY; Name: string; Default: string): string;
begin
Result:= Default;
CheckKey(Key);
fValueSize:= 0;
if (RegQueryValueEx(Key, pChar(Name), nil,
@fValueType, nil, @fValueSize)=ERROR_SUCCESS) and (fValueType=REG_SZ) then
begin
GetMem(fpValue, fValueSize);
try
if RegQueryValueEx(Key, pChar(Name), nil,
@fValueType, fpValue, @fValueSize)=ERROR_SUCCESS then
Result:= pChar(fpValue)
else
Result:= Default;
finally
FreeMem(fpValue);
end;
end;
end;
procedure TSoftReg.WriteString(Key: HKEY; Name: string; Value: string);
begin
CheckKey(Key);
RegSetValueEx(Key, pChar(Name), 0, REG_SZ, pChar(Value), Succ(Length(Value)));
end;
procedure TSoftReg.WriteVariant(Key: HKEY; Name: string; Value: Variant);
var
aDouble : Double;
aDate : TDateTime;
begin
case VarType(Value) of
varEmpty,
varNull : DeleteValue(Key, Name);
varSmallInt,
varInteger,
varByte : WriteInteger(Key, Name, Value);
varSingle,
varDouble,
varCurrency : begin
aDouble := Value;
WriteBinary(Key, Name, aDouble, SizeOf(aDouble));
end;
varDate : begin
aDate := Value;
WriteBinary(Key, Name, aDate, SizeOf(aDate));
end;
varOleStr,
varString : WriteString(Key, Name, Value);
varBoolean : WriteBoolean(Key, Name, Value);
end;
end;
function TSoftReg.ReadVariant(Key: HKEY; Name: string; VariantType : Integer; Default: Variant): Variant;
var
aDouble : Double;
aDate : TDateTime;
begin
try
Result := Default;
CheckKey(Key);
fValueSize := 0;
if (RegQueryValueEx(Key, pChar(Name), nil, @fValueType, nil, @fValueSize) = ERROR_SUCCESS) then
begin
case VariantType of
varString : if (fValueType = REG_SZ) then
Result := ReadString(Key, Name, Default)
else
DeleteValue(Key, Name);
varBoolean : if (fValueType = REG_DWORD) then
Result := ReadBoolean(Key, Name, Default)
else
DeleteValue(Key, Name);
varInteger : if (fValueType = REG_DWORD) then
Result := ReadInteger(Key, Name, Default)
else
DeleteValue(Key, Name);
varDouble : if (fValueType = REG_BINARY) then
if ReadBinary(Key, Name, aDouble, SizeOf(aDouble)) then
Result := aDouble
else
Result := Default
else
DeleteValue(Key, Name);
varDate : if (fValueType = REG_BINARY) then
if ReadBinary(Key, Name, aDate, SizeOf(aDate)) then
Result := aDate
else
Result := Default
else
DeleteValue(Key, Name);
end;
end;
except
Result := Default;
end;
end;
function TSoftReg.ReadStrings(Key: HKEY; Name: string; Strings: TStrings): Boolean;
var
P, pLine: pChar;
Text: string;
begin
Result:= False;
CheckKey(Key);
fValueSize:= 0;
if (RegQueryValueEx(Key, pChar(Name), nil,
@fValueType, nil, @fValueSize)=ERROR_SUCCESS) and (fValueType=REG_MULTI_SZ) then
begin
GetMem(fpValue, fValueSize);
try
if RegQueryValueEx(Key, pChar(Name), nil,
@fValueType, fpValue, @fValueSize)=ERROR_SUCCESS then
begin
Strings.Clear;
P:= pChar(fpValue);
while P^<>#0 do
begin
pLine:= P;
while p^<>#0 do Inc(P);
SetString(Text, pLine, P-pLine);
Strings.Add(Text);
Inc(P);
end;
Result:= True;
end;
finally
FreeMem(fpValue);
end;
end;
end;
procedure TSoftReg.WriteStrings(Key: HKEY; Name: string; Strings: TStrings);
var
Text: string;
Index: Integer;
begin
CheckKey(Key);
fValueSize:= 1;
for Index:= 0 to Pred(Strings.Count) do
begin
Text:= Text+Strings[Index]+#0;
Inc(fValueSize, Succ(Length(Strings[Index])));
end;
Text:= Text+#0;
RegSetValueEx(Key, pChar(Name), 0,
REG_MULTI_SZ, pChar(Text), fValueSize);
end;
function TSoftReg.ReadWindowText(Key: HKEY; Control: TWinControl): string;
var
i : integer;
IndexStrList : TStringList;
begin
if Control is TListBox then
begin
IndexStrList := TStringList.Create;
try
ReadStrings(Key, ComponentValueName(Control), IndexStrList);
with TListBox(Control) do
begin
for i:=0 to (IndexStrList.Count - 1) do
if (Items.IndexOf(IndexStrList[i]) <> -1) then
Selected[Items.IndexOf(IndexStrList[i])] := TRUE;
end;
finally
IndexStrList.Free;
end;
end
else
begin
Result:= ReadString(Key,
ComponentValueName(Control), WindowText(Control.Handle));
if Result <> '' then
begin
if Control is TComboBox then
begin
with TComboBox(Control) do
begin
if Style=csDropDownList then
begin
if Items.IndexOf(Result)>=0 then
ItemIndex:= Items.IndexOf(Result);
end
else
Text:= Result;
end;
end
else
SetWindowText(Control.Handle, pChar(Result));
end;
end;
end;
procedure TSoftReg.WriteWindowText(Key: HKEY; Control: TWinControl);
var
i : integer;
IndexStrList : TStringList;
begin
if Control is TListBox then
begin
with TListBox(Control) do
begin
IndexStrList := TStringList.Create;
try
for i:=0 to (Items.Count - 1) do
begin
if Selected[i] then
IndexStrList.Add(Items[i]);
end;
WriteStrings(Key, ComponentValueName(Control), IndexStrList{.Text});
finally
IndexStrList.Free;
end;
end;
end
else
WriteString(Key, ComponentValueName(Control), WindowText(Control.Handle));
end;
function TSoftReg.ReadForm(Key: HKEY; Form: TForm): Boolean;
begin
Result:= ReadFormEx(Key, Form, False, False);
end;
function TSoftReg.ReadFormEx(Key: HKEY; Form: TForm;
Maximized, NoMaximize: Boolean): Boolean; {CR 9829 - PB}
var
Options: TRegWindowOptions;
begin
Options:= [];
if Maximized then
Include(Options, rwoDefaultMaximize);
if NoMaximize then
Include(Options, rwoNoMaximize);
Result:= ReadForm(Key, Form, Options);
end;
function TSoftReg.ReadForm(Key: HKEY; Form: TForm; Options: TRegWindowOptions): Boolean;
begin
Result:= RegReadWindow(Key, ComponentValueName(Form), Form.Handle, Options);
end;
procedure TSoftReg.WriteForm(Key: HKEY; Form: TForm);
begin
RegWriteWindow(Key, ComponentValueName(Form), Form.Handle);
end;
function TSoftReg.ReadListViewWidths(Key: HKEY; Wnd: HWND;
ValueName: string): Boolean;
begin
Result:= RegReadListView(Key, ValueName, Wnd);
end;
function TSoftReg.ReadListViewWidths(Key: HKEY; ListView: TListView): Boolean;
begin
Result:= ReadListViewWidths(Key, ListView, ComponentValueName(ListView));
end;
function TSoftReg.ReadListViewWidths(Key: HKEY; ListView: TListView;
ValueName: string): Boolean;
begin
Result:= ReadListViewWidths(Key, ListView.Handle, ValueName);
end;
procedure TSoftReg.WriteListViewWidths(Key: HKEY; Wnd: HWND;
ValueName: string);
begin
RegWriteListView(Key, ValueName, Wnd);
end;
procedure TSoftReg.WriteListViewWidths(Key: HKEY; ListView: TListView;
ValueName: string); {CR 9631 - PB}
begin
WriteListViewWidths(Key, ListView.Handle, ValueName);
end;
procedure TSoftReg.WriteListViewWidths(Key: HKEY; ListView: TListView);
begin
WriteListViewWidths(Key, ListView, ComponentValueName(ListView));
end;
procedure TSoftReg.SaveKey(Key: HKEY; Filename: string); {spb}
var
Err : DWORD;
begin {converts long path name to short path name but requires short filename}
CheckKey(Key);
Err := RegSaveKey(Key, PChar(ShortPathName(Filename)), nil);
if (Err <> ERROR_SUCCESS) then
begin
SetLastError(Err);
BccRaiseLastWin32Error(ERegistryError, Format(SErrorSavingKey, [FileName]));
end;
end;
function TSoftReg.TempKeyName : string;
begin
Result := TempFilename('', '');
SysUtils.DeleteFile(Result);
Result := ExtractFilename(Result);
Result := ChangeFileExt(Result, '');
end;
procedure TSoftReg.ClearKey(Key: HKEY);
var
List : TStringList;
index : Integer;
begin
List := TStringList.Create;
try
GetKeyNames(Key, List);
for Index := 0 to Pred(List.Count) do
DeleteKey(Key, List[Index]);
List.Clear;
GetValueNames(Key, List);
for Index := 0 to Pred(List.Count) do
DeleteValue(Key, List[Index]);
finally
List.Free;
end;
end;
procedure TSoftReg.RestoreKey(Key: HKEY; Filename: string);
var
ErrLoad : DWORD;
TempKey : HKEY;
TempName : string;
BackupName : string;
BackupKey : HKEY;
begin {converts long path name to short path name but requires short filename}
TempName := TempKeyName;
ErrLoad := RegLoadKey(HKEY_USERS, PChar(TempName), PChar(ShortPathName(Filename))); {load saved hive}
if (ErrLoad <> ERROR_SUCCESS) then
begin
SetLastError(ErrLoad);
BccRaiseLastWin32Error(ERegistryError, Format(SErrorLoadingKey, [FileName]));
end;
try
TempKey := OpenKey(HKEY_USERS, PChar(TempName), false); {open saved hive}
if (TempKey <> 0) then
try
CheckKey(Key);
BackupName := TempKeyName;
BackupKey := OpenKey(HKEY_USERS, PChar(BackupName), true); {create backup key}
try
CopyKey(Key, BackupKey); {backup current key}
try
ClearKey(Key); {clear current key}
CopyKey(TempKey, Key); {copy saved hive to current key}
except
ClearKey(Key); {clear current key}
CopyKey(BackupKey, Key); {Restore backedup hive}
raise;
end;
finally
CloseKey(BackupKey);
BackupKey := OpenKey(HKEY_USERS, '', false);
if (BackupKey <> 0) then
try
DeleteKey(BackupKey, BackupName); {delete backup hive}
finally
CloseKey(Backupkey);
end;
end;
finally
CloseKey(TempKey);
end;
finally
RegUnloadKey(HKEY_USERS, PChar(TempName)); {unload saved hive}
end;
end;
procedure TSoftReg.CopyKey(SourceKey, DestinationKey: HKEY);
var
dwSubKeyLength : DWORD;
dwKeyIndex : DWORD;
szSubKey : PChar;
Err : DWORD;
hNewKey : HKEY;
hRtnKey : HKEY;
begin
dwKeyIndex := 0;
szSubKey := StrAlloc(REGSTR_MAX_VALUE_LENGTH);
try
Err := ERROR_SUCCESS;
while true do
begin
dwSubKeyLength := REGSTR_MAX_VALUE_LENGTH;
Err := RegEnumKey(SourceKey, dwKeyIndex, szSubKey, dwSubKeyLength);
case Err of
ERROR_NO_MORE_ITEMS : begin
Err := ERROR_SUCCESS;
Break;
end;
ERROR_SUCCESS : begin
Err := RegCreateKey(DestinationKey, szSubKey, hNewKey);
case Err of
ERROR_SUCCESS : try
CreateValues(SourceKey, szSubKey, hNewKey);
Err := RegOpenKey(SourceKey, szSubKey, hRtnKey);
case Err of
ERROR_SUCCESS : try
CopyKey(hRtnKey, hNewKey);
finally
CloseHandle(hRtnKey);
end;
else Break;
end;
finally
CloseHandle(hNewKey);
end;
else Break;
end;
end;
else Break;
end;
Inc(dwKeyIndex);
end;
if (Err <> ERROR_SUCCESS) then
begin
SetLastError(Err);
BccRaiseLastWin32Error(ERegistryError, SErrorCopyingKey);
end;
finally
StrDispose(szSubKey);
end;
end;
procedure TSoftReg.CreateValues(SourceKey : HKEY; Subkey : string; DestinationKey : HKEY);
var
cbValue : DWORD;
dwType : DWORD;
cdwBuf : DWORD;
i : DWORD;
szValue : PChar;
TempKey : HKEY;
lRet : DWORD;
pBuf : PByte;
TempKeyInfo : TKeyInfo;
begin
lRet := RegOpenKey(SourceKey, PChar(Subkey), Tempkey);
if (lRet <> ERROR_SUCCESS) then
begin
SetLastError(lRet);
BccRaiseLastWin32Error(ERegistryError, SCreateValuesError);
end
else
try
TempKeyInfo := KeyInfo(TempKey);
if (TempKeyInfo.NumValues > 0) then
begin
szValue := StrAlloc(Succ(TempKeyInfo.MaxValueLen));
try
pBuf := HeapAlloc(GetProcessHeap, HEAP_GENERATE_EXCEPTIONS or HEAP_ZERO_MEMORY, Succ(TempKeyInfo.MaxDataLen));
try
for i := 0 to Pred(TempKeyinfo.NumValues) do
begin
cbValue := Succ(TempKeyInfo.MaxValueLen);
cdwBuf := Succ(TempKeyInfo.MaxDataLen);
lRet := RegEnumValue(Tempkey, i, szValue, cbValue, nil, @dwType, pBuf, @cdwBuf);
if (lRet <> ERROR_SUCCESS) then
begin
SetLastError(lRet);
BccRaiseLastWin32Error(ERegistryError, SCreateValuesError);
end
else
begin
lRet := RegSetValueEx(DestinationKey, szValue, 0, dwType, Pointer(pBuf), cdwBuf);
if (lRet <> ERROR_SUCCESS) then
begin
SetLastError(lRet);
BccRaiseLastWin32Error(ERegistryError, SCreateValuesError);
end;