-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathNtUtils.Registry.Offline.pas
1054 lines (865 loc) · 26.1 KB
/
NtUtils.Registry.Offline.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 NtUtils.Registry.Offline;
{
This module provides functions for working with offline registry hives.
}
interface
uses
Ntapi.WinNt, Ntapi.ntregapi, Ntapi.ntioapi, Ntapi.Versions,
NtUtils, DelphiApi.Reflection;
type
IORHandle = interface (IHandle)
function GetHive: IORHandle;
property Hive: IORHandle read GetHive;
end;
TORxSubKeyInfo = record
KeyName: String;
ClassName: String;
LastWriteTime: TLargeInteger;
end;
TORxKeyInfo = record
ClassName: String;
SubKeys: Cardinal;
MaxSubKeyLen: Cardinal;
MaxClassLen: Cardinal;
Values: Cardinal;
MaxValueNameLen: Cardinal;
[Bytes] MaxValueLen: Cardinal;
[Bytes] SecurityDescriptorSize: Cardinal;
LastWriteTime: TLargeInteger
end;
TORxValueInfo = record
ValueName: String;
ValueType: TRegValueType;
[MayReturnNil] Data: IMemory;
end;
{ Hives }
// Create an empty in-memory hive
[MinOSVersion(OsWin81)]
function ORxCreateHive(
out hxHive: IORHandle
): TNtxStatus;
// Parse a hive from a file by name
[MinOSVersion(OsWin81)]
function ORxOpenHiveByName(
out hxHive: IORHandle;
[Access(FILE_READ_DATA)] const FilePath: String
): TNtxStatus;
// Parse a hive from a file by handle
[MinOSVersion(OsWin81)]
function ORxOpenHiveByHandle(
out hxHive: IORHandle;
[Access(FILE_READ_DATA)] const hxFile: IHandle
): TNtxStatus;
// Merges keys from multiple hives into one in-memry hive
[MinOSVersion(OsWin1020H1)]
function ORxMergeHives(
out hxMergedHive: IORHandle;
const Hives: TArray<IORHandle>
): TNtxStatus;
// Save changes made to the hive into a file.
// Mapping of OS version to hive format version:
// 5.0 -> 1.3
// 5.1, 5.2, 6.0, 6.1, 6.2, 6.3, 10.0 -> 1.5
[MinOSVersion(OsWin81)]
function ORxSaveHive(
const hxHive: IORHandle;
[Access(FILE_WRITE_DATA)] const FilePath: String;
OsMajor: Cardinal = 6;
OsMinor: Cardinal = 1
): TNtxStatus;
{ Keys }
// Open an key under a hive or another key
[MinOSVersion(OsWin81)]
function ORxOpenKey(
out hxKey: IORHandle;
const hxParent: IORHandle;
const SubKeyName: String
): TNtxStatus;
// Create an key under a hive or another key
[MinOSVersion(OsWin81)]
function ORxCreateKey(
out hxKey: IORHandle;
const hxParent: IORHandle;
const SubKeyName: String;
Options: TRegOpenOptions = 0;
[opt] SecurityDescriptor: PSecurityDescriptor = nil;
[opt] const ClassName: String = '';
[out, opt] Disposition: PRegDisposition = nil
): TNtxStatus;
// Enumerate sub-keys under the specified hive or key one-by-one
[MinOSVersion(OsWin81)]
function ORxEnumerateKey(
const hxParent: IORHandle;
Index: Cardinal;
out Key: TORxSubKeyInfo;
[opt, NumberOfElements] InitialNameLength: Cardinal = 20;
[opt, NumberOfElements] InitialClassNameLength: Cardinal = 0
): TNtxStatus;
// Enumerate all sub-keys under the specified hive or key
[MinOSVersion(OsWin81)]
function ORxEnumerateKeys(
const hxParent: IORHandle;
out Keys: TArray<TORxSubKeyInfo>;
[opt, NumberOfElements] InitialNameLength: Cardinal = 20;
[opt, NumberOfElements] InitialClassNameLength: Cardinal = 0
): TNtxStatus;
// Make a for-in iterator for enumerating keys under the specified hive or key.
// Note: when the Status parameter is not set, the function might raise
// exceptions during enumeration.
[MinOSVersion(OsWin81)]
function ORxIterateKey(
[out, opt] Status: PNtxStatus;
const hxParent: IORHandle;
[opt, NumberOfElements] InitialNameLength: Cardinal = 20;
[opt, NumberOfElements] InitialClassNameLength: Cardinal = 0
): IEnumerable<TORxSubKeyInfo>;
// Retrieve various infomation about a hive or a key
[MinOSVersion(OsWin81)]
function ORxQueryKey(
const hxKey: IORHandle;
out Info: TORxKeyInfo;
[opt, NumberOfElements] InitialClassNameLength: Cardinal = 0
): TNtxStatus;
// Retrieve virtualization control flags on a key
[MinOSVersion(OsWin81)]
function ORxQueryVirtualFlagsKey(
const hxKey: IORHandle;
out Flags: TKeyControlFlags
): TNtxStatus;
// Set virtualization control flags on a key
[MinOSVersion(OsWin81)]
function ORxSetVirtualFlagsKey(
const hxKey: IORHandle;
Flags: TKeyControlFlags
): TNtxStatus;
// Retrieve the security descriptor of a key
[MinOSVersion(OsWin81)]
function ORxQuerySecurityKey(
const hxKey: IORHandle;
Info: TSecurityInformation;
out SD: ISecurityDescriptor
): TNtxStatus;
// Set the security descriptor on an key
[MinOSVersion(OsWin81)]
function ORxSetSecurityKey(
const hxKey: IORHandle;
Info: TSecurityInformation;
[in] SD: PSecurityDescriptor
): TNtxStatus;
// Delete the specified key
[MinOSVersion(OsWin81)]
function ORxDeleteKey(
const hxParent: IORHandle;
[opt] const KeyName: String = ''
): TNtxStatus;
// Rename the specified key
[MinOSVersion(OsWin10RS2)]
function ORxRenameKey(
const hxKey: IORHandle;
const KeyName: String
): TNtxStatus;
{ Values }
// Enumerate values under the specified hive or key one-by-one
[MinOSVersion(OsWin81)]
function ORxEnumerateValue(
const hxKey: IORHandle;
Index: Cardinal;
out Value: TORxValueInfo;
RetrieveData: Boolean = False;
[opt, NumberOfElements] InitialNameLength: Cardinal = 0;
[opt, NumberOfBytes] InitialDataSize: Cardinal = 0
): TNtxStatus;
// Enumerate all values under the specified hive or key
[MinOSVersion(OsWin81)]
function ORxEnumerateValues(
const hxKey: IORHandle;
out Values: TArray<TORxValueInfo>;
RetrieveData: Boolean = False;
[opt, NumberOfElements] InitialNameLength: Cardinal = 0;
[opt, NumberOfBytes] InitialDataSize: Cardinal = 0
): TNtxStatus;
// Make a for-in iterator for enumerating keys under the specified hive or key.
// Note: when the Status parameter is not set, the function might raise
// exceptions during enumeration.
[MinOSVersion(OsWin81)]
function ORxIterateValue(
[out, opt] Status: PNtxStatus;
const hxKey: IORHandle;
RetrieveData: Boolean = False;
[opt, NumberOfElements] InitialNameLength: Cardinal = 0;
[opt, NumberOfBytes] InitialDataSize: Cardinal = 0
): IEnumerable<TORxValueInfo>;
// Retrieve value information and/or data
[MinOSVersion(OsWin81)]
function ORxGetValue(
const hxKey: IORHandle;
[opt] const SubKeyName: String;
[opt] const ValueName: String;
out Info: TORxValueInfo;
RetrieveData: Boolean = True;
[opt, NumberOfBytes] InitialDataSize: Cardinal = 0
): TNtxStatus;
// Retrieve a 32-bit value
[MinOSVersion(OsWin81)]
function ORxGetValueUInt32(
const hxKey: IORHandle;
[opt] const SubKeyName: String;
[opt] const ValueName: String;
out Value: Cardinal
): TNtxStatus;
// Retrieve a 64-bit value
[MinOSVersion(OsWin81)]
function ORxGetValueUInt64(
const hxKey: IORHandle;
[opt] const SubKeyName: String;
[opt] const ValueName: String;
out Value: UInt64
): TNtxStatus;
// Retrieve a string value
[MinOSVersion(OsWin81)]
function ORxGetValueString(
const hxKey: IORHandle;
[opt] const SubKeyName: String;
[opt] const ValueName: String;
out Value: String;
[out, opt] ValueType: PRegValueType = nil
): TNtxStatus;
// Retrieve a multi-string value
[MinOSVersion(OsWin81)]
function ORxGetValueMultiString(
const hxKey: IORHandle;
[opt] const SubKeyName: String;
[opt] const ValueName: String;
out Value: TArray<String>
): TNtxStatus;
// Set a value of an arbitrary type
[MinOSVersion(OsWin81)]
function ORxSetValue(
const hxKey: IORHandle;
[opt] const ValueName: String;
[in] Data: Pointer;
[NumberOfBytes] DataSize: Cardinal;
ValueType: TRegValueType = REG_BINARY
): TNtxStatus;
// Set a value of a 32-bit integer type
[MinOSVersion(OsWin81)]
function ORxSetValueUInt32(
const hxKey: IORHandle;
[opt] const ValueName: String;
Value: Cardinal;
ValueType: TRegValueType = REG_DWORD
): TNtxStatus;
// Set a value of a 64-bit integer type
[MinOSVersion(OsWin81)]
function ORxSetValueUInt64(
const hxKey: IORHandle;
[opt] const ValueName: String;
const Value: UInt64
): TNtxStatus;
// Set a value of a string type
[MinOSVersion(OsWin81)]
function ORxSetValueString(
const hxKey: IORHandle;
[opt] const ValueName: String;
const Value: String;
ValueType: TRegValueType = REG_SZ
): TNtxStatus;
// Set a value of a multi-string type
[MinOSVersion(OsWin81)]
function ORxSetValueMultiString(
const hxKey: IORHandle;
[opt] const ValueName: String;
const Value: TArray<String>
): TNtxStatus;
// Delete an existing value
[MinOSVersion(OsWin81)]
function ORxDeleteValue(
const hxKey: IORHandle;
[opt] const ValueName: String
): TNtxStatus;
implementation
uses
Ntapi.WinError, Ntapi.ntstatus, Ntapi.offreg, DelphiUtils.AutoObjects,
NtUtils.Ldr, NtUtils.SysUtils;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ Hives }
type
TORAutoHiveHandle = class(TCustomAutoHandle, IORHandle, IAutoReleasable)
function GetHive: IORHandle;
procedure Release; override;
end;
function TORAutoHiveHandle.GetHive;
begin
// The parent hive for a hive handle is itself
Result := Self;
end;
procedure TORAutoHiveHandle.Release;
begin
if (FHandle <> 0) and LdrxCheckDelayedImport(delayed_ORCloseHive).IsSuccess then
ORCloseHive(FHandle);
FHandle := 0;
inherited;
end;
function ORxCreateHive;
var
hHive: TORHandle;
begin
Result := LdrxCheckDelayedImport(delayed_ORCreateHive);
if not Result.IsSuccess then
Exit;
Result.Location := 'ORCreateHive';
Result.Win32ErrorOrSuccess := ORCreateHive(hHive);
if Result.IsSuccess then
hxHive := TORAutoHiveHandle.Capture(hHive);
end;
function ORxOpenHiveByName;
var
hHive: TORHandle;
begin
Result := LdrxCheckDelayedImport(delayed_OROpenHive);
if not Result.IsSuccess then
Exit;
Result.Location := 'OROpenHive';
Result.LastCall.Expects<TIoFileAccessMask>(FILE_READ_DATA);
Result.Win32ErrorOrSuccess := OROpenHive(PWideChar(FilePath), hHive);
if Result.IsSuccess then
hxHive := TORAutoHiveHandle.Capture(hHive);
end;
function ORxOpenHiveByHandle;
var
hHive: TORHandle;
begin
Result := LdrxCheckDelayedImport(delayed_OROpenHiveByHandle);
if not Result.IsSuccess then
Exit;
Result.Location := 'OROpenHiveByHandle';
Result.LastCall.Expects<TIoFileAccessMask>(FILE_READ_DATA);
Result.Win32ErrorOrSuccess := OROpenHiveByHandle(HandleOrDefault(hxFile),
hHive);
if Result.IsSuccess then
hxHive := TORAutoHiveHandle.Capture(hHive);
end;
function ORxMergeHives;
var
HiveHandles: TArray<TORHandle>;
pHives: PORHandleArray;
hNewHive: TORHandle;
i: Integer;
begin
Result := LdrxCheckDelayedImport(delayed_ORMergeHives);
if not Result.IsSuccess then
Exit;
SetLength(HiveHandles, Length(Hives));
for i := 0 to High(Hives) do
HiveHandles[i] := Hives[i].Handle;
if Length(HiveHandles) > 0 then
pHives := Pointer(@HiveHandles[0])
else
pHives := nil;
Result.Location := 'ORMergeHives';
Result.Win32ErrorOrSuccess := ORMergeHives(pHives, Length(HiveHandles),
hNewHive);
if Result.IsSuccess then
hxMergedHive := TORAutoHiveHandle.Capture(hNewHive);
end;
function ORxSaveHive;
begin
Result := LdrxCheckDelayedImport(delayed_ORSaveHive);
if not Result.IsSuccess then
Exit;
Result.Location := 'ORSaveHive';
Result.LastCall.Expects<TIoFileAccessMask>(FILE_WRITE_DATA);
Result.Win32ErrorOrSuccess := ORSaveHive(HandleOrDefault(hxHive),
PWideChar(FilePath), OsMajor, OsMinor);
end;
{ Keys }
type
TORAutoKeyHandle = class(TCustomAutoHandle, IORHandle, IAutoReleasable)
FHive: IORHandle;
function GetHive: IORHandle;
procedure Release; override;
constructor Capture(hKey: THandle; const ParentHive: IORHandle);
end;
constructor TORAutoKeyHandle.Capture;
begin
// Prolong the lifetime of the parent hive
FHive := ParentHive;
inherited Capture(hKey);
end;
function TORAutoKeyHandle.GetHive;
begin
Result := FHive;
end;
procedure TORAutoKeyHandle.Release;
begin
if (FHandle <> 0) and LdrxCheckDelayedImport(delayed_ORCloseKey).IsSuccess then
ORCloseKey(FHandle);
FHandle := 0;
FHive := nil;
inherited;
end;
function ORxOpenKey;
var
hKey: TORHandle;
begin
Result := LdrxCheckDelayedImport(delayed_OROpenKey);
if not Result.IsSuccess then
Exit;
Result.Location := 'OROpenKey';
Result.Win32ErrorOrSuccess := OROpenKey(HandleOrDefault(hxParent),
RefStrOrNil(SubKeyName), hKey);
if Result.IsSuccess then
hxKey := TORAutoKeyHandle.Capture(hKey, hxParent.Hive);
end;
function ORxCreateKey;
var
hKey: TORHandle;
begin
Result := LdrxCheckDelayedImport(delayed_ORCreateKey);
if not Result.IsSuccess then
Exit;
Result.Location := 'ORCreateKey';
Result.Win32ErrorOrSuccess := ORCreateKey(HandleOrDefault(hxParent),
PWideChar(SubKeyName), RefStrOrNil(ClassName), Options, SecurityDescriptor,
hKey, Disposition);
if Result.IsSuccess then
hxKey := TORAutoKeyHandle.Capture(hKey, hxParent.Hive);
end;
function ORxEnumerateKey;
var
NameBuffer, ClassNameBuffer: IMemory;
NameRequired, ClassNameRequired: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_OREnumKey);
if not Result.IsSuccess then
Exit;
// Prepare initial buffers for the name and class name
NameBuffer := Auto.AllocateDynamic(Succ(InitialNameLength) *
SizeOf(WideChar));
ClassNameBuffer := Auto.AllocateDynamic(Succ(InitialClassNameLength) *
SizeOf(WideChar));
repeat
// Calculate available sizes in chars
NameRequired := NameBuffer.Size div SizeOf(WideChar);
ClassNameRequired := ClassNameBuffer.Size div SizeOf(WideChar);
Result.Location := 'OREnumKey';
Result.Win32ErrorOrSuccess := OREnumKey(HandleOrDefault(hxParent), Index,
NameBuffer.Data, NameRequired, ClassNameBuffer.Data, @ClassNameRequired,
@Key.LastWriteTime);
// Retry if at least one buffer requires expansion
{$BOOLEVAL ON}
until not NtxExpandBufferEx(Result, NameBuffer, NameRequired *
SizeOf(WideChar), nil) and not NtxExpandBufferEx(Result, ClassNameBuffer,
ClassNameRequired * SizeOf(WideChar), nil);
{$BOOLEVAL OFF}
// Make the function `while ORxEnumerateKey(...).Save(Result) do`-compatible
if Result.Win32Error = ERROR_NO_MORE_ITEMS then
Result.Status := STATUS_NO_MORE_ENTRIES;
if not Result.IsSuccess then
Exit;
// Capture the strings and advance the cursor
SetString(Key.KeyName, PWideChar(NameBuffer.Data), NameRequired);
SetString(Key.ClassName, PWideChar(ClassNameBuffer.Data), ClassNameRequired);
end;
function ORxEnumerateKeys;
var
Key: TORxSubKeyInfo;
Index: Cardinal;
begin
Keys := nil;
Index := 0;
while ORxEnumerateKey(hxParent, Index, Key, InitialNameLength,
InitialClassNameLength).HasEntry(Result) do
begin
SetLength(Keys, Succ(Length(Keys)));
Keys[High(Keys)] := Key;
Inc(Index);
end;
end;
function ORxIterateKey;
var
Index: Cardinal;
begin
Index := 0;
Result := NtxAuto.Iterate<TORxSubKeyInfo>(Status,
function (out Current: TORxSubKeyInfo): TNtxStatus
begin
// Retrieve the sub-key by index
Result := ORxEnumerateKey(hxParent, Index, Current, InitialNameLength,
InitialClassNameLength);
if not Result.IsSuccess then
Exit;
// Advance to the next
Inc(Index);
end
);
end;
function ORxQueryKey;
var
ClassNameBuffer: IMemory;
ClassNameRequired: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_ORQueryInfoKey);
if not Result.IsSuccess then
Exit;
ClassNameBuffer := Auto.AllocateDynamic(Succ(InitialClassNameLength) *
SizeOf(WideChar));
repeat
// Class name buffer size counts in characters
ClassNameRequired := ClassNameBuffer.Size div SizeOf(WideChar);
Result.Location := 'ORQueryInfoKey';
Result.Win32ErrorOrSuccess := ORQueryInfoKey(HandleOrDefault(hxKey),
ClassNameBuffer.Data, @ClassNameRequired, @Info.SubKeys,
@Info.MaxSubKeyLen, @Info.MaxClassLen, @Info.Values,
@Info.MaxValueNameLen, @Info.MaxValueLen, @Info.SecurityDescriptorSize,
@Info.LastWriteTime);
// Expand the buffer and retry if necessary
until not NtxExpandBufferEx(Result, ClassNameBuffer, ClassNameRequired *
SizeOf(WideChar), nil);
if Result.IsSuccess then
Exit;
SetString(Info.ClassName, PWideChar(ClassNameBuffer.Data), ClassNameRequired);
end;
function ORxQueryVirtualFlagsKey;
begin
Result := LdrxCheckDelayedImport(delayed_ORGetVirtualFlags);
if not Result.IsSuccess then
Exit;
Result.Location := 'ORGetVirtualFlags';
Result.Win32ErrorOrSuccess := ORGetVirtualFlags(HandleOrDefault(hxKey),
Flags);
end;
function ORxSetVirtualFlagsKey;
begin
Result := LdrxCheckDelayedImport(delayed_ORSetVirtualFlags);
if not Result.IsSuccess then
Exit;
Result.Location := 'ORSetVirtualFlags';
Result.Win32ErrorOrSuccess := ORSetVirtualFlags(HandleOrDefault(hxKey),
Flags);
end;
function ORxQuerySecurityKey;
const
INITIAL_SIZE = 256;
var
Buffer: IMemory absolute SD;
RequiredSize: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_ORGetKeySecurity);
if not Result.IsSuccess then
Exit;
Buffer := Auto.AllocateDynamic(INITIAL_SIZE);
repeat
RequiredSize := Buffer.Size;
Result.Location := 'ORGetKeySecurity';
Result.Win32ErrorOrSuccess := ORGetKeySecurity(HandleOrDefault(hxKey), Info,
Buffer.Data, RequiredSize);
// Expand the buffer and retry if necessary
until not NtxExpandBufferEx(Result, Buffer, RequiredSize, nil);
end;
function ORxSetSecurityKey;
begin
Result := LdrxCheckDelayedImport(delayed_ORSetKeySecurity);
if not Result.IsSuccess then
Exit;
Result.Location := 'ORSetKeySecurity';
Result.Win32ErrorOrSuccess := ORSetKeySecurity(HandleOrDefault(hxKey), Info,
SD);
end;
function ORxDeleteKey;
begin
Result := LdrxCheckDelayedImport(delayed_ORDeleteKey);
if not Result.IsSuccess then
Exit;
Result.Location := 'ORDeleteKey';
Result.Win32ErrorOrSuccess := ORDeleteKey(HandleOrDefault(hxParent),
RefStrOrNil(KeyName));
end;
function ORxRenameKey;
begin
Result := LdrxCheckDelayedImport(delayed_ORRenameKey);
if not Result.IsSuccess then
Exit;
Result.Location := 'ORRenameKey';
Result.Win32ErrorOrSuccess := ORRenameKey(HandleOrDefault(hxKey),
PWideChar(KeyName));
end;
{ Values }
function ORxEnumerateValue;
var
NameBuffer: IMemory;
NameRequired, DataRequired: Cardinal;
pDataRequired: PCardinal;
RetryDueToDataExpansion: Boolean;
begin
Result := LdrxCheckDelayedImport(delayed_OREnumValue);
if not Result.IsSuccess then
Exit;
// Prepare the initial data buffer
if RetrieveData then
Value.Data := Auto.AllocateDynamic(InitialDataSize)
else
Value.Data := nil;
// Prepare the initial name buffer
NameBuffer := Auto.AllocateDynamic(Succ(InitialNameLength) *
SizeOf(WideChar));
repeat
// The function counts characters, not bytes
NameRequired := NameBuffer.Size div SizeOf(WideChar);
// Data retrieval is optional and happens for non-nil buffers
if RetrieveData then
begin
DataRequired := Value.Data.Size;
pDataRequired := @DataRequired;
end
else
pDataRequired := nil;
Result.Location := 'OREnumValue';
Result.Win32ErrorOrSuccess := OREnumValue(HandleOrDefault(hxKey), Index,
NameBuffer.Data, NameRequired, Value.ValueType, Auto.RefOrNil(Value.Data),
pDataRequired);
// Check if we need more space for data
RetryDueToDataExpansion := RetrieveData and
NtxExpandBufferEx(Result, Value.Data, DataRequired, nil);
// Retry if at least one buffer requires expansion
until not NtxExpandBufferEx(Result, NameBuffer, NameRequired *
SizeOf(WideChar), nil) and not RetryDueToDataExpansion;
// Make the function `while ORxEnumerateValue(...).Save(Result) do`-compatible
if Result.Win32Error = ERROR_NO_MORE_ITEMS then
Result.Status := STATUS_NO_MORE_ENTRIES;
if not Result.IsSuccess then
Exit;
// Capture the string and advance the cursor
SetString(Value.ValueName, PWideChar(NameBuffer.Data), NameRequired);
end;
function ORxEnumerateValues;
var
Index: Cardinal;
Value: TORxValueInfo;
begin
Values := nil;
Index := 0;
while ORxEnumerateValue(hxKey, Index, Value, RetrieveData, InitialNameLength,
InitialDataSize).HasEntry(Result) do
begin
SetLength(Values, Succ(Length(Values)));
Values[High(Values)] := Value;
Inc(Index);
end;
end;
function ORxIterateValue;
var
Index: Cardinal;
begin
Index := 0;
Result := NtxAuto.Iterate<TORxValueInfo>(Status,
function (out Current: TORxValueInfo): TNtxStatus
begin
// Retrieve the value by index
Result := ORxEnumerateValue(hxKey, Index, Current, RetrieveData,
InitialNameLength, InitialDataSize);
if not Result.IsSuccess then
Exit;
// Advance to the next
Inc(Index);
end
);
end;
function ORxGetValue;
var
DataRequired: Cardinal;
pDataRequired: PCardinal;
begin
Result := LdrxCheckDelayedImport(delayed_ORGetValue);
if not Result.IsSuccess then
Exit;
// Prepare the initial data buffer
if RetrieveData then
Info.Data := Auto.AllocateDynamic(InitialDataSize)
else
Info.Data := nil;
Info.ValueName := ValueName;
repeat
// Data retrieval is optional and happens for non-nil buffers
if RetrieveData then
begin
DataRequired := Info.Data.Size;
pDataRequired := @DataRequired;
end
else
pDataRequired := nil;
Result.Location := 'ORGetValue';
Result.Win32ErrorOrSuccess := ORGetValue(HandleOrDefault(hxKey),
RefStrOrNil(SubKeyName), RefStrOrNil(ValueName), Info.ValueType,
Auto.RefOrNil(Info.Data), pDataRequired);
// The function can succeed even when the buffer is too small; fix it here
if RetrieveData and (DataRequired > Info.Data.Size) then
Result.Win32Error := ERROR_MORE_DATA;
// Expand the data buffer if required
until not RetrieveData or not NtxExpandBufferEx(Result, Info.Data,
DataRequired, nil);
end;
function ORxGetValueUInt32;
var
ValueType: TRegValueType;
RequiredSize: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_ORGetValue);
if not Result.IsSuccess then
Exit;
RequiredSize := SizeOf(Value);
Result.Location := 'ORGetValue';
Result.Win32ErrorOrSuccess := ORGetValue(HandleOrDefault(hxKey),
RefStrOrNil(SubKeyName), RefStrOrNil(ValueName), ValueType,
@Value, @RequiredSize);
if not Result.IsSuccess then
Exit;
if not (ValueType in [REG_DWORD, REG_DWORD_BIG_ENDIAN]) then
begin
Result.Location := 'ORxGetValueUInt32';
Result.Status := STATUS_OBJECT_TYPE_MISMATCH;
end
else if RequiredSize <> SizeOf(Value) then
begin
Result.Location := 'ORxGetValueUInt32';
Result.Status := STATUS_INVALID_BUFFER_SIZE;
end;
if ValueType = REG_DWORD_BIG_ENDIAN then
Value := RtlxSwapEndianness(Value);
end;
function ORxGetValueUInt64;
var
ValueType: TRegValueType;
RequiredSize: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_ORGetValue);
if not Result.IsSuccess then
Exit;
Value := 0;
RequiredSize := SizeOf(Value);
Result.Location := 'ORGetValue';
Result.Win32ErrorOrSuccess := ORGetValue(HandleOrDefault(hxKey),
RefStrOrNil(SubKeyName), RefStrOrNil(ValueName), ValueType,
@Value, @RequiredSize);
if not Result.IsSuccess then
Exit;
if ValueType <> REG_QWORD then
begin
Result.Location := 'ORxGetValueUInt64';
Result.Status := STATUS_OBJECT_TYPE_MISMATCH;
end
else if RequiredSize <> SizeOf(Value) then
begin
Result.Location := 'ORxGetValueUInt64';
Result.Status := STATUS_INVALID_BUFFER_SIZE;
end;
end;
function ORxGetValueString;
var
Info: TORxValueInfo;
begin
Result := ORxGetValue(hxKey, SubKeyName, ValueName, Info, True);
if not Result.IsSuccess then
Exit;
case Info.ValueType of
// Normal strings should be zero-terminated
REG_SZ, REG_EXPAND_SZ:
Value := RtlxCaptureString(Info.Data.Data,
Info.Data.Size div SizeOf(WideChar));
// Symlinks store the target path as-is
REG_LINK:
SetString(Value, PWideChar(Info.Data.Data),
Info.Data.Size div SizeOf(WideChar));
// No value type mean no/empty string
REG_NONE:
begin
Value := '';
if Info.Data.Size <> 0 then
begin
// A REG_NONE with a non-zero size is not close enough to a string
Result.Location := 'ORxGetValueString';
Result.Status := STATUS_INVALID_BUFFER_SIZE;
Exit;
end;
end
else
Result.Location := 'ORxGetValueString';
Result.Status := STATUS_OBJECT_TYPE_MISMATCH;
Exit;
end;
if Assigned(ValueType) then
ValueType^ := Info.ValueType;
end;
function ORxGetValueMultiString;
var
Info: TORxValueInfo;
begin
Result := ORxGetValue(hxKey, SubKeyName, ValueName, Info, True);
if not Result.IsSuccess then
Exit;
case Info.ValueType of
REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ:
Value := RtlxParseWideMultiSz(Info.Data.Data, Info.Data.Size div
SizeOf(WideChar));
else
Result.Location := 'ORxGetValueMultiString';
Result.Status := STATUS_OBJECT_TYPE_MISMATCH;
end;
end;
function ORxSetValue;
begin
Result := LdrxCheckDelayedImport(delayed_ORSetValue);
if not Result.IsSuccess then
Exit;
Result.Location := 'ORSetValue';
Result.Win32ErrorOrSuccess := ORSetValue(HandleOrDefault(hxKey),
RefStrOrNil(ValueName), ValueType, Data, DataSize);
end;
function ORxSetValueUInt32;
begin