shdocvw: Move IConnectionPointContainer implementation to separated object.
[wine/testsucceed.git] / dlls / ole32 / regsvr.c
blobb4e3f942e93e20c8821a8298c2c01d1cbd61e55b
1 /*
2 * self-registerable dll functions for ole32.dll
4 * Copyright (C) 2003 John K. Hohm
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.
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.
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winreg.h"
30 #include "winerror.h"
31 #include "objbase.h"
33 #include "compobj_private.h"
34 #include "ole2.h"
35 #include "olectl.h"
36 #include "moniker.h"
38 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(ole);
43 * Near the bottom of this file are the exported DllRegisterServer and
44 * DllUnregisterServer, which make all this worthwhile.
47 /***********************************************************************
48 * interface for self-registering
50 struct regsvr_interface
52 IID const *iid; /* NULL for end of list */
53 LPCSTR name; /* can be NULL to omit */
54 IID const *base_iid; /* can be NULL to omit */
55 int num_methods; /* can be <0 to omit */
56 CLSID const *ps_clsid; /* can be NULL to omit */
57 CLSID const *ps_clsid32; /* can be NULL to omit */
60 static HRESULT register_interfaces(struct regsvr_interface const *list);
61 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
63 struct regsvr_coclass
65 CLSID const *clsid; /* NULL for end of list */
66 LPCSTR name; /* can be NULL to omit */
67 LPCSTR ips; /* can be NULL to omit */
68 LPCSTR ips32; /* can be NULL to omit */
69 LPCSTR ips32_tmodel; /* can be NULL to omit */
72 static HRESULT register_coclasses(struct regsvr_coclass const *list);
73 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
75 /***********************************************************************
76 * static string constants
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 char const tmodel_valuename[] = "ThreadingModel";
101 /***********************************************************************
102 * static helper functions
104 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
105 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
106 WCHAR const *value);
107 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
108 char const *value);
109 static LONG recursive_delete_key(HKEY key);
112 /***********************************************************************
113 * register_interfaces
115 static HRESULT register_interfaces(struct regsvr_interface const *list)
117 LONG res = ERROR_SUCCESS;
118 HKEY interface_key;
120 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
121 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
122 if (res != ERROR_SUCCESS) goto error_return;
124 for (; res == ERROR_SUCCESS && list->iid; ++list) {
125 WCHAR buf[39];
126 HKEY iid_key;
128 StringFromGUID2(list->iid, buf, 39);
129 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
130 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
131 if (res != ERROR_SUCCESS) goto error_close_interface_key;
133 if (list->name) {
134 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
135 (CONST BYTE*)(list->name),
136 strlen(list->name) + 1);
137 if (res != ERROR_SUCCESS) goto error_close_iid_key;
140 if (list->base_iid) {
141 res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
142 if (res != ERROR_SUCCESS) goto error_close_iid_key;
145 if (0 <= list->num_methods) {
146 static WCHAR const fmt[3] = { '%', 'd', 0 };
147 HKEY key;
149 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
150 KEY_READ | KEY_WRITE, NULL, &key, NULL);
151 if (res != ERROR_SUCCESS) goto error_close_iid_key;
153 wsprintfW(buf, fmt, list->num_methods);
154 res = RegSetValueExW(key, NULL, 0, REG_SZ,
155 (CONST BYTE*)buf,
156 (lstrlenW(buf) + 1) * sizeof(WCHAR));
157 RegCloseKey(key);
159 if (res != ERROR_SUCCESS) goto error_close_iid_key;
162 if (list->ps_clsid) {
163 register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
164 if (res != ERROR_SUCCESS) goto error_close_iid_key;
167 if (list->ps_clsid32) {
168 register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
169 if (res != ERROR_SUCCESS) goto error_close_iid_key;
172 error_close_iid_key:
173 RegCloseKey(iid_key);
176 error_close_interface_key:
177 RegCloseKey(interface_key);
178 error_return:
179 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
182 /***********************************************************************
183 * unregister_interfaces
185 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
187 LONG res = ERROR_SUCCESS;
188 HKEY interface_key;
190 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
191 KEY_READ | KEY_WRITE, &interface_key);
192 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
193 if (res != ERROR_SUCCESS) goto error_return;
195 for (; res == ERROR_SUCCESS && list->iid; ++list) {
196 WCHAR buf[39];
197 HKEY iid_key;
199 StringFromGUID2(list->iid, buf, 39);
200 res = RegOpenKeyExW(interface_key, buf, 0,
201 KEY_READ | KEY_WRITE, &iid_key);
202 if (res == ERROR_FILE_NOT_FOUND) {
203 res = ERROR_SUCCESS;
204 continue;
206 if (res != ERROR_SUCCESS) goto error_close_interface_key;
207 res = recursive_delete_key(iid_key);
208 RegCloseKey(iid_key);
209 if (res != ERROR_SUCCESS) goto error_close_interface_key;
212 error_close_interface_key:
213 RegCloseKey(interface_key);
214 error_return:
215 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
218 /***********************************************************************
219 * register_coclasses
221 static HRESULT register_coclasses(struct regsvr_coclass const *list)
223 LONG res = ERROR_SUCCESS;
224 HKEY coclass_key;
226 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
227 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
228 if (res != ERROR_SUCCESS) goto error_return;
230 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
231 WCHAR buf[39];
232 HKEY clsid_key;
234 StringFromGUID2(list->clsid, buf, 39);
235 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
236 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
237 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
239 if (list->name) {
240 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
241 (CONST BYTE*)(list->name),
242 strlen(list->name) + 1);
243 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
246 if (list->ips) {
247 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
248 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
251 if (list->ips32) {
252 HKEY ips32_key;
254 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
255 KEY_READ | KEY_WRITE, NULL,
256 &ips32_key, NULL);
257 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
259 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
260 (CONST BYTE*)list->ips32,
261 lstrlenA(list->ips32) + 1);
262 if (res == ERROR_SUCCESS && list->ips32_tmodel)
263 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
264 (CONST BYTE*)list->ips32_tmodel,
265 strlen(list->ips32_tmodel) + 1);
266 RegCloseKey(ips32_key);
267 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
270 error_close_clsid_key:
271 RegCloseKey(clsid_key);
274 error_close_coclass_key:
275 RegCloseKey(coclass_key);
276 error_return:
277 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
280 /***********************************************************************
281 * unregister_coclasses
283 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
285 LONG res = ERROR_SUCCESS;
286 HKEY coclass_key;
288 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
289 KEY_READ | KEY_WRITE, &coclass_key);
290 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
291 if (res != ERROR_SUCCESS) goto error_return;
293 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
294 WCHAR buf[39];
295 HKEY clsid_key;
297 StringFromGUID2(list->clsid, buf, 39);
298 res = RegOpenKeyExW(coclass_key, buf, 0,
299 KEY_READ | KEY_WRITE, &clsid_key);
300 if (res == ERROR_FILE_NOT_FOUND) {
301 res = ERROR_SUCCESS;
302 continue;
304 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
305 res = recursive_delete_key(clsid_key);
306 RegCloseKey(clsid_key);
307 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
310 error_close_coclass_key:
311 RegCloseKey(coclass_key);
312 error_return:
313 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
316 /***********************************************************************
317 * regsvr_key_guid
319 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
321 WCHAR buf[39];
323 StringFromGUID2(guid, buf, 39);
324 return register_key_defvalueW(base, name, buf);
327 /***********************************************************************
328 * regsvr_key_defvalueW
330 static LONG register_key_defvalueW(
331 HKEY base,
332 WCHAR const *name,
333 WCHAR const *value)
335 LONG res;
336 HKEY key;
338 res = RegCreateKeyExW(base, name, 0, NULL, 0,
339 KEY_READ | KEY_WRITE, NULL, &key, NULL);
340 if (res != ERROR_SUCCESS) return res;
341 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
342 (lstrlenW(value) + 1) * sizeof(WCHAR));
343 RegCloseKey(key);
344 return res;
347 /***********************************************************************
348 * regsvr_key_defvalueA
350 static LONG register_key_defvalueA(
351 HKEY base,
352 WCHAR const *name,
353 char const *value)
355 LONG res;
356 HKEY key;
358 res = RegCreateKeyExW(base, name, 0, NULL, 0,
359 KEY_READ | KEY_WRITE, NULL, &key, NULL);
360 if (res != ERROR_SUCCESS) return res;
361 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
362 lstrlenA(value) + 1);
363 RegCloseKey(key);
364 return res;
367 /***********************************************************************
368 * recursive_delete_key
370 static LONG recursive_delete_key(HKEY key)
372 LONG res;
373 WCHAR subkey_name[MAX_PATH];
374 DWORD cName;
375 HKEY subkey;
377 for (;;) {
378 cName = sizeof(subkey_name) / sizeof(WCHAR);
379 res = RegEnumKeyExW(key, 0, subkey_name, &cName,
380 NULL, NULL, NULL, NULL);
381 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) {
382 res = ERROR_SUCCESS; /* presumably we're done enumerating */
383 break;
385 res = RegOpenKeyExW(key, subkey_name, 0,
386 KEY_READ | KEY_WRITE, &subkey);
387 if (res == ERROR_FILE_NOT_FOUND) continue;
388 if (res != ERROR_SUCCESS) break;
390 res = recursive_delete_key(subkey);
391 RegCloseKey(subkey);
392 if (res != ERROR_SUCCESS) break;
395 if (res == ERROR_SUCCESS) res = RegDeleteKeyW(key, 0);
396 return res;
399 /***********************************************************************
400 * coclass list
402 static GUID const CLSID_PointerMoniker = {
403 0x00000306, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
405 static GUID const CLSID_PackagerMoniker = {
406 0x00000308, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
408 static struct regsvr_coclass const coclass_list[] = {
409 { &CLSID_FileMoniker,
410 "FileMoniker",
411 NULL,
412 "ole32.dll",
413 "Both"
415 { &CLSID_ItemMoniker,
416 "ItemMoniker",
417 NULL,
418 "ole32.dll",
419 "Both"
421 { &CLSID_AntiMoniker,
422 "AntiMoniker",
423 NULL,
424 "ole32.dll",
425 "Both"
427 { &CLSID_PointerMoniker,
428 "PointerMoniker",
429 NULL,
430 "ole32.dll",
431 "Both"
433 { &CLSID_PackagerMoniker,
434 "PackagerMoniker",
435 NULL,
436 "ole32.dll",
437 "Both"
439 { &CLSID_CompositeMoniker,
440 "CompositeMoniker",
441 NULL,
442 "ole32.dll",
443 "Both"
445 { &CLSID_DfMarshal,
446 "DfMarshal",
447 NULL,
448 "ole32.dll",
449 "Both"
451 { &CLSID_ClassMoniker,
452 "ClassMoniker",
453 NULL,
454 "ole32.dll",
455 "Both"
457 { &CLSID_PSFactoryBuffer,
458 "PSFactoryBuffer",
459 NULL,
460 "ole32.dll",
461 "Both"
463 { &CLSID_StdGlobalInterfaceTable,
464 "StdGlobalInterfaceTable",
465 NULL,
466 "ole32.dll",
467 "Apartment"
469 { NULL } /* list terminator */
472 /***********************************************************************
473 * interface list
476 #define INTERFACE_ENTRY(interface, base, clsid32, clsid16) { &IID_##interface, #interface, base, sizeof(interface##Vtbl)/sizeof(void*), clsid16, clsid32 }
477 #define STD_INTERFACE_ENTRY(interface) INTERFACE_ENTRY(interface, NULL, &CLSID_PSFactoryBuffer, NULL)
478 #define LCL_INTERFACE_ENTRY(interface) INTERFACE_ENTRY(interface, NULL, NULL, NULL)
480 static const struct regsvr_interface interface_list[] = {
481 LCL_INTERFACE_ENTRY(IUnknown),
482 STD_INTERFACE_ENTRY(IClassFactory),
483 LCL_INTERFACE_ENTRY(IMalloc),
484 LCL_INTERFACE_ENTRY(IMarshal),
485 STD_INTERFACE_ENTRY(IStorage),
486 LCL_INTERFACE_ENTRY(IMessageFilter),
487 LCL_INTERFACE_ENTRY(IStdMarshalInfo),
488 LCL_INTERFACE_ENTRY(IExternalConnection),
489 LCL_INTERFACE_ENTRY(IMallocSpy),
490 LCL_INTERFACE_ENTRY(IMultiQI),
491 STD_INTERFACE_ENTRY(IStream),
492 STD_INTERFACE_ENTRY(IPersistStorage),
493 STD_INTERFACE_ENTRY(IDataObject),
494 STD_INTERFACE_ENTRY(IAdviseSink),
495 LCL_INTERFACE_ENTRY(IDataAdviseHolder),
496 LCL_INTERFACE_ENTRY(IOleAdviseHolder),
497 STD_INTERFACE_ENTRY(IOleObject),
498 STD_INTERFACE_ENTRY(IOleClientSite),
499 LCL_INTERFACE_ENTRY(IDropSource),
500 STD_INTERFACE_ENTRY(IRemUnknown),
501 LCL_INTERFACE_ENTRY(IClientSecurity),
502 LCL_INTERFACE_ENTRY(IServerSecurity),
503 { NULL } /* list terminator */
506 /***********************************************************************
507 * DllRegisterServer (OLE32.@)
509 HRESULT WINAPI DllRegisterServer()
511 HRESULT hr;
513 TRACE("\n");
515 hr = register_coclasses(coclass_list);
516 if (SUCCEEDED(hr))
517 hr = register_interfaces(interface_list);
518 return hr;
521 /***********************************************************************
522 * DllUnregisterServer (OLE32.@)
524 HRESULT WINAPI DllUnregisterServer()
526 HRESULT hr;
528 TRACE("\n");
530 hr = unregister_coclasses(coclass_list);
531 if (SUCCEEDED(hr))
532 hr = unregister_interfaces(interface_list);
533 return hr;