Sync to Wine-20050419:
[reactos.git] / reactos / lib / shell32 / regsvr.c
1 /*
2 * self-registerable dll functions for shell32.dll
3 *
4 * Copyright (C) 2003 John K. Hohm
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winuser.h"
27 #include "winreg.h"
28 #include "winerror.h"
29
30 #include "ole2.h"
31 #include "shlguid.h"
32 #include "shell32_main.h"
33
34 #include "wine/debug.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(ole);
37
38 /*
39 * Near the bottom of this file are the exported DllRegisterServer and
40 * DllUnregisterServer, which make all this worthwhile.
41 */
42
43 /***********************************************************************
44 * interface for self-registering
45 */
46 struct regsvr_interface
47 {
48 IID const *iid; /* NULL for end of list */
49 LPCSTR name; /* can be NULL to omit */
50 IID const *base_iid; /* can be NULL to omit */
51 int num_methods; /* can be <0 to omit */
52 CLSID const *ps_clsid; /* can be NULL to omit */
53 CLSID const *ps_clsid32; /* can be NULL to omit */
54 };
55
56 static HRESULT register_interfaces(struct regsvr_interface const *list);
57 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
58
59 struct regsvr_coclass
60 {
61 CLSID const *clsid; /* NULL for end of list */
62 LPCSTR name; /* can be NULL to omit */
63 LPCSTR ips; /* can be NULL to omit */
64 LPCSTR ips32; /* can be NULL to omit */
65 LPCSTR ips32_tmodel; /* can be NULL to omit */
66 DWORD flags;
67 LPCSTR clsid_str; /* can be NULL to omit */
68 LPCSTR progid; /* can be NULL to omit */
69 };
70
71 /* flags for regsvr_coclass.flags */
72 #define SHELLEX_MAYCHANGEDEFAULTMENU 0x00000001
73
74 static HRESULT register_coclasses(struct regsvr_coclass const *list);
75 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
76
77 /***********************************************************************
78 * static string constants
79 */
80 static WCHAR const interface_keyname[10] = {
81 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
82 static WCHAR const base_ifa_keyname[14] = {
83 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
84 'e', 0 };
85 static WCHAR const num_methods_keyname[11] = {
86 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
87 static WCHAR const ps_clsid_keyname[15] = {
88 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
89 'i', 'd', 0 };
90 static WCHAR const ps_clsid32_keyname[17] = {
91 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
92 'i', 'd', '3', '2', 0 };
93 static WCHAR const clsid_keyname[6] = {
94 'C', 'L', 'S', 'I', 'D', 0 };
95 static WCHAR const ips_keyname[13] = {
96 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
97 0 };
98 static WCHAR const ips32_keyname[15] = {
99 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
100 '3', '2', 0 };
101 static WCHAR const progid_keyname[7] = {
102 'P', 'r', 'o', 'g', 'I', 'D', 0 };
103 static WCHAR const shellex_keyname[8] = {
104 's', 'h', 'e', 'l', 'l', 'e', 'x', 0 };
105 static WCHAR const mcdm_keyname[21] = {
106 'M', 'a', 'y', 'C', 'h', 'a', 'n', 'g', 'e', 'D', 'e', 'f',
107 'a', 'u', 'l', 't', 'M', 'e', 'n', 'u', 0 };
108 static char const tmodel_valuename[] = "ThreadingModel";
109
110 /***********************************************************************
111 * static helper functions
112 */
113 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
114 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
115 WCHAR const *value);
116 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
117 char const *value);
118 static LONG recursive_delete_key(HKEY key);
119 static LONG recursive_delete_keyA(HKEY base, char const *name);
120 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name);
121
122 /***********************************************************************
123 * register_interfaces
124 */
125 static HRESULT register_interfaces(struct regsvr_interface const *list)
126 {
127 LONG res = ERROR_SUCCESS;
128 HKEY interface_key;
129
130 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
131 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
132 if (res != ERROR_SUCCESS) goto error_return;
133
134 for (; res == ERROR_SUCCESS && list->iid; ++list) {
135 WCHAR buf[39];
136 HKEY iid_key;
137
138 StringFromGUID2(list->iid, buf, 39);
139 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
140 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
141 if (res != ERROR_SUCCESS) goto error_close_interface_key;
142
143 if (list->name) {
144 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
145 (CONST BYTE*)(list->name),
146 strlen(list->name) + 1);
147 if (res != ERROR_SUCCESS) goto error_close_iid_key;
148 }
149
150 if (list->base_iid) {
151 register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
152 if (res != ERROR_SUCCESS) goto error_close_iid_key;
153 }
154
155 if (0 <= list->num_methods) {
156 static WCHAR const fmt[3] = { '%', 'd', 0 };
157 HKEY key;
158
159 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
160 KEY_READ | KEY_WRITE, NULL, &key, NULL);
161 if (res != ERROR_SUCCESS) goto error_close_iid_key;
162
163 wsprintfW(buf, fmt, list->num_methods);
164 res = RegSetValueExW(key, NULL, 0, REG_SZ,
165 (CONST BYTE*)buf,
166 (lstrlenW(buf) + 1) * sizeof(WCHAR));
167 RegCloseKey(key);
168
169 if (res != ERROR_SUCCESS) goto error_close_iid_key;
170 }
171
172 if (list->ps_clsid) {
173 register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
174 if (res != ERROR_SUCCESS) goto error_close_iid_key;
175 }
176
177 if (list->ps_clsid32) {
178 register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
179 if (res != ERROR_SUCCESS) goto error_close_iid_key;
180 }
181
182 error_close_iid_key:
183 RegCloseKey(iid_key);
184 }
185
186 error_close_interface_key:
187 RegCloseKey(interface_key);
188 error_return:
189 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
190 }
191
192 /***********************************************************************
193 * unregister_interfaces
194 */
195 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
196 {
197 LONG res = ERROR_SUCCESS;
198 HKEY interface_key;
199
200 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
201 KEY_READ | KEY_WRITE, &interface_key);
202 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
203 if (res != ERROR_SUCCESS) goto error_return;
204
205 for (; res == ERROR_SUCCESS && list->iid; ++list) {
206 WCHAR buf[39];
207
208 StringFromGUID2(list->iid, buf, 39);
209 res = recursive_delete_keyW(interface_key, buf);
210 }
211
212 RegCloseKey(interface_key);
213 error_return:
214 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
215 }
216
217 /***********************************************************************
218 * register_coclasses
219 */
220 static HRESULT register_coclasses(struct regsvr_coclass const *list)
221 {
222 LONG res = ERROR_SUCCESS;
223 HKEY coclass_key;
224
225 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
226 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
227 if (res != ERROR_SUCCESS) goto error_return;
228
229 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
230 WCHAR buf[39];
231 HKEY clsid_key;
232
233 StringFromGUID2(list->clsid, buf, 39);
234 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
235 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
236 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
237
238 if (list->name) {
239 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
240 (CONST BYTE*)(list->name),
241 strlen(list->name) + 1);
242 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
243 }
244
245 if (list->ips) {
246 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
247 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
248 }
249
250 if (list->ips32) {
251 HKEY ips32_key;
252
253 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
254 KEY_READ | KEY_WRITE, NULL,
255 &ips32_key, NULL);
256 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
257
258 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
259 (CONST BYTE*)list->ips32,
260 lstrlenA(list->ips32) + 1);
261 if (res == ERROR_SUCCESS && list->ips32_tmodel)
262 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
263 (CONST BYTE*)list->ips32_tmodel,
264 strlen(list->ips32_tmodel) + 1);
265 RegCloseKey(ips32_key);
266 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
267 }
268
269 if (list->flags & SHELLEX_MAYCHANGEDEFAULTMENU) {
270 HKEY shellex_key, mcdm_key;
271
272 res = RegCreateKeyExW(clsid_key, shellex_keyname, 0, NULL, 0,
273 KEY_READ | KEY_WRITE, NULL,
274 &shellex_key, NULL);
275 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
276 res = RegCreateKeyExW(shellex_key, mcdm_keyname, 0, NULL, 0,
277 KEY_READ | KEY_WRITE, NULL,
278 &mcdm_key, NULL);
279 RegCloseKey(shellex_key);
280 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
281 RegCloseKey(mcdm_key);
282 }
283
284 if (list->clsid_str) {
285 res = register_key_defvalueA(clsid_key, clsid_keyname,
286 list->clsid_str);
287 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
288 }
289
290 if (list->progid) {
291 HKEY progid_key;
292
293 res = register_key_defvalueA(clsid_key, progid_keyname,
294 list->progid);
295 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
296
297 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->progid, 0,
298 NULL, 0, KEY_READ | KEY_WRITE, NULL,
299 &progid_key, NULL);
300 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
301
302 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
303 RegCloseKey(progid_key);
304 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
305 }
306
307 error_close_clsid_key:
308 RegCloseKey(clsid_key);
309 }
310
311 error_close_coclass_key:
312 RegCloseKey(coclass_key);
313 error_return:
314 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
315 }
316
317 /***********************************************************************
318 * unregister_coclasses
319 */
320 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
321 {
322 LONG res = ERROR_SUCCESS;
323 HKEY coclass_key;
324
325 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
326 KEY_READ | KEY_WRITE, &coclass_key);
327 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
328 if (res != ERROR_SUCCESS) goto error_return;
329
330 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
331 WCHAR buf[39];
332
333 StringFromGUID2(list->clsid, buf, 39);
334 res = recursive_delete_keyW(coclass_key, buf);
335 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
336
337 if (list->progid) {
338 res = recursive_delete_keyA(HKEY_CLASSES_ROOT, list->progid);
339 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
340 }
341 }
342
343 error_close_coclass_key:
344 RegCloseKey(coclass_key);
345 error_return:
346 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
347 }
348
349 /***********************************************************************
350 * regsvr_key_guid
351 */
352 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
353 {
354 WCHAR buf[39];
355
356 StringFromGUID2(guid, buf, 39);
357 return register_key_defvalueW(base, name, buf);
358 }
359
360 /***********************************************************************
361 * regsvr_key_defvalueW
362 */
363 static LONG register_key_defvalueW(
364 HKEY base,
365 WCHAR const *name,
366 WCHAR const *value)
367 {
368 LONG res;
369 HKEY key;
370
371 res = RegCreateKeyExW(base, name, 0, NULL, 0,
372 KEY_READ | KEY_WRITE, NULL, &key, NULL);
373 if (res != ERROR_SUCCESS) return res;
374 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
375 (lstrlenW(value) + 1) * sizeof(WCHAR));
376 RegCloseKey(key);
377 return res;
378 }
379
380 /***********************************************************************
381 * regsvr_key_defvalueA
382 */
383 static LONG register_key_defvalueA(
384 HKEY base,
385 WCHAR const *name,
386 char const *value)
387 {
388 LONG res;
389 HKEY key;
390
391 res = RegCreateKeyExW(base, name, 0, NULL, 0,
392 KEY_READ | KEY_WRITE, NULL, &key, NULL);
393 if (res != ERROR_SUCCESS) return res;
394 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
395 lstrlenA(value) + 1);
396 RegCloseKey(key);
397 return res;
398 }
399
400 /***********************************************************************
401 * recursive_delete_key
402 */
403 static LONG recursive_delete_key(HKEY key)
404 {
405 LONG res;
406 WCHAR subkey_name[MAX_PATH];
407 DWORD cName;
408 HKEY subkey;
409
410 for (;;) {
411 cName = sizeof(subkey_name) / sizeof(WCHAR);
412 res = RegEnumKeyExW(key, 0, subkey_name, &cName,
413 NULL, NULL, NULL, NULL);
414 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) {
415 res = ERROR_SUCCESS; /* presumably we're done enumerating */
416 break;
417 }
418 res = RegOpenKeyExW(key, subkey_name, 0,
419 KEY_READ | KEY_WRITE, &subkey);
420 if (res == ERROR_FILE_NOT_FOUND) continue;
421 if (res != ERROR_SUCCESS) break;
422
423 res = recursive_delete_key(subkey);
424 RegCloseKey(subkey);
425 if (res != ERROR_SUCCESS) break;
426 }
427
428 if (res == ERROR_SUCCESS) res = RegDeleteKeyW(key, 0);
429 return res;
430 }
431
432 /***********************************************************************
433 * recursive_delete_keyA
434 */
435 static LONG recursive_delete_keyA(HKEY base, char const *name)
436 {
437 LONG res;
438 HKEY key;
439
440 res = RegOpenKeyExA(base, name, 0, KEY_READ | KEY_WRITE, &key);
441 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
442 if (res != ERROR_SUCCESS) return res;
443 res = recursive_delete_key(key);
444 RegCloseKey(key);
445 return res;
446 }
447
448 /***********************************************************************
449 * recursive_delete_keyW
450 */
451 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name)
452 {
453 LONG res;
454 HKEY key;
455
456 res = RegOpenKeyExW(base, name, 0, KEY_READ | KEY_WRITE, &key);
457 if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
458 if (res != ERROR_SUCCESS) return res;
459 res = recursive_delete_key(key);
460 RegCloseKey(key);
461 return res;
462 }
463
464 /***********************************************************************
465 * coclass list
466 */
467 static GUID const CLSID_Desktop = {
468 0x00021400, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
469
470 static GUID const CLSID_Shortcut = {
471 0x00021401, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
472
473 static struct regsvr_coclass const coclass_list[] = {
474 { &CLSID_Desktop,
475 "Desktop",
476 NULL,
477 "shell32.dll",
478 "Apartment"
479 },
480 { &CLSID_DragDropHelper,
481 "Shell Drag and Drop Helper",
482 NULL,
483 "shell32.dll",
484 "Apartment"
485 },
486 { &CLSID_MyComputer,
487 "My Computer",
488 NULL,
489 "shell32.dll",
490 "Apartment"
491 },
492 { &CLSID_Shortcut,
493 "Shortcut",
494 NULL,
495 "shell32.dll",
496 "Apartment",
497 SHELLEX_MAYCHANGEDEFAULTMENU
498 },
499 { &CLSID_AutoComplete,
500 "AutoComplete",
501 NULL,
502 "shell32.dll",
503 "Apartment",
504 },
505 { NULL } /* list terminator */
506 };
507
508 /***********************************************************************
509 * interface list
510 */
511
512 static struct regsvr_interface const interface_list[] = {
513 { NULL } /* list terminator */
514 };
515
516 /***********************************************************************
517 * DllRegisterServer (SHELL32.@)
518 */
519 HRESULT WINAPI SHELL32_DllRegisterServer()
520 {
521 HRESULT hr;
522
523 TRACE("\n");
524
525 hr = register_coclasses(coclass_list);
526 if (SUCCEEDED(hr))
527 hr = register_interfaces(interface_list);
528 if (SUCCEEDED(hr))
529 hr = SHELL_RegisterShellFolders();
530 return hr;
531 }
532
533 /***********************************************************************
534 * DllUnregisterServer (SHELL32.@)
535 */
536 HRESULT WINAPI SHELL32_DllUnregisterServer()
537 {
538 HRESULT hr;
539
540 TRACE("\n");
541
542 hr = unregister_coclasses(coclass_list);
543 if (SUCCEEDED(hr))
544 hr = unregister_interfaces(interface_list);
545 return hr;
546 }