3 * Copyright (C) 2003 ReactOS Team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * COPYRIGHT: See COPYING in the top level directory
21 * PROJECT: ReactOS text-mode setup
22 * FILE: subsys/system/usetup/registry.c
23 * PURPOSE: Registry creation functions
24 * PROGRAMMER: Eric Kohl
27 /* INCLUDES *****************************************************************/
34 #define FLG_ADDREG_BINVALUETYPE 0x00000001
35 #define FLG_ADDREG_NOCLOBBER 0x00000002
36 #define FLG_ADDREG_DELVAL 0x00000004
37 #define FLG_ADDREG_APPEND 0x00000008
38 #define FLG_ADDREG_KEYONLY 0x00000010
39 #define FLG_ADDREG_OVERWRITEONLY 0x00000020
40 #define FLG_ADDREG_TYPE_SZ 0x00000000
41 #define FLG_ADDREG_TYPE_MULTI_SZ 0x00010000
42 #define FLG_ADDREG_TYPE_EXPAND_SZ 0x00020000
43 #define FLG_ADDREG_TYPE_BINARY (0x00000000 | FLG_ADDREG_BINVALUETYPE)
44 #define FLG_ADDREG_TYPE_DWORD (0x00010000 | FLG_ADDREG_BINVALUETYPE)
45 #define FLG_ADDREG_TYPE_NONE (0x00020000 | FLG_ADDREG_BINVALUETYPE)
46 #define FLG_ADDREG_TYPE_MASK (0xFFFF0000 | FLG_ADDREG_BINVALUETYPE)
50 typedef struct _REG_DISK_MOUNT_INFO
53 LARGE_INTEGER StartingOffset
;
54 } REG_DISK_MOUNT_INFO
, *PREG_DISK_MOUNT_INFO
;
58 /* FUNCTIONS ****************************************************************/
61 GetRootKey (PWCHAR Name
)
63 if (!_wcsicmp (Name
, L
"HKCR"))
65 wcscpy (Name
, L
"\\Registry\\Machine\\SOFTWARE\\Classes\\");
69 if (!_wcsicmp (Name
, L
"HKCU"))
71 wcscpy (Name
, L
"\\Registry\\User\\.DEFAULT\\");
75 if (!_wcsicmp (Name
, L
"HKLM"))
77 wcscpy (Name
, L
"\\Registry\\Machine\\");
81 if (!_wcsicmp (Name
, L
"HKU"))
83 wcscpy (Name
, L
"\\Registry\\User\\");
88 if (!_wcsicmp (Name
, L
"HKR"))
96 /***********************************************************************
97 * append_multi_sz_value
99 * Append a multisz string to a multisz registry value.
103 append_multi_sz_value (HANDLE hkey
,
105 const WCHAR
*strings
,
108 DWORD size
, type
, total
;
111 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
112 if (type
!= REG_MULTI_SZ
) return;
114 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, (size
+ str_size
) * sizeof(WCHAR
) ))) return;
115 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
117 /* compare each string against all the existing ones */
121 int len
= strlenW(strings
) + 1;
123 for (p
= buffer
; *p
; p
+= strlenW(p
) + 1)
124 if (!strcmpiW( p
, strings
)) break;
126 if (!*p
) /* not found, need to append it */
128 memcpy( p
, strings
, len
* sizeof(WCHAR
) );
136 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
) );
137 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
, (BYTE
*)buffer
, total
);
140 HeapFree( GetProcessHeap(), 0, buffer
);
144 /***********************************************************************
145 * delete_multi_sz_value
147 * Remove a string from a multisz registry value.
150 static void delete_multi_sz_value( HKEY hkey
, const WCHAR
*value
, const WCHAR
*string
)
153 WCHAR
*buffer
, *src
, *dst
;
155 if (RegQueryValueExW( hkey
, value
, NULL
, &type
, NULL
, &size
)) return;
156 if (type
!= REG_MULTI_SZ
) return;
157 /* allocate double the size, one for value before and one for after */
158 if (!(buffer
= HeapAlloc( GetProcessHeap(), 0, size
* 2 * sizeof(WCHAR
) ))) return;
159 if (RegQueryValueExW( hkey
, value
, NULL
, NULL
, (BYTE
*)buffer
, &size
)) goto done
;
164 int len
= strlenW(src
) + 1;
165 if (strcmpiW( src
, string
))
167 memcpy( dst
, src
, len
* sizeof(WCHAR
) );
173 if (dst
!= buffer
+ 2*size
) /* did we remove something? */
175 TRACE( "setting value %s to %s\n", debugstr_w(value
), debugstr_w(buffer
+ size
) );
176 RegSetValueExW( hkey
, value
, 0, REG_MULTI_SZ
,
177 (BYTE
*)(buffer
+ size
), dst
- (buffer
+ size
) );
180 HeapFree( GetProcessHeap(), 0, buffer
);
184 /***********************************************************************
187 * Perform an add/delete registry operation depending on the flags.
190 do_reg_operation(HANDLE KeyHandle
,
191 PUNICODE_STRING ValueName
,
195 WCHAR EmptyStr
= (WCHAR
)0;
199 if (Flags
& FLG_ADDREG_DELVAL
) /* deletion */
204 RegDeleteValueW( hkey
, value
);
208 RegDeleteKeyW( hkey
, NULL
);
214 if (Flags
& FLG_ADDREG_KEYONLY
)
218 if (Flags
& (FLG_ADDREG_NOCLOBBER
| FLG_ADDREG_OVERWRITEONLY
))
220 BOOL exists
= !RegQueryValueExW( hkey
, value
, NULL
, NULL
, NULL
, NULL
);
221 if (exists
&& (flags
& FLG_ADDREG_NOCLOBBER
))
223 if (!exists
& (flags
& FLG_ADDREG_OVERWRITEONLY
))
228 switch (Flags
& FLG_ADDREG_TYPE_MASK
)
230 case FLG_ADDREG_TYPE_SZ
:
234 case FLG_ADDREG_TYPE_MULTI_SZ
:
238 case FLG_ADDREG_TYPE_EXPAND_SZ
:
239 Type
= REG_EXPAND_SZ
;
242 case FLG_ADDREG_TYPE_BINARY
:
246 case FLG_ADDREG_TYPE_DWORD
:
250 case FLG_ADDREG_TYPE_NONE
:
259 if (!(Flags
& FLG_ADDREG_BINVALUETYPE
) ||
260 (Type
== REG_DWORD
&& InfGetFieldCount (Context
) == 5))
264 if (Type
== REG_MULTI_SZ
)
266 if (!InfGetMultiSzField (Context
, 5, NULL
, 0, &Size
))
271 Str
= RtlAllocateHeap (ProcessHeap
, 0, Size
* sizeof(WCHAR
));
275 InfGetMultiSzField (Context
, 5, Str
, Size
, NULL
);
278 if (Flags
& FLG_ADDREG_APPEND
)
283 // append_multi_sz_value( hkey, value, str, size );
285 RtlFreeHeap (ProcessHeap
, 0, Str
);
288 /* else fall through to normal string handling */
292 if (!InfGetStringField (Context
, 5, NULL
, 0, &Size
))
297 Str
= RtlAllocateHeap (ProcessHeap
, 0, Size
* sizeof(WCHAR
));
301 InfGetStringField (Context
, 5, Str
, Size
, NULL
);
305 if (Type
== REG_DWORD
)
307 ULONG dw
= Str
? wcstol (Str
, NULL
, 0) : 0;
309 DPRINT("setting dword %wZ to %lx\n", ValueName
, dw
);
311 NtSetValueKey (KeyHandle
,
320 DPRINT("setting value %wZ to %S\n", ValueName
, Str
);
324 NtSetValueKey (KeyHandle
,
329 Size
* sizeof(WCHAR
));
333 NtSetValueKey (KeyHandle
,
341 RtlFreeHeap (ProcessHeap
, 0, Str
);
343 else /* get the binary data */
347 if (!InfGetBinaryField (Context
, 5, NULL
, 0, &Size
))
352 Data
= RtlAllocateHeap (ProcessHeap
, 0, Size
);
356 DPRINT("setting binary data %wZ len %lu\n", ValueName
, Size
);
357 InfGetBinaryField (Context
, 5, Data
, Size
, NULL
);
360 NtSetValueKey (KeyHandle
,
367 RtlFreeHeap (ProcessHeap
, 0, Data
);
375 CreateNestedKey (PHANDLE KeyHandle
,
376 ACCESS_MASK DesiredAccess
,
377 POBJECT_ATTRIBUTES ObjectAttributes
)
379 OBJECT_ATTRIBUTES LocalObjectAttributes
;
380 UNICODE_STRING LocalKeyName
;
383 ULONG FullNameLength
;
386 HANDLE LocalKeyHandle
;
388 Status
= NtCreateKey (KeyHandle
,
395 DPRINT("NtCreateKey(%wZ) called (Status %lx)\n", ObjectAttributes
->ObjectName
, Status
);
396 if (Status
!= STATUS_OBJECT_NAME_NOT_FOUND
)
399 /* Copy object attributes */
400 RtlCopyMemory (&LocalObjectAttributes
,
402 sizeof(OBJECT_ATTRIBUTES
));
403 RtlCreateUnicodeString (&LocalKeyName
,
404 ObjectAttributes
->ObjectName
->Buffer
);
405 LocalObjectAttributes
.ObjectName
= &LocalKeyName
;
406 FullNameLength
= LocalKeyName
.Length
/ sizeof(WCHAR
);
408 /* Remove the last part of the key name and try to create the key again. */
409 while (Status
== STATUS_OBJECT_NAME_NOT_FOUND
)
411 Ptr
= wcsrchr (LocalKeyName
.Buffer
, '\\');
412 if (Ptr
== NULL
|| Ptr
== LocalKeyName
.Buffer
)
414 Status
= STATUS_UNSUCCESSFUL
;
418 LocalKeyName
.Length
= wcslen (LocalKeyName
.Buffer
) * sizeof(WCHAR
);
420 Status
= NtCreateKey (&LocalKeyHandle
,
422 &LocalObjectAttributes
,
427 DPRINT("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
430 if (!NT_SUCCESS(Status
))
432 RtlFreeUnicodeString (&LocalKeyName
);
436 /* Add removed parts of the key name and create them too. */
437 Length
= wcslen (LocalKeyName
.Buffer
);
440 if (Length
== FullNameLength
)
442 Status
= STATUS_SUCCESS
;
443 *KeyHandle
= LocalKeyHandle
;
446 NtClose (LocalKeyHandle
);
448 LocalKeyName
.Buffer
[Length
] = L
'\\';
449 Length
= wcslen (LocalKeyName
.Buffer
);
450 LocalKeyName
.Length
= Length
* sizeof(WCHAR
);
452 Status
= NtCreateKey (&LocalKeyHandle
,
454 &LocalObjectAttributes
,
459 DPRINT("NtCreateKey(%wZ) called (Status %lx)\n", &LocalKeyName
, Status
);
460 if (!NT_SUCCESS(Status
))
464 RtlFreeUnicodeString (&LocalKeyName
);
470 /***********************************************************************
473 * Called once for each AddReg and DelReg entry in a given section.
476 registry_callback (HINF hInf
, PCWSTR Section
, BOOLEAN Delete
)
478 OBJECT_ATTRIBUTES ObjectAttributes
;
479 WCHAR Buffer
[MAX_INF_STRING_LENGTH
];
481 UNICODE_STRING Value
;
482 PUNICODE_STRING ValuePtr
;
492 Ok
= InfFindFirstLine (hInf
, Section
, NULL
, &Context
);
496 for (;Ok
; Ok
= InfFindNextLine (Context
, Context
))
499 if (!InfGetStringField (Context
, 1, Buffer
, MAX_INF_STRING_LENGTH
, NULL
))
501 if (!GetRootKey (Buffer
))
505 Length
= wcslen (Buffer
);
506 if (!InfGetStringField (Context
, 2, Buffer
+ Length
, MAX_INF_STRING_LENGTH
- Length
, NULL
))
509 DPRINT("KeyName: <%S>\n", Buffer
);
512 if (!InfGetIntField (Context
, 4, (PLONG
)&Flags
))
515 DPRINT("Flags: %lx\n", Flags
);
517 RtlInitUnicodeString (&Name
,
520 InitializeObjectAttributes (&ObjectAttributes
,
522 OBJ_CASE_INSENSITIVE
,
526 if (Delete
|| (Flags
& FLG_ADDREG_OVERWRITEONLY
))
528 Status
= NtOpenKey (&KeyHandle
,
531 if (!NT_SUCCESS(Status
))
533 DPRINT("NtOpenKey(%wZ) failed (Status %lx)\n", &Name
, Status
);
534 continue; /* ignore if it doesn't exist */
539 Status
= CreateNestedKey (&KeyHandle
,
542 if (!NT_SUCCESS(Status
))
544 DPRINT("CreateNestedKey(%wZ) failed (Status %lx)\n", &Name
, Status
);
550 if (InfGetStringField (Context
, 3, Buffer
, MAX_INF_STRING_LENGTH
, NULL
))
552 RtlInitUnicodeString (&Value
,
562 if (!do_reg_operation (KeyHandle
, ValuePtr
, Context
, Flags
))
570 InfFreeContext(Context
);
578 ImportRegistryFile(PWSTR Filename
,
582 WCHAR FileNameBuffer
[MAX_PATH
];
583 UNICODE_STRING FileName
;
588 /* Load inf file from install media. */
589 wcscpy(FileNameBuffer
, SourceRootPath
.Buffer
);
590 wcscat(FileNameBuffer
, L
"\\reactos\\");
591 wcscat(FileNameBuffer
, Filename
);
593 RtlInitUnicodeString(&FileName
,
596 Status
= InfOpenFile(&hInf
,
599 if (!NT_SUCCESS(Status
))
601 DPRINT1("InfOpenFile() failed (Status %lx)\n", Status
);
605 if (!registry_callback (hInf
, L
"AddReg", FALSE
))
607 DPRINT1("registry_callback() failed\n");
617 SetInstallPathValue(PUNICODE_STRING InstallPath
)
619 OBJECT_ATTRIBUTES ObjectAttributes
;
620 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\HARDWARE");
621 UNICODE_STRING ValueName
= RTL_CONSTANT_STRING(L
"InstallPath");
625 /* Create the 'secret' InstallPath key */
626 InitializeObjectAttributes (&ObjectAttributes
,
628 OBJ_CASE_INSENSITIVE
,
631 Status
= NtOpenKey (&KeyHandle
,
634 if (!NT_SUCCESS(Status
))
636 DPRINT1("NtOpenKey() failed (Status %lx)\n", Status
);
640 Status
= NtSetValueKey (KeyHandle
,
644 (PVOID
)InstallPath
->Buffer
,
645 InstallPath
->Length
);
647 if (!NT_SUCCESS(Status
))
649 DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status
);
657 SetMountedDeviceValue(CHAR Letter
, ULONG Signature
, LARGE_INTEGER StartingOffset
)
659 OBJECT_ATTRIBUTES ObjectAttributes
;
660 WCHAR ValueNameBuffer
[16];
661 UNICODE_STRING KeyName
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\SYSTEM\\MountedDevices");
662 UNICODE_STRING ValueName
;
663 REG_DISK_MOUNT_INFO MountInfo
;
667 swprintf(ValueNameBuffer
, L
"\\DosDevices\\%C:", Letter
);
668 RtlInitUnicodeString(&ValueName
, ValueNameBuffer
);
670 InitializeObjectAttributes (&ObjectAttributes
,
672 OBJ_CASE_INSENSITIVE
,
675 Status
= NtOpenKey (&KeyHandle
,
678 if (!NT_SUCCESS(Status
))
680 Status
= NtCreateKey(&KeyHandle
,
685 REG_OPTION_NON_VOLATILE
,
689 if (!NT_SUCCESS(Status
))
691 DPRINT1("NtCreateKey() failed (Status %lx)\n", Status
);
695 MountInfo
.Signature
= Signature
;
696 MountInfo
.StartingOffset
= StartingOffset
;
697 Status
= NtSetValueKey (KeyHandle
,
704 if (!NT_SUCCESS(Status
))
706 DPRINT1("NtSetValueKey() failed (Status %lx)\n", Status
);