-
-
Notifications
You must be signed in to change notification settings - Fork 706
/
registry.d
1863 lines (1615 loc) · 50.5 KB
/
registry.d
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
/**
This library provides Win32 Registry facilities.
Copyright: Copyright 2003-2004 by Matthew Wilson and Synesis Software
Written by Matthew Wilson
License: $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
Author: Matthew Wilson, Kenji Hara
History:
Created 15th March 2003,
Updated 25th April 2004,
Source: $(PHOBOSSRC std/windows/registry.d)
*/
/* /////////////////////////////////////////////////////////////////////////////
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, in both source and binary form, subject to the following
* restrictions:
*
* - The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
* - Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
* - This notice may not be removed or altered from any source
* distribution.
*
* ////////////////////////////////////////////////////////////////////////// */
module std.windows.registry;
version (Windows):
import core.sys.windows.winbase, core.sys.windows.windef, core.sys.windows.winreg;
import std.array;
import std.conv;
import std.exception;
import std.internal.cstring;
import std.internal.windows.advapi32;
import std.system : Endian, endian;
import std.windows.syserror;
//debug = winreg;
debug(winreg) import std.stdio;
private
{
import core.sys.windows.winbase : lstrlenW;
void enforceSucc(LONG res, lazy string message, string fn = __FILE__, size_t ln = __LINE__)
{
if (res != ERROR_SUCCESS)
throw new RegistryException(message, res, fn, ln);
}
}
/* ************* Exceptions *************** */
// Do not use. Left for compatibility.
class Win32Exception : WindowsException
{
@safe
this(string message, string fn = __FILE__, size_t ln = __LINE__, Throwable next = null)
{
super(0, message, fn, ln);
}
@safe
this(string message, int errnum, string fn = __FILE__, size_t ln = __LINE__, Throwable next = null)
{
super(errnum, message, fn, ln);
}
@property int error() { return super.code; }
}
version (StdUnittest) import std.string : startsWith, endsWith;
@safe unittest
{
// Test that we can throw and catch one by its own type
string message = "Test W1";
auto e = collectException!Win32Exception(
enforce(false, new Win32Exception(message)));
assert(e.msg.startsWith(message));
}
@system unittest
{
// ditto
string message = "Test W2";
int code = 5;
auto e = collectException!Win32Exception(
enforce(false, new Win32Exception(message, code)));
assert(e.error == code);
assert(e.msg.startsWith(message));
}
/**
Exception class thrown by the std.windows.registry classes.
*/
class RegistryException
: Win32Exception
{
public:
/**
Creates an instance of the exception.
Params:
message = The message associated with the exception.
*/
@safe
this(string message, string fn = __FILE__, size_t ln = __LINE__, Throwable next = null)
{
super(message, fn, ln, next);
}
/**
Creates an instance of the exception, with the given.
Params:
message = The message associated with the exception.
error = The Win32 error number associated with the exception.
*/
@safe
this(string message, int error, string fn = __FILE__, size_t ln = __LINE__, Throwable next = null)
{
super(message, error, fn, ln, next);
}
}
@system unittest
{
// (i) Test that we can throw and catch one by its own type
string message = "Test 1";
int code = 3;
auto e = collectException!RegistryException(
enforce(false, new RegistryException(message, code)));
assert(e.error == code);
assert(e.msg.startsWith(message));
}
@safe unittest
{
// ditto
string message = "Test 2";
auto e = collectException!RegistryException(
enforce(false, new RegistryException(message)));
assert(e.msg.startsWith(message));
}
/* ************* public enumerations *************** */
/**
Enumeration of the recognised registry access modes.
*/
enum REGSAM
{
KEY_QUERY_VALUE = 0x0001, /// Permission to query subkey data
KEY_SET_VALUE = 0x0002, /// Permission to set subkey data
KEY_CREATE_SUB_KEY = 0x0004, /// Permission to create subkeys
KEY_ENUMERATE_SUB_KEYS = 0x0008, /// Permission to enumerate subkeys
KEY_NOTIFY = 0x0010, /// Permission for change notification
KEY_CREATE_LINK = 0x0020, /// Permission to create a symbolic link
KEY_WOW64_32KEY = 0x0200, /// Enables a 64- or 32-bit application to open a 32-bit key
KEY_WOW64_64KEY = 0x0100, /// Enables a 64- or 32-bit application to open a 64-bit key
KEY_WOW64_RES = 0x0300, ///
KEY_READ = (STANDARD_RIGHTS_READ
| KEY_QUERY_VALUE | KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY)
& ~(SYNCHRONIZE),
/// Combines the STANDARD_RIGHTS_READ, KEY_QUERY_VALUE,
/// KEY_ENUMERATE_SUB_KEYS, and KEY_NOTIFY access rights
KEY_WRITE = (STANDARD_RIGHTS_WRITE
| KEY_SET_VALUE | KEY_CREATE_SUB_KEY)
& ~(SYNCHRONIZE),
/// Combines the STANDARD_RIGHTS_WRITE, KEY_SET_VALUE,
/// and KEY_CREATE_SUB_KEY access rights
KEY_EXECUTE = KEY_READ & ~(SYNCHRONIZE),
/// Permission for read access
KEY_ALL_ACCESS = (STANDARD_RIGHTS_ALL
| KEY_QUERY_VALUE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY
| KEY_ENUMERATE_SUB_KEYS | KEY_NOTIFY | KEY_CREATE_LINK)
& ~(SYNCHRONIZE),
/// Combines the KEY_QUERY_VALUE, KEY_ENUMERATE_SUB_KEYS,
/// KEY_NOTIFY, KEY_CREATE_SUB_KEY, KEY_CREATE_LINK, and
/// KEY_SET_VALUE access rights, plus all the standard
/// access rights except SYNCHRONIZE
}
/**
Enumeration of the recognised registry value types.
*/
enum REG_VALUE_TYPE : DWORD
{
REG_UNKNOWN = -1, ///
REG_NONE = 0, /// The null value type. (In practise this is treated as a zero-length binary array by the Win32 registry)
REG_SZ = 1, /// A zero-terminated string
REG_EXPAND_SZ = 2, /// A zero-terminated string containing expandable environment variable references
REG_BINARY = 3, /// A binary blob
REG_DWORD = 4, /// A 32-bit unsigned integer
REG_DWORD_LITTLE_ENDIAN = 4, /// A 32-bit unsigned integer, stored in little-endian byte order
REG_DWORD_BIG_ENDIAN = 5, /// A 32-bit unsigned integer, stored in big-endian byte order
REG_LINK = 6, /// A registry link
REG_MULTI_SZ = 7, /// A set of zero-terminated strings
REG_RESOURCE_LIST = 8, /// A hardware resource list
REG_FULL_RESOURCE_DESCRIPTOR = 9, /// A hardware resource descriptor
REG_RESOURCE_REQUIREMENTS_LIST = 10, /// A hardware resource requirements list
REG_QWORD = 11, /// A 64-bit unsigned integer
REG_QWORD_LITTLE_ENDIAN = 11, /// A 64-bit unsigned integer, stored in little-endian byte order
}
/* ************* private *************** */
import core.sys.windows.winnt :
DELETE ,
READ_CONTROL ,
WRITE_DAC ,
WRITE_OWNER ,
SYNCHRONIZE ,
STANDARD_RIGHTS_REQUIRED,
STANDARD_RIGHTS_READ ,
STANDARD_RIGHTS_WRITE ,
STANDARD_RIGHTS_EXECUTE ,
STANDARD_RIGHTS_ALL ,
SPECIFIC_RIGHTS_ALL ;
import core.sys.windows.winreg :
REG_CREATED_NEW_KEY ,
REG_OPENED_EXISTING_KEY ;
// Returns samDesired but without WoW64 flags if not in WoW64 mode
// for compatibility with Windows 2000
private REGSAM compatibleRegsam(in REGSAM samDesired)
{
return isWow64 ? samDesired : cast(REGSAM)(samDesired & ~REGSAM.KEY_WOW64_RES);
}
///Returns true, if we are in WoW64 mode and have WoW64 flags
private bool haveWoW64Job(in REGSAM samDesired)
{
return isWow64 && (samDesired & REGSAM.KEY_WOW64_RES);
}
private REG_VALUE_TYPE _RVT_from_Endian(Endian endian)
{
final switch (endian)
{
case Endian.bigEndian:
return REG_VALUE_TYPE.REG_DWORD_BIG_ENDIAN;
case Endian.littleEndian:
return REG_VALUE_TYPE.REG_DWORD_LITTLE_ENDIAN;
}
}
private LONG regCloseKey(in HKEY hkey)
in
{
assert(hkey !is null);
}
do
{
/* No need to attempt to close any of the standard hive keys.
* Although it's documented that calling RegCloseKey() on any of
* these hive keys is ignored, we'd rather not trust the Win32
* API.
*/
if (cast(uint) hkey & 0x80000000)
{
switch (cast(uint) hkey)
{
case HKEY_CLASSES_ROOT:
case HKEY_CURRENT_USER:
case HKEY_LOCAL_MACHINE:
case HKEY_USERS:
case HKEY_PERFORMANCE_DATA:
case HKEY_PERFORMANCE_TEXT:
case HKEY_PERFORMANCE_NLSTEXT:
case HKEY_CURRENT_CONFIG:
case HKEY_DYN_DATA:
return ERROR_SUCCESS;
default:
/* Do nothing */
break;
}
}
return RegCloseKey(hkey);
}
private void regFlushKey(in HKEY hkey)
in
{
assert(hkey !is null);
}
do
{
immutable res = RegFlushKey(hkey);
enforceSucc(res, "Key cannot be flushed");
}
private HKEY regCreateKey(in HKEY hkey, in string subKey, in DWORD dwOptions, in REGSAM samDesired,
in LPSECURITY_ATTRIBUTES lpsa, out DWORD disposition)
in
{
assert(hkey !is null);
assert(subKey !is null);
}
do
{
HKEY hkeyResult;
enforceSucc(RegCreateKeyExW(
hkey, subKey.tempCStringW(), 0, null, dwOptions,
compatibleRegsam(samDesired), cast(LPSECURITY_ATTRIBUTES) lpsa,
&hkeyResult, &disposition),
"Failed to create requested key: \"" ~ subKey ~ "\"");
return hkeyResult;
}
private void regDeleteKey(in HKEY hkey, in string subKey, in REGSAM samDesired)
in
{
assert(hkey !is null);
assert(subKey !is null);
}
do
{
LONG res;
if (haveWoW64Job(samDesired))
{
loadAdvapi32();
res = pRegDeleteKeyExW(hkey, subKey.tempCStringW(), samDesired, 0);
}
else
{
res = RegDeleteKeyW(hkey, subKey.tempCStringW());
}
enforceSucc(res, "Key cannot be deleted: \"" ~ subKey ~ "\"");
}
private void regDeleteValue(in HKEY hkey, in string valueName)
in
{
assert(hkey !is null);
assert(valueName !is null);
}
do
{
enforceSucc(RegDeleteValueW(hkey, valueName.tempCStringW()),
"Value cannot be deleted: \"" ~ valueName ~ "\"");
}
private HKEY regDup(HKEY hkey)
in
{
assert(hkey !is null);
}
do
{
/* Can't duplicate standard keys, but don't need to, so can just return */
if (cast(uint) hkey & 0x80000000)
{
switch (cast(uint) hkey)
{
case HKEY_CLASSES_ROOT:
case HKEY_CURRENT_USER:
case HKEY_LOCAL_MACHINE:
case HKEY_USERS:
case HKEY_PERFORMANCE_DATA:
case HKEY_PERFORMANCE_TEXT:
case HKEY_PERFORMANCE_NLSTEXT:
case HKEY_CURRENT_CONFIG:
case HKEY_DYN_DATA:
return hkey;
default:
/* Do nothing */
break;
}
}
HKEY hkeyDup;
immutable res = RegOpenKeyW(hkey, null, &hkeyDup);
debug(winreg)
{
if (res != ERROR_SUCCESS)
{
writefln("regDup() failed: 0x%08x 0x%08x %d", hkey, hkeyDup, res);
}
assert(res == ERROR_SUCCESS);
}
return (res == ERROR_SUCCESS) ? hkeyDup : null;
}
private LONG regEnumKeyName(in HKEY hkey, in DWORD index, ref wchar[] name, out DWORD cchName)
in
{
assert(hkey !is null);
assert(name !is null);
assert(name.length > 0);
}
out(res)
{
assert(res != ERROR_MORE_DATA);
}
do
{
// The Registry API lies about the lengths of a very few sub-key lengths
// so we have to test to see if it whinges about more data, and provide
// more if it does.
for (;;)
{
cchName = to!DWORD(name.length);
immutable res = RegEnumKeyExW(hkey, index, name.ptr, &cchName, null, null, null, null);
if (res != ERROR_MORE_DATA)
return res;
// Now need to increase the size of the buffer and try again
name.length *= 2;
}
assert(0);
}
private LONG regEnumValueName(in HKEY hkey, in DWORD dwIndex, ref wchar[] name, out DWORD cchName)
in
{
assert(hkey !is null);
}
do
{
for (;;)
{
cchName = to!DWORD(name.length);
immutable res = RegEnumValueW(hkey, dwIndex, name.ptr, &cchName, null, null, null, null);
if (res != ERROR_MORE_DATA)
return res;
name.length *= 2;
}
assert(0);
}
private LONG regGetNumSubKeys(in HKEY hkey, out DWORD cSubKeys, out DWORD cchSubKeyMaxLen)
in
{
assert(hkey !is null);
}
do
{
return RegQueryInfoKeyW(hkey, null, null, null, &cSubKeys,
&cchSubKeyMaxLen, null, null, null, null, null, null);
}
private LONG regGetNumValues(in HKEY hkey, out DWORD cValues, out DWORD cchValueMaxLen)
in
{
assert(hkey !is null);
}
do
{
return RegQueryInfoKeyW(hkey, null, null, null, null, null, null,
&cValues, &cchValueMaxLen, null, null, null);
}
private REG_VALUE_TYPE regGetValueType(in HKEY hkey, in string name)
in
{
assert(hkey !is null);
}
do
{
REG_VALUE_TYPE type;
enforceSucc(RegQueryValueExW(hkey, name.tempCStringW(), null, cast(LPDWORD) &type, null, null),
"Value cannot be opened: \"" ~ name ~ "\"");
return type;
}
private HKEY regOpenKey(in HKEY hkey, in string subKey, in REGSAM samDesired)
in
{
assert(hkey !is null);
assert(subKey !is null);
}
do
{
HKEY hkeyResult;
enforceSucc(RegOpenKeyExW(hkey, subKey.tempCStringW(), 0, compatibleRegsam(samDesired), &hkeyResult),
"Failed to open requested key: \"" ~ subKey ~ "\"");
return hkeyResult;
}
private void regQueryValue(in HKEY hkey, string name, out string value, REG_VALUE_TYPE reqType)
in
{
assert(hkey !is null);
}
do
{
import core.bitop : bswap;
REG_VALUE_TYPE type;
// See https://issues.dlang.org/show_bug.cgi?id=961 on this
union U
{
uint dw;
ulong qw;
}
U u;
void* data = &u.qw;
DWORD cbData = u.qw.sizeof;
auto keynameTmp = name.tempCStringW();
LONG res = RegQueryValueExW(hkey, keynameTmp, null, cast(LPDWORD) &type, data, &cbData);
if (res == ERROR_MORE_DATA)
{
data = (new ubyte[cbData]).ptr;
res = RegQueryValueExW(hkey, keynameTmp, null, cast(LPDWORD) &type, data, &cbData);
}
enforceSucc(res,
"Cannot read the requested value");
enforce(type == reqType,
new RegistryException("Value type has been changed since the value was acquired"));
switch (type)
{
case REG_VALUE_TYPE.REG_SZ:
case REG_VALUE_TYPE.REG_EXPAND_SZ:
auto wstr = (cast(immutable(wchar)*)data)[0 .. cbData / wchar.sizeof];
assert(wstr.length > 0 && wstr[$-1] == '\0');
if (wstr.length && wstr[$-1] == '\0')
wstr.length = wstr.length - 1;
assert(wstr.length == 0 || wstr[$-1] != '\0');
value = wstr.to!string;
break;
case REG_VALUE_TYPE.REG_DWORD_LITTLE_ENDIAN:
version (LittleEndian)
value = to!string(u.dw);
else
value = to!string(bswap(u.dw));
break;
case REG_VALUE_TYPE.REG_DWORD_BIG_ENDIAN:
version (LittleEndian)
value = to!string(bswap(u.dw));
else
value = to!string(u.dw);
break;
case REG_VALUE_TYPE.REG_QWORD_LITTLE_ENDIAN:
value = to!string(u.qw);
break;
case REG_VALUE_TYPE.REG_BINARY:
case REG_VALUE_TYPE.REG_MULTI_SZ:
default:
throw new RegistryException("Cannot read the given value as a string");
}
}
private void regQueryValue(in HKEY hkey, in string name, out string[] value, REG_VALUE_TYPE reqType)
in
{
assert(hkey !is null);
}
do
{
REG_VALUE_TYPE type;
auto keynameTmp = name.tempCStringW();
wchar[] data = new wchar[256];
DWORD cbData = to!DWORD(data.length * wchar.sizeof);
LONG res = RegQueryValueExW(hkey, keynameTmp, null, cast(LPDWORD) &type, data.ptr, &cbData);
if (res == ERROR_MORE_DATA)
{
data.length = cbData / wchar.sizeof;
res = RegQueryValueExW(hkey, keynameTmp, null, cast(LPDWORD) &type, data.ptr, &cbData);
}
else if (res == ERROR_SUCCESS)
{
data.length = cbData / wchar.sizeof;
}
enforceSucc(res, "Cannot read the requested value");
enforce(type == REG_VALUE_TYPE.REG_MULTI_SZ,
new RegistryException("Cannot read the given value as a string"));
enforce(type == reqType,
new RegistryException("Value type has been changed since the value was acquired"));
// Remove last two (or one) null terminator
assert(data.length > 0 && data[$-1] == '\0');
data.length = data.length - 1;
if (data.length > 0 && data[$-1] == '\0')
data.length = data.length - 1;
auto list = std.array.split(data[], "\0");
value.length = list.length;
foreach (i, ref v; value)
{
v = list[i].to!string;
}
}
private void regQueryValue(in HKEY hkey, in string name, out uint value, REG_VALUE_TYPE reqType)
in
{
assert(hkey !is null);
}
do
{
import core.bitop : bswap;
REG_VALUE_TYPE type;
DWORD cbData = value.sizeof;
enforceSucc(RegQueryValueExW(hkey, name.tempCStringW(), null, cast(LPDWORD) &type, &value, &cbData),
"Cannot read the requested value");
enforce(type == reqType,
new RegistryException("Value type has been changed since the value was acquired"));
switch (type)
{
case REG_VALUE_TYPE.REG_DWORD_LITTLE_ENDIAN:
version (LittleEndian)
static assert(REG_VALUE_TYPE.REG_DWORD == REG_VALUE_TYPE.REG_DWORD_LITTLE_ENDIAN);
else
value = bswap(value);
break;
case REG_VALUE_TYPE.REG_DWORD_BIG_ENDIAN:
version (LittleEndian)
value = bswap(value);
else
static assert(REG_VALUE_TYPE.REG_DWORD == REG_VALUE_TYPE.REG_DWORD_BIG_ENDIAN);
break;
default:
throw new RegistryException("Cannot read the given value as a 32-bit integer");
}
}
private void regQueryValue(in HKEY hkey, in string name, out ulong value, REG_VALUE_TYPE reqType)
in
{
assert(hkey !is null);
}
do
{
REG_VALUE_TYPE type;
DWORD cbData = value.sizeof;
enforceSucc(RegQueryValueExW(hkey, name.tempCStringW(), null, cast(LPDWORD) &type, &value, &cbData),
"Cannot read the requested value");
enforce(type == reqType,
new RegistryException("Value type has been changed since the value was acquired"));
switch (type)
{
case REG_VALUE_TYPE.REG_QWORD_LITTLE_ENDIAN:
break;
default:
throw new RegistryException("Cannot read the given value as a 64-bit integer");
}
}
private void regQueryValue(in HKEY hkey, in string name, out byte[] value, REG_VALUE_TYPE reqType)
in
{
assert(hkey !is null);
}
do
{
REG_VALUE_TYPE type;
byte[] data = new byte[100];
DWORD cbData = to!DWORD(data.length);
LONG res;
auto keynameTmp = name.tempCStringW();
res = RegQueryValueExW(hkey, keynameTmp, null, cast(LPDWORD) &type, data.ptr, &cbData);
if (res == ERROR_MORE_DATA)
{
data.length = cbData;
res = RegQueryValueExW(hkey, keynameTmp, null, cast(LPDWORD) &type, data.ptr, &cbData);
}
enforceSucc(res, "Cannot read the requested value");
enforce(type == reqType,
new RegistryException("Value type has been changed since the value was acquired"));
switch (type)
{
case REG_VALUE_TYPE.REG_BINARY:
data.length = cbData;
value = data;
break;
default:
throw new RegistryException("Cannot read the given value as a string");
}
}
private void regSetValue(in HKEY hkey, in string subKey, in REG_VALUE_TYPE type, in LPCVOID lpData, in DWORD cbData)
in
{
assert(hkey !is null);
}
do
{
enforceSucc(RegSetValueExW(hkey, subKey.tempCStringW(), 0, type, cast(BYTE*) lpData, cbData),
"Value cannot be set: \"" ~ subKey ~ "\"");
}
private void regProcessNthKey(Key key, scope void delegate(scope LONG delegate(DWORD, out string)) dg)
{
DWORD cSubKeys;
DWORD cchSubKeyMaxLen;
immutable res = regGetNumSubKeys(key.m_hkey, cSubKeys, cchSubKeyMaxLen);
assert(res == ERROR_SUCCESS);
wchar[] sName = new wchar[cchSubKeyMaxLen + 1];
// Capture `key` in the lambda to keep the object alive (and so its HKEY handle open).
dg((DWORD index, out string name)
{
DWORD cchName;
immutable res = regEnumKeyName(key.m_hkey, index, sName, cchName);
if (res == ERROR_SUCCESS)
{
name = sName[0 .. cchName].to!string;
}
return res;
});
}
private void regProcessNthValue(Key key, scope void delegate(scope LONG delegate(DWORD, out string)) dg)
{
DWORD cValues;
DWORD cchValueMaxLen;
immutable res = regGetNumValues(key.m_hkey, cValues, cchValueMaxLen);
assert(res == ERROR_SUCCESS);
wchar[] sName = new wchar[cchValueMaxLen + 1];
// Capture `key` in the lambda to keep the object alive (and so its HKEY handle open).
dg((DWORD index, out string name)
{
DWORD cchName;
immutable res = regEnumValueName(key.m_hkey, index, sName, cchName);
if (res == ERROR_SUCCESS)
{
name = sName[0 .. cchName].to!string;
}
return res;
});
}
/* ************* public classes *************** */
/**
This class represents a registry key.
*/
class Key
{
@safe pure nothrow
invariant()
{
assert(m_hkey !is null);
}
private:
@safe pure nothrow
this(HKEY hkey, string name, bool created)
in
{
assert(hkey !is null);
}
do
{
m_hkey = hkey;
m_name = name;
}
~this()
{
regCloseKey(m_hkey);
// Even though this is horried waste-of-cycles programming
// we're doing it here so that the
m_hkey = null;
}
public:
/// The name of the key
@property string name() @safe pure nothrow const
{
return m_name;
}
/**
The number of sub keys.
*/
@property size_t keyCount() const
{
uint cSubKeys;
uint cchSubKeyMaxLen;
enforceSucc(regGetNumSubKeys(m_hkey, cSubKeys, cchSubKeyMaxLen),
"Number of sub-keys cannot be determined");
return cSubKeys;
}
/**
An enumerable sequence of all the sub-keys of this key.
*/
@property KeySequence keys() @safe pure
{
return new KeySequence(this);
}
/**
An enumerable sequence of the names of all the sub-keys of this key.
*/
@property KeyNameSequence keyNames() @safe pure
{
return new KeyNameSequence(this);
}
/**
The number of values.
*/
@property size_t valueCount() const
{
uint cValues;
uint cchValueMaxLen;
enforceSucc(regGetNumValues(m_hkey, cValues, cchValueMaxLen),
"Number of values cannot be determined");
return cValues;
}
/**
An enumerable sequence of all the values of this key.
*/
@property ValueSequence values() @safe pure
{
return new ValueSequence(this);
}
/**
An enumerable sequence of the names of all the values of this key.
*/
@property ValueNameSequence valueNames() @safe pure
{
return new ValueNameSequence(this);
}
/**
Returns the named sub-key of this key.
Params:
name = The name of the subkey to create. May not be `null`.
Returns:
The created key.
Throws:
`RegistryException` is thrown if the key cannot be created.
*/
Key createKey(string name, REGSAM access = REGSAM.KEY_ALL_ACCESS)
{
enforce(!name.empty, new RegistryException("Key name is invalid"));
DWORD disposition;
HKEY hkey = regCreateKey(m_hkey, name, 0, access, null, disposition);
assert(hkey !is null);
// Potential resource leak here!!
//
// If the allocation of the memory for Key fails, the HKEY could be
// lost. Hence, we catch such a failure by the finally, and release
// the HKEY there. If the creation of
try
{
Key key = new Key(hkey, name, disposition == REG_CREATED_NEW_KEY);
hkey = null;
return key;
}
finally
{
if (hkey !is null)
{
regCloseKey(hkey);
}
}
}
/**
Returns the named sub-key of this key.
Params:
name = The name of the subkey to aquire. If name is the empty
string, then the called key is duplicated.
access = The desired access; one of the `REGSAM` enumeration.
Returns:
The aquired key.
Throws:
This function never returns `null`. If a key corresponding to
the requested name is not found, `RegistryException` is thrown.
*/
Key getKey(string name, REGSAM access = REGSAM.KEY_READ)
{
if (name.empty)
return new Key(regDup(m_hkey), m_name, false);
HKEY hkey = regOpenKey(m_hkey, name, access);
assert(hkey !is null);
// Potential resource leak here!!
//
// If the allocation of the memory for Key fails, the HKEY could be
// lost. Hence, we catch such a failure by the finally, and release
// the HKEY there. If the creation of
try
{
Key key = new Key(hkey, name, false);
hkey = null;
return key;
}
finally
{
if (hkey != null)
{
regCloseKey(hkey);
}
}
}
/**
Deletes the named key.
Params:
name = The name of the key to delete. May not be `null`.
*/
void deleteKey(string name, REGSAM access = cast(REGSAM) 0)
{
enforce(!name.empty, new RegistryException("Key name is invalid"));
regDeleteKey(m_hkey, name, access);
}
/**
Returns the named value.
If `name` is the empty string, then the default value is returned.
Returns:
This function never returns `null`. If a value corresponding
to the requested name is not found, `RegistryException` is thrown.
*/
Value getValue(string name)
{
return new Value(this, name, regGetValueType(m_hkey, name));
}
/**
Sets the named value with the given 32-bit unsigned integer value.
Params:
name = The name of the value to set. If it is the empty string,
sets the default value.
value = The 32-bit unsigned value to set.
Throws:
If a value corresponding to the requested name is not found,
`RegistryException` is thrown.
*/
void setValue(string name, uint value)