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