mirrored from git://source.winehq.org/git/wine.git
/
registry.c
2946 lines (2540 loc) · 96.9 KB
/
registry.c
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
/*
* Registry management
*
* Copyright (C) 1999 Alexandre Julliard
*
* Based on misc/registry.c code
* Copyright (C) 1996 Marcus Meissner
* Copyright (C) 1998 Matthew Becker
* Copyright (C) 1999 Sylvain St-Germain
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include "ntstatus.h"
#define WIN32_NO_STATUS
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "winerror.h"
#include "winternl.h"
#include "winuser.h"
#include "wine/unicode.h"
#include "wine/debug.h"
WINE_DEFAULT_DEBUG_CHANNEL(reg);
#define HKEY_SPECIAL_ROOT_FIRST HKEY_CLASSES_ROOT
#define HKEY_SPECIAL_ROOT_LAST HKEY_DYN_DATA
static const WCHAR name_CLASSES_ROOT[] =
{'M','a','c','h','i','n','e','\\',
'S','o','f','t','w','a','r','e','\\',
'C','l','a','s','s','e','s',0};
static const WCHAR name_LOCAL_MACHINE[] =
{'M','a','c','h','i','n','e',0};
static const WCHAR name_USERS[] =
{'U','s','e','r',0};
static const WCHAR name_PERFORMANCE_DATA[] =
{'P','e','r','f','D','a','t','a',0};
static const WCHAR name_CURRENT_CONFIG[] =
{'M','a','c','h','i','n','e','\\',
'S','y','s','t','e','m','\\',
'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
'H','a','r','d','w','a','r','e',' ','P','r','o','f','i','l','e','s','\\',
'C','u','r','r','e','n','t',0};
static const WCHAR name_DYN_DATA[] =
{'D','y','n','D','a','t','a',0};
static const WCHAR * const root_key_names[] =
{
name_CLASSES_ROOT,
NULL, /* HKEY_CURRENT_USER is determined dynamically */
name_LOCAL_MACHINE,
name_USERS,
name_PERFORMANCE_DATA,
name_CURRENT_CONFIG,
name_DYN_DATA
};
#define NB_SPECIAL_ROOT_KEYS (sizeof(root_key_names)/sizeof(root_key_names[0]))
static HKEY special_root_keys[NB_SPECIAL_ROOT_KEYS];
static BOOL hkcu_cache_disabled;
static const BOOL is_win64 = (sizeof(void *) > sizeof(int));
/* check if value type needs string conversion (Ansi<->Unicode) */
static inline BOOL is_string( DWORD type )
{
return (type == REG_SZ) || (type == REG_EXPAND_SZ) || (type == REG_MULTI_SZ);
}
/* check if current version is NT or Win95 */
static inline BOOL is_version_nt(void)
{
return !(GetVersion() & 0x80000000);
}
static BOOL is_wow6432node( const UNICODE_STRING *name )
{
static const WCHAR wow6432nodeW[] = {'W','o','w','6','4','3','2','N','o','d','e'};
return (name->Length == sizeof(wow6432nodeW) &&
!memicmpW( name->Buffer, wow6432nodeW, sizeof(wow6432nodeW)/sizeof(WCHAR) ));
}
/* open the Wow6432Node subkey of the specified key */
static HANDLE open_wow6432node( HANDLE key, const UNICODE_STRING *name )
{
static const WCHAR wow6432nodeW[] = {'W','o','w','6','4','3','2','N','o','d','e',0};
OBJECT_ATTRIBUTES attr;
UNICODE_STRING nameW;
HANDLE ret;
attr.Length = sizeof(attr);
attr.RootDirectory = key;
attr.ObjectName = &nameW;
attr.Attributes = 0;
attr.SecurityDescriptor = NULL;
attr.SecurityQualityOfService = NULL;
RtlInitUnicodeString( &nameW, wow6432nodeW );
if (NtOpenKey( &ret, MAXIMUM_ALLOWED, &attr )) ret = 0;
return ret;
}
/* wrapper for NtCreateKey that creates the key recursively if necessary */
static NTSTATUS create_key( HKEY *retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr,
const UNICODE_STRING *class, ULONG options, PULONG dispos )
{
BOOL force_wow32 = is_win64 && (access & KEY_WOW64_32KEY);
NTSTATUS status = STATUS_OBJECT_NAME_NOT_FOUND;
if (!force_wow32) status = NtCreateKey( (HANDLE *)retkey, access, attr, 0, class, options, dispos );
if (status == STATUS_OBJECT_NAME_NOT_FOUND)
{
HANDLE subkey, root = attr->RootDirectory;
WCHAR *buffer = attr->ObjectName->Buffer;
DWORD attrs, pos = 0, i = 0, len = attr->ObjectName->Length / sizeof(WCHAR);
UNICODE_STRING str;
while (i < len && buffer[i] != '\\') i++;
if (i == len && !force_wow32) return status;
attrs = attr->Attributes;
attr->ObjectName = &str;
for (;;)
{
str.Buffer = buffer + pos;
str.Length = (i - pos) * sizeof(WCHAR);
if (force_wow32 && pos)
{
if (is_wow6432node( &str )) force_wow32 = FALSE;
else if ((subkey = open_wow6432node( attr->RootDirectory, &str )))
{
if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
attr->RootDirectory = subkey;
force_wow32 = FALSE;
}
}
if (i == len)
{
attr->Attributes = attrs;
status = NtCreateKey( (PHANDLE)retkey, access, attr, 0, class, options, dispos );
}
else
{
attr->Attributes = attrs & ~OBJ_OPENLINK;
status = NtCreateKey( &subkey, access, attr, 0, class,
options & ~REG_OPTION_CREATE_LINK, dispos );
}
if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
if (status) return status;
if (i == len) break;
attr->RootDirectory = subkey;
while (i < len && buffer[i] == '\\') i++;
pos = i;
while (i < len && buffer[i] != '\\') i++;
}
}
return status;
}
/* wrapper for NtOpenKey to handle Wow6432 nodes */
static NTSTATUS open_key( HKEY *retkey, ACCESS_MASK access, OBJECT_ATTRIBUTES *attr )
{
NTSTATUS status;
BOOL force_wow32 = is_win64 && (access & KEY_WOW64_32KEY);
HANDLE subkey, root = attr->RootDirectory;
WCHAR *buffer = attr->ObjectName->Buffer;
DWORD attrs, pos = 0, i = 0, len = attr->ObjectName->Length / sizeof(WCHAR);
UNICODE_STRING str;
if (!force_wow32) return NtOpenKey( (HANDLE *)retkey, access, attr );
if (len && buffer[0] == '\\') return STATUS_OBJECT_PATH_INVALID;
while (i < len && buffer[i] != '\\') i++;
attrs = attr->Attributes;
attr->ObjectName = &str;
for (;;)
{
str.Buffer = buffer + pos;
str.Length = (i - pos) * sizeof(WCHAR);
if (force_wow32 && pos)
{
if (is_wow6432node( &str )) force_wow32 = FALSE;
else if ((subkey = open_wow6432node( attr->RootDirectory, &str )))
{
if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
attr->RootDirectory = subkey;
force_wow32 = FALSE;
}
}
if (i == len)
{
attr->Attributes = attrs;
status = NtOpenKey( (PHANDLE)retkey, access, attr );
}
else
{
attr->Attributes = attrs & ~OBJ_OPENLINK;
status = NtOpenKey( &subkey, access, attr );
}
if (attr->RootDirectory != root) NtClose( attr->RootDirectory );
if (status) return status;
if (i == len) break;
attr->RootDirectory = subkey;
while (i < len && buffer[i] == '\\') i++;
pos = i;
while (i < len && buffer[i] != '\\') i++;
}
return status;
}
/* create one of the HKEY_* special root keys */
static HKEY create_special_root_hkey( HKEY hkey, DWORD access )
{
HKEY ret = 0;
int idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
if (HandleToUlong(hkey) == HandleToUlong(HKEY_CURRENT_USER))
{
if (RtlOpenCurrentUser( access, (HANDLE *)&hkey )) return 0;
TRACE( "HKEY_CURRENT_USER -> %p\n", hkey );
/* don't cache the key in the table if caching is disabled */
if (hkcu_cache_disabled)
return hkey;
}
else
{
OBJECT_ATTRIBUTES attr;
UNICODE_STRING name;
attr.Length = sizeof(attr);
attr.RootDirectory = 0;
attr.ObjectName = &name;
attr.Attributes = 0;
attr.SecurityDescriptor = NULL;
attr.SecurityQualityOfService = NULL;
RtlInitUnicodeString( &name, root_key_names[idx] );
if (create_key( &hkey, access, &attr, NULL, 0, NULL )) return 0;
TRACE( "%s -> %p\n", debugstr_w(attr.ObjectName->Buffer), hkey );
}
if (!(access & (KEY_WOW64_64KEY | KEY_WOW64_32KEY)))
{
if (!(ret = InterlockedCompareExchangePointer( (void **)&special_root_keys[idx], hkey, 0 )))
ret = hkey;
else
NtClose( hkey ); /* somebody beat us to it */
}
else
ret = hkey;
return ret;
}
/* map the hkey from special root to normal key if necessary */
static inline HKEY get_special_root_hkey( HKEY hkey, REGSAM access )
{
HKEY ret = hkey;
if ((HandleToUlong(hkey) >= HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
&& (HandleToUlong(hkey) <= HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
{
REGSAM mask = 0;
if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT))
mask = KEY_WOW64_32KEY | KEY_WOW64_64KEY;
if ((access & mask) ||
!(ret = special_root_keys[HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST)]))
ret = create_special_root_hkey( hkey, MAXIMUM_ALLOWED | (access & mask) );
}
return ret;
}
/******************************************************************************
* RegOverridePredefKey [ADVAPI32.@]
*/
LSTATUS WINAPI RegOverridePredefKey( HKEY hkey, HKEY override )
{
HKEY old_key;
int idx;
TRACE("(%p %p)\n", hkey, override);
if ((HandleToUlong(hkey) < HandleToUlong(HKEY_SPECIAL_ROOT_FIRST))
|| (HandleToUlong(hkey) > HandleToUlong(HKEY_SPECIAL_ROOT_LAST)))
return ERROR_INVALID_PARAMETER;
idx = HandleToUlong(hkey) - HandleToUlong(HKEY_SPECIAL_ROOT_FIRST);
if (override)
{
NTSTATUS status = NtDuplicateObject( GetCurrentProcess(), override,
GetCurrentProcess(), (HANDLE *)&override,
0, 0, DUPLICATE_SAME_ACCESS );
if (status) return RtlNtStatusToDosError( status );
}
old_key = InterlockedExchangePointer( (void **)&special_root_keys[idx], override );
if (old_key) NtClose( old_key );
return ERROR_SUCCESS;
}
/******************************************************************************
* RegCreateKeyExW [ADVAPI32.@]
*
* See RegCreateKeyExA.
*/
LSTATUS WINAPI RegCreateKeyExW( HKEY hkey, LPCWSTR name, DWORD reserved, LPWSTR class,
DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
PHKEY retkey, LPDWORD dispos )
{
OBJECT_ATTRIBUTES attr;
UNICODE_STRING nameW, classW;
if (reserved) return ERROR_INVALID_PARAMETER;
if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
attr.Length = sizeof(attr);
attr.RootDirectory = hkey;
attr.ObjectName = &nameW;
attr.Attributes = 0;
attr.SecurityDescriptor = NULL;
attr.SecurityQualityOfService = NULL;
if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
RtlInitUnicodeString( &nameW, name );
RtlInitUnicodeString( &classW, class );
return RtlNtStatusToDosError( create_key( retkey, access, &attr, &classW, options, dispos ) );
}
/******************************************************************************
* RegCreateKeyExA [ADVAPI32.@]
*
* Open a registry key, creating it if it doesn't exist.
*
* PARAMS
* hkey [I] Handle of the parent registry key
* name [I] Name of the new key to open or create
* reserved [I] Reserved, pass 0
* class [I] The object type of the new key
* options [I] Flags controlling the key creation (REG_OPTION_* flags from "winnt.h")
* access [I] Access level desired
* sa [I] Security attributes for the key
* retkey [O] Destination for the resulting handle
* dispos [O] Receives REG_CREATED_NEW_KEY or REG_OPENED_EXISTING_KEY
*
* RETURNS
* Success: ERROR_SUCCESS.
* Failure: A standard Win32 error code. retkey remains untouched.
*
* FIXME
* MAXIMUM_ALLOWED in access mask not supported by server
*/
LSTATUS WINAPI RegCreateKeyExA( HKEY hkey, LPCSTR name, DWORD reserved, LPSTR class,
DWORD options, REGSAM access, SECURITY_ATTRIBUTES *sa,
PHKEY retkey, LPDWORD dispos )
{
OBJECT_ATTRIBUTES attr;
UNICODE_STRING classW;
ANSI_STRING nameA, classA;
NTSTATUS status;
if (reserved) return ERROR_INVALID_PARAMETER;
if (!is_version_nt())
{
access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
if (name && *name == '\\') name++; /* win9x,ME ignores one (and only one) beginning backslash */
}
if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
attr.Length = sizeof(attr);
attr.RootDirectory = hkey;
attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
attr.Attributes = 0;
attr.SecurityDescriptor = NULL;
attr.SecurityQualityOfService = NULL;
if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
RtlInitAnsiString( &nameA, name );
RtlInitAnsiString( &classA, class );
if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
&nameA, FALSE )))
{
if (!(status = RtlAnsiStringToUnicodeString( &classW, &classA, TRUE )))
{
status = create_key( retkey, access, &attr, &classW, options, dispos );
RtlFreeUnicodeString( &classW );
}
}
return RtlNtStatusToDosError( status );
}
/******************************************************************************
* RegCreateKeyW [ADVAPI32.@]
*
* Creates the specified reg key.
*
* PARAMS
* hKey [I] Handle to an open key.
* lpSubKey [I] Name of a key that will be opened or created.
* phkResult [O] Receives a handle to the opened or created key.
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: nonzero error code defined in Winerror.h
*/
LSTATUS WINAPI RegCreateKeyW( HKEY hkey, LPCWSTR lpSubKey, PHKEY phkResult )
{
/* FIXME: previous implementation converted ERROR_INVALID_HANDLE to ERROR_BADKEY, */
/* but at least my version of NT (4.0 SP5) doesn't do this. -- AJ */
return RegCreateKeyExW( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
MAXIMUM_ALLOWED, NULL, phkResult, NULL );
}
/******************************************************************************
* RegCreateKeyA [ADVAPI32.@]
*
* See RegCreateKeyW.
*/
LSTATUS WINAPI RegCreateKeyA( HKEY hkey, LPCSTR lpSubKey, PHKEY phkResult )
{
return RegCreateKeyExA( hkey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
MAXIMUM_ALLOWED, NULL, phkResult, NULL );
}
/******************************************************************************
* RegOpenKeyExW [ADVAPI32.@]
*
* See RegOpenKeyExA.
*/
LSTATUS WINAPI RegOpenKeyExW( HKEY hkey, LPCWSTR name, DWORD options, REGSAM access, PHKEY retkey )
{
OBJECT_ATTRIBUTES attr;
UNICODE_STRING nameW;
/* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
if (!retkey) return ERROR_INVALID_PARAMETER;
if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
attr.Length = sizeof(attr);
attr.RootDirectory = hkey;
attr.ObjectName = &nameW;
attr.Attributes = 0;
attr.SecurityDescriptor = NULL;
attr.SecurityQualityOfService = NULL;
if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
RtlInitUnicodeString( &nameW, name );
return RtlNtStatusToDosError( open_key( retkey, access, &attr ) );
}
/******************************************************************************
* RegOpenKeyExA [ADVAPI32.@]
*
* Open a registry key.
*
* PARAMS
* hkey [I] Handle of open key
* name [I] Name of subkey to open
* options [I] Open options (can be set to REG_OPTION_OPEN_LINK)
* access [I] Security access mask
* retkey [O] Handle to open key
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: A standard Win32 error code. retkey is set to 0.
*
* NOTES
* Unlike RegCreateKeyExA(), this function will not create the key if it
* does not exist.
*/
LSTATUS WINAPI RegOpenKeyExA( HKEY hkey, LPCSTR name, DWORD options, REGSAM access, PHKEY retkey )
{
OBJECT_ATTRIBUTES attr;
STRING nameA;
NTSTATUS status;
if (!is_version_nt()) access = MAXIMUM_ALLOWED; /* Win95 ignores the access mask */
else
{
/* NT+ allows beginning backslash for HKEY_CLASSES_ROOT */
if (HandleToUlong(hkey) == HandleToUlong(HKEY_CLASSES_ROOT) && name && *name == '\\') name++;
}
if (!(hkey = get_special_root_hkey( hkey, access ))) return ERROR_INVALID_HANDLE;
attr.Length = sizeof(attr);
attr.RootDirectory = hkey;
attr.ObjectName = &NtCurrentTeb()->StaticUnicodeString;
attr.Attributes = 0;
attr.SecurityDescriptor = NULL;
attr.SecurityQualityOfService = NULL;
if (options & REG_OPTION_OPEN_LINK) attr.Attributes |= OBJ_OPENLINK;
RtlInitAnsiString( &nameA, name );
if (!(status = RtlAnsiStringToUnicodeString( &NtCurrentTeb()->StaticUnicodeString,
&nameA, FALSE )))
{
status = open_key( retkey, access, &attr );
}
return RtlNtStatusToDosError( status );
}
/******************************************************************************
* RegOpenKeyW [ADVAPI32.@]
*
* See RegOpenKeyA.
*/
LSTATUS WINAPI RegOpenKeyW( HKEY hkey, LPCWSTR name, PHKEY retkey )
{
if (!retkey)
return ERROR_INVALID_PARAMETER;
if (!name || !*name)
{
*retkey = hkey;
return ERROR_SUCCESS;
}
return RegOpenKeyExW( hkey, name, 0, MAXIMUM_ALLOWED, retkey );
}
/******************************************************************************
* RegOpenKeyA [ADVAPI32.@]
*
* Open a registry key.
*
* PARAMS
* hkey [I] Handle of parent key to open the new key under
* name [I] Name of the key under hkey to open
* retkey [O] Destination for the resulting Handle
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: A standard Win32 error code. When retkey is valid, *retkey is set to 0.
*/
LSTATUS WINAPI RegOpenKeyA( HKEY hkey, LPCSTR name, PHKEY retkey )
{
if (!retkey)
return ERROR_INVALID_PARAMETER;
if (!name || !*name)
{
*retkey = hkey;
return ERROR_SUCCESS;
}
return RegOpenKeyExA( hkey, name, 0, MAXIMUM_ALLOWED, retkey );
}
/******************************************************************************
* RegOpenCurrentUser [ADVAPI32.@]
*
* Get a handle to the HKEY_CURRENT_USER key for the user
* the current thread is impersonating.
*
* PARAMS
* access [I] Desired access rights to the key
* retkey [O] Handle to the opened key
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: nonzero error code from Winerror.h
*
* FIXME
* This function is supposed to retrieve a handle to the
* HKEY_CURRENT_USER for the user the current thread is impersonating.
* Since Wine does not currently allow threads to impersonate other users,
* this stub should work fine.
*/
LSTATUS WINAPI RegOpenCurrentUser( REGSAM access, PHKEY retkey )
{
return RegOpenKeyExA( HKEY_CURRENT_USER, "", 0, access, retkey );
}
/******************************************************************************
* RegEnumKeyExW [ADVAPI32.@]
*
* Enumerate subkeys of the specified open registry key.
*
* PARAMS
* hkey [I] Handle to key to enumerate
* index [I] Index of subkey to enumerate
* name [O] Buffer for subkey name
* name_len [O] Size of subkey buffer
* reserved [I] Reserved
* class [O] Buffer for class string
* class_len [O] Size of class buffer
* ft [O] Time key last written to
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: System error code. If there are no more subkeys available, the
* function returns ERROR_NO_MORE_ITEMS.
*/
LSTATUS WINAPI RegEnumKeyExW( HKEY hkey, DWORD index, LPWSTR name, LPDWORD name_len,
LPDWORD reserved, LPWSTR class, LPDWORD class_len, FILETIME *ft )
{
NTSTATUS status;
char buffer[256], *buf_ptr = buffer;
KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
DWORD total_size;
TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
name_len ? *name_len : 0, reserved, class, class_len, ft );
if (reserved) return ERROR_INVALID_PARAMETER;
if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
status = NtEnumerateKey( hkey, index, KeyNodeInformation,
buffer, sizeof(buffer), &total_size );
while (status == STATUS_BUFFER_OVERFLOW)
{
/* retry with a dynamically allocated buffer */
if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
return ERROR_NOT_ENOUGH_MEMORY;
info = (KEY_NODE_INFORMATION *)buf_ptr;
status = NtEnumerateKey( hkey, index, KeyNodeInformation,
buf_ptr, total_size, &total_size );
}
if (!status)
{
DWORD len = info->NameLength / sizeof(WCHAR);
DWORD cls_len = info->ClassLength / sizeof(WCHAR);
if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
status = STATUS_BUFFER_OVERFLOW;
else
{
*name_len = len;
memcpy( name, info->Name, info->NameLength );
name[len] = 0;
if (class_len)
{
*class_len = cls_len;
if (class)
{
memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
class[cls_len] = 0;
}
}
}
}
if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
return RtlNtStatusToDosError( status );
}
/******************************************************************************
* RegEnumKeyExA [ADVAPI32.@]
*
* See RegEnumKeyExW.
*/
LSTATUS WINAPI RegEnumKeyExA( HKEY hkey, DWORD index, LPSTR name, LPDWORD name_len,
LPDWORD reserved, LPSTR class, LPDWORD class_len, FILETIME *ft )
{
NTSTATUS status;
char buffer[256], *buf_ptr = buffer;
KEY_NODE_INFORMATION *info = (KEY_NODE_INFORMATION *)buffer;
DWORD total_size;
TRACE( "(%p,%d,%p,%p(%u),%p,%p,%p,%p)\n", hkey, index, name, name_len,
name_len ? *name_len : 0, reserved, class, class_len, ft );
if (reserved) return ERROR_INVALID_PARAMETER;
if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
status = NtEnumerateKey( hkey, index, KeyNodeInformation,
buffer, sizeof(buffer), &total_size );
while (status == STATUS_BUFFER_OVERFLOW)
{
/* retry with a dynamically allocated buffer */
if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
return ERROR_NOT_ENOUGH_MEMORY;
info = (KEY_NODE_INFORMATION *)buf_ptr;
status = NtEnumerateKey( hkey, index, KeyNodeInformation,
buf_ptr, total_size, &total_size );
}
if (!status)
{
DWORD len, cls_len;
RtlUnicodeToMultiByteSize( &len, info->Name, info->NameLength );
RtlUnicodeToMultiByteSize( &cls_len, (WCHAR *)(buf_ptr + info->ClassOffset),
info->ClassLength );
if (ft) *ft = *(FILETIME *)&info->LastWriteTime;
if (len >= *name_len || (class && class_len && (cls_len >= *class_len)))
status = STATUS_BUFFER_OVERFLOW;
else
{
*name_len = len;
RtlUnicodeToMultiByteN( name, len, NULL, info->Name, info->NameLength );
name[len] = 0;
if (class_len)
{
*class_len = cls_len;
if (class)
{
RtlUnicodeToMultiByteN( class, cls_len, NULL,
(WCHAR *)(buf_ptr + info->ClassOffset),
info->ClassLength );
class[cls_len] = 0;
}
}
}
}
if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
return RtlNtStatusToDosError( status );
}
/******************************************************************************
* RegEnumKeyW [ADVAPI32.@]
*
* Enumerates subkeys of the specified open reg key.
*
* PARAMS
* hKey [I] Handle to an open key.
* dwIndex [I] Index of the subkey of hKey to retrieve.
* lpName [O] Name of the subkey.
* cchName [I] Size of lpName in TCHARS.
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: system error code. If there are no more subkeys available, the
* function returns ERROR_NO_MORE_ITEMS.
*/
LSTATUS WINAPI RegEnumKeyW( HKEY hkey, DWORD index, LPWSTR name, DWORD name_len )
{
return RegEnumKeyExW( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
}
/******************************************************************************
* RegEnumKeyA [ADVAPI32.@]
*
* See RegEnumKeyW.
*/
LSTATUS WINAPI RegEnumKeyA( HKEY hkey, DWORD index, LPSTR name, DWORD name_len )
{
return RegEnumKeyExA( hkey, index, name, &name_len, NULL, NULL, NULL, NULL );
}
/******************************************************************************
* RegQueryInfoKeyW [ADVAPI32.@]
*
* Retrieves information about the specified registry key.
*
* PARAMS
* hkey [I] Handle to key to query
* class [O] Buffer for class string
* class_len [O] Size of class string buffer
* reserved [I] Reserved
* subkeys [O] Buffer for number of subkeys
* max_subkey [O] Buffer for longest subkey name length
* max_class [O] Buffer for longest class string length
* values [O] Buffer for number of value entries
* max_value [O] Buffer for longest value name length
* max_data [O] Buffer for longest value data length
* security [O] Buffer for security descriptor length
* modif [O] Modification time
*
* RETURNS
* Success: ERROR_SUCCESS
* Failure: system error code.
*
* NOTES
* - win95 allows class to be valid and class_len to be NULL
* - winnt returns ERROR_INVALID_PARAMETER if class is valid and class_len is NULL
* - both allow class to be NULL and class_len to be NULL
* (it's hard to test validity, so test !NULL instead)
*/
LSTATUS WINAPI RegQueryInfoKeyW( HKEY hkey, LPWSTR class, LPDWORD class_len, LPDWORD reserved,
LPDWORD subkeys, LPDWORD max_subkey, LPDWORD max_class,
LPDWORD values, LPDWORD max_value, LPDWORD max_data,
LPDWORD security, FILETIME *modif )
{
NTSTATUS status;
char buffer[256], *buf_ptr = buffer;
KEY_FULL_INFORMATION *info = (KEY_FULL_INFORMATION *)buffer;
DWORD total_size;
TRACE( "(%p,%p,%d,%p,%p,%p,%p,%p,%p,%p,%p)\n", hkey, class, class_len ? *class_len : 0,
reserved, subkeys, max_subkey, values, max_value, max_data, security, modif );
if (class && !class_len && is_version_nt()) return ERROR_INVALID_PARAMETER;
if (!(hkey = get_special_root_hkey( hkey, 0 ))) return ERROR_INVALID_HANDLE;
status = NtQueryKey( hkey, KeyFullInformation, buffer, sizeof(buffer), &total_size );
if (status && status != STATUS_BUFFER_OVERFLOW) goto done;
if (class)
{
/* retry with a dynamically allocated buffer */
while (status == STATUS_BUFFER_OVERFLOW)
{
if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
if (!(buf_ptr = HeapAlloc( GetProcessHeap(), 0, total_size )))
return ERROR_NOT_ENOUGH_MEMORY;
info = (KEY_FULL_INFORMATION *)buf_ptr;
status = NtQueryKey( hkey, KeyFullInformation, buf_ptr, total_size, &total_size );
}
if (status) goto done;
if (class_len && (info->ClassLength/sizeof(WCHAR) + 1 > *class_len))
{
status = STATUS_BUFFER_OVERFLOW;
}
else
{
memcpy( class, buf_ptr + info->ClassOffset, info->ClassLength );
class[info->ClassLength/sizeof(WCHAR)] = 0;
}
}
else status = STATUS_SUCCESS;
if (class_len) *class_len = info->ClassLength / sizeof(WCHAR);
if (subkeys) *subkeys = info->SubKeys;
if (max_subkey) *max_subkey = info->MaxNameLen;
if (max_class) *max_class = info->MaxClassLen;
if (values) *values = info->Values;
if (max_value) *max_value = info->MaxValueNameLen;
if (max_data) *max_data = info->MaxValueDataLen;
if (modif) *modif = *(FILETIME *)&info->LastWriteTime;
done:
if (buf_ptr != buffer) HeapFree( GetProcessHeap(), 0, buf_ptr );
return RtlNtStatusToDosError( status );
}
/******************************************************************************
* RegQueryMultipleValuesA [ADVAPI32.@]
*
* Retrieves the type and data for a list of value names associated with a key.
*
* PARAMS
* hKey [I] Handle to an open key.
* val_list [O] Array of VALENT structures that describes the entries.
* num_vals [I] Number of elements in val_list.
* lpValueBuf [O] Pointer to a buffer that receives the data for each value.
* ldwTotsize [I/O] Size of lpValueBuf.
*
* RETURNS
* Success: ERROR_SUCCESS. ldwTotsize contains num bytes copied.
* Failure: nonzero error code from Winerror.h ldwTotsize contains num needed
* bytes.
*/
LSTATUS WINAPI RegQueryMultipleValuesA( HKEY hkey, PVALENTA val_list, DWORD num_vals,
LPSTR lpValueBuf, LPDWORD ldwTotsize )
{
unsigned int i;
DWORD maxBytes = *ldwTotsize;
HRESULT status;
LPSTR bufptr = lpValueBuf;
*ldwTotsize = 0;
TRACE("(%p,%p,%d,%p,%p=%d)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
for(i=0; i < num_vals; ++i)
{
val_list[i].ve_valuelen=0;
status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
if(status != ERROR_SUCCESS)
{
return status;
}
if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
{
status = RegQueryValueExA(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
(LPBYTE)bufptr, &val_list[i].ve_valuelen);
if(status != ERROR_SUCCESS)
{
return status;
}
val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
bufptr += val_list[i].ve_valuelen;
}
*ldwTotsize += val_list[i].ve_valuelen;
}
return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
}
/******************************************************************************
* RegQueryMultipleValuesW [ADVAPI32.@]
*
* See RegQueryMultipleValuesA.
*/
LSTATUS WINAPI RegQueryMultipleValuesW( HKEY hkey, PVALENTW val_list, DWORD num_vals,
LPWSTR lpValueBuf, LPDWORD ldwTotsize )
{
unsigned int i;
DWORD maxBytes = *ldwTotsize;
HRESULT status;
LPSTR bufptr = (LPSTR)lpValueBuf;
*ldwTotsize = 0;
TRACE("(%p,%p,%d,%p,%p=%d)\n", hkey, val_list, num_vals, lpValueBuf, ldwTotsize, *ldwTotsize);
for(i=0; i < num_vals; ++i)
{
val_list[i].ve_valuelen=0;
status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, NULL, NULL, &val_list[i].ve_valuelen);
if(status != ERROR_SUCCESS)
{
return status;
}
if(lpValueBuf != NULL && *ldwTotsize + val_list[i].ve_valuelen <= maxBytes)
{
status = RegQueryValueExW(hkey, val_list[i].ve_valuename, NULL, &val_list[i].ve_type,
(LPBYTE)bufptr, &val_list[i].ve_valuelen);
if(status != ERROR_SUCCESS)
{
return status;
}
val_list[i].ve_valueptr = (DWORD_PTR)bufptr;
bufptr += val_list[i].ve_valuelen;
}
*ldwTotsize += val_list[i].ve_valuelen;
}
return lpValueBuf != NULL && *ldwTotsize <= maxBytes ? ERROR_SUCCESS : ERROR_MORE_DATA;
}
/******************************************************************************
* RegQueryInfoKeyA [ADVAPI32.@]
*
* Retrieves information about a registry key.
*
* PARAMS
* hKey [I] Handle to an open key.
* lpClass [O] Class string of the key.
* lpcClass [I/O] size of lpClass.
* lpReserved [I] Reserved; must be NULL.
* lpcSubKeys [O] Number of subkeys contained by the key.
* lpcMaxSubKeyLen [O] Size of the key's subkey with the longest name.
* lpcMaxClassLen [O] Size of the longest string specifying a subkey
* class in TCHARS.
* lpcValues [O] Number of values associated with the key.
* lpcMaxValueNameLen [O] Size of the key's longest value name in TCHARS.
* lpcMaxValueLen [O] Longest data component among the key's values
* lpcbSecurityDescriptor [O] Size of the key's security descriptor.
* lpftLastWriteTime [O] FILETIME structure that is the last write time.
*