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