a277d071f40291cb48ba5a65b55770ed71a8196e
[reactos.git] / reactos / lib / userenv / registry.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2004 ReactOS Team
4 *
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.
9 *
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.
14 *
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.
18 */
19 /* $Id$
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * FILE: lib/userenv/registry.c
24 * PURPOSE: User profile code
25 * PROGRAMMER: Eric Kohl
26 */
27
28 #include <precomp.h>
29
30 #define NDEBUG
31 #include <debug.h>
32
33
34 /* FUNCTIONS ***************************************************************/
35
36 static BOOL
37 CopyKey (HKEY hDstKey,
38 HKEY hSrcKey)
39 {
40 FILETIME LastWrite;
41 DWORD dwSubKeys;
42 DWORD dwValues;
43 DWORD dwType;
44 DWORD dwMaxSubKeyNameLength;
45 DWORD dwSubKeyNameLength;
46 DWORD dwMaxValueNameLength;
47 DWORD dwValueNameLength;
48 DWORD dwMaxValueLength;
49 DWORD dwValueLength;
50 DWORD dwDisposition;
51 DWORD i;
52 LPWSTR lpNameBuffer;
53 LPBYTE lpDataBuffer;
54 HKEY hDstSubKey;
55 HKEY hSrcSubKey;
56
57 DPRINT ("CopyKey() called \n");
58
59 if (RegQueryInfoKey (hSrcKey,
60 NULL,
61 NULL,
62 NULL,
63 &dwSubKeys,
64 &dwMaxSubKeyNameLength,
65 NULL,
66 &dwValues,
67 &dwMaxValueNameLength,
68 &dwMaxValueLength,
69 NULL,
70 NULL))
71 {
72 DPRINT1 ("RegQueryInfoKey() failed (Error %lu)\n", GetLastError ());
73 return FALSE;
74 }
75
76 DPRINT ("dwSubKeys %lu\n", dwSubKeys);
77 DPRINT ("dwMaxSubKeyNameLength %lu\n", dwMaxSubKeyNameLength);
78 DPRINT ("dwValues %lu\n", dwValues);
79 DPRINT ("dwMaxValueNameLength %lu\n", dwMaxValueNameLength);
80 DPRINT ("dwMaxValueLength %lu\n", dwMaxValueLength);
81
82 /* Copy subkeys */
83 if (dwSubKeys != 0)
84 {
85 lpNameBuffer = HeapAlloc (GetProcessHeap (),
86 0,
87 dwMaxSubKeyNameLength * sizeof(WCHAR));
88 if (lpNameBuffer == NULL)
89 {
90 DPRINT1("Buffer allocation failed\n");
91 return FALSE;
92 }
93
94 for (i = 0; i < dwSubKeys; i++)
95 {
96 dwSubKeyNameLength = dwMaxSubKeyNameLength;
97 if (RegEnumKeyExW (hSrcKey,
98 i,
99 lpNameBuffer,
100 &dwSubKeyNameLength,
101 NULL,
102 NULL,
103 NULL,
104 &LastWrite))
105 {
106 DPRINT1 ("Subkey enumeration failed (Error %lu)\n", GetLastError());
107 HeapFree (GetProcessHeap (),
108 0,
109 lpNameBuffer);
110 return FALSE;
111 }
112
113 if (RegCreateKeyExW (hDstKey,
114 lpNameBuffer,
115 0,
116 NULL,
117 REG_OPTION_NON_VOLATILE,
118 KEY_WRITE,
119 NULL,
120 &hDstSubKey,
121 &dwDisposition))
122 {
123 DPRINT1 ("Subkey creation failed (Error %lu)\n", GetLastError());
124 HeapFree (GetProcessHeap (),
125 0,
126 lpNameBuffer);
127 return FALSE;
128 }
129
130 if (RegOpenKeyExW (hSrcKey,
131 lpNameBuffer,
132 0,
133 KEY_READ,
134 &hSrcSubKey))
135 {
136 DPRINT1 ("Error: %lu\n", GetLastError());
137 RegCloseKey (hDstSubKey);
138 HeapFree (GetProcessHeap (),
139 0,
140 lpNameBuffer);
141 return FALSE;
142 }
143
144 if (!CopyKey (hDstSubKey,
145 hSrcSubKey))
146 {
147 DPRINT1 ("Error: %lu\n", GetLastError());
148 RegCloseKey (hSrcSubKey);
149 RegCloseKey (hDstSubKey);
150 HeapFree (GetProcessHeap (),
151 0,
152 lpNameBuffer);
153 return FALSE;
154 }
155
156 RegCloseKey (hSrcSubKey);
157 RegCloseKey (hDstSubKey);
158 }
159
160 HeapFree (GetProcessHeap (),
161 0,
162 lpNameBuffer);
163 }
164
165 /* Copy values */
166 if (dwValues != 0)
167 {
168 lpNameBuffer = HeapAlloc (GetProcessHeap (),
169 0,
170 dwMaxValueNameLength * sizeof(WCHAR));
171 if (lpNameBuffer == NULL)
172 {
173 DPRINT1 ("Buffer allocation failed\n");
174 return FALSE;
175 }
176
177 lpDataBuffer = HeapAlloc (GetProcessHeap (),
178 0,
179 dwMaxValueLength);
180 if (lpDataBuffer == NULL)
181 {
182 DPRINT1 ("Buffer allocation failed\n");
183 HeapFree (GetProcessHeap (),
184 0,
185 lpNameBuffer);
186 return FALSE;
187 }
188
189 for (i = 0; i < dwValues; i++)
190 {
191 dwValueNameLength = dwMaxValueNameLength;
192 dwValueLength = dwMaxValueLength;
193 if (RegEnumValueW (hSrcKey,
194 i,
195 lpNameBuffer,
196 &dwValueNameLength,
197 NULL,
198 &dwType,
199 lpDataBuffer,
200 &dwValueLength))
201 {
202 DPRINT1("Error: %lu\n", GetLastError());
203 HeapFree (GetProcessHeap (),
204 0,
205 lpDataBuffer);
206 HeapFree (GetProcessHeap (),
207 0,
208 lpNameBuffer);
209 return FALSE;
210 }
211
212 if (RegSetValueExW (hDstKey,
213 lpNameBuffer,
214 0,
215 dwType,
216 lpDataBuffer,
217 dwValueLength))
218 {
219 DPRINT1("Error: %lu\n", GetLastError());
220 HeapFree (GetProcessHeap (),
221 0,
222 lpDataBuffer);
223 HeapFree (GetProcessHeap (),
224 0,
225 lpNameBuffer);
226 return FALSE;
227 }
228 }
229
230 HeapFree (GetProcessHeap (),
231 0,
232 lpDataBuffer);
233
234 HeapFree (GetProcessHeap (),
235 0,
236 lpNameBuffer);
237 }
238
239 DPRINT ("CopyKey() done \n");
240
241 return TRUE;
242 }
243
244
245 BOOL
246 CreateUserHive (LPCWSTR lpKeyName,
247 LPCWSTR lpProfilePath)
248 {
249 HKEY hDefaultKey;
250 HKEY hUserKey;
251
252 DPRINT ("CreateUserHive(%S) called\n", lpKeyName);
253
254 if (RegOpenKeyExW (HKEY_USERS,
255 L".Default",
256 0,
257 KEY_READ,
258 &hDefaultKey))
259 {
260 DPRINT1 ("Error: %lu\n", GetLastError());
261 return FALSE;
262 }
263
264 if (RegOpenKeyExW (HKEY_USERS,
265 lpKeyName,
266 0,
267 KEY_ALL_ACCESS,
268 &hUserKey))
269 {
270 DPRINT1 ("Error: %lu\n", GetLastError());
271 RegCloseKey (hDefaultKey);
272 return FALSE;
273 }
274
275 if (!CopyKey(hUserKey, hDefaultKey))
276 {
277 DPRINT1 ("Error: %lu\n", GetLastError());
278 RegCloseKey (hUserKey);
279 RegCloseKey (hDefaultKey);
280 return FALSE;
281 }
282
283 if (!UpdateUsersShellFolderSettings(lpProfilePath,
284 hUserKey))
285 {
286 DPRINT1("Error: %lu\n", GetLastError());
287 RegCloseKey (hUserKey);
288 RegCloseKey (hDefaultKey);
289 return FALSE;
290 }
291
292 RegFlushKey (hUserKey);
293
294 RegCloseKey (hUserKey);
295 RegCloseKey (hDefaultKey);
296
297 DPRINT ("CreateUserHive() done\n");
298
299 return TRUE;
300 }
301
302 /* EOF */