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