Release 1.3.7.
[wine/gsoc-2012-control.git] / dlls / oleaut32 / regsvr.c
blobf97544f51ed1239f94062e866b1ce83ed7e3bd38
1 /*
2 * self-registerable dll functions for oleaut32.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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <string.h>
24 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "winerror.h"
31 #include "ole2.h"
32 #include "olectl.h"
33 #include "oleauto.h"
34 #include "initguid.h"
35 #include "typelib.h"
36 #include "wincodec.h"
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
44 * Near the bottom of this file are the exported DllRegisterServer and
45 * DllUnregisterServer, which make all this worthwhile.
48 /***********************************************************************
49 * interface for self-registering
51 struct regsvr_interface
53 IID const *iid; /* NULL for end of list */
54 LPCSTR name; /* can be NULL to omit */
55 IID const *base_iid; /* can be NULL to omit */
56 int num_methods; /* can be <0 to omit */
57 CLSID const *ps_clsid; /* can be NULL to omit */
58 CLSID const *ps_clsid32; /* can be NULL to omit */
61 static HRESULT register_interfaces(struct regsvr_interface const *list);
62 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
64 struct regsvr_coclass
66 CLSID const *clsid; /* NULL for end of list */
67 LPCSTR name; /* can be NULL to omit */
68 LPCSTR ips; /* can be NULL to omit */
69 LPCSTR ips32; /* can be NULL to omit */
70 LPCSTR ips32_tmodel; /* can be NULL to omit */
71 LPCSTR clsid_str; /* can be NULL to omit */
72 LPCSTR progid; /* can be NULL to omit */
75 static HRESULT register_coclasses(struct regsvr_coclass const *list);
76 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
78 /***********************************************************************
79 * static string constants
81 static WCHAR const interface_keyname[10] = {
82 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
83 static WCHAR const base_ifa_keyname[14] = {
84 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
85 'e', 0 };
86 static WCHAR const num_methods_keyname[11] = {
87 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
88 static WCHAR const ps_clsid_keyname[15] = {
89 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
90 'i', 'd', 0 };
91 static WCHAR const ps_clsid32_keyname[17] = {
92 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
93 'i', 'd', '3', '2', 0 };
94 static WCHAR const clsid_keyname[6] = {
95 'C', 'L', 'S', 'I', 'D', 0 };
96 static WCHAR const ips_keyname[13] = {
97 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
98 0 };
99 static WCHAR const ips32_keyname[15] = {
100 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
101 '3', '2', 0 };
102 static WCHAR const progid_keyname[7] = {
103 'P', 'r', 'o', 'g', 'I', 'D', 0 };
104 static char const tmodel_valuename[] = "ThreadingModel";
106 /***********************************************************************
107 * static helper functions
109 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
110 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
111 WCHAR const *value);
112 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
113 char const *value);
115 /***********************************************************************
116 * register_interfaces
118 static HRESULT register_interfaces(struct regsvr_interface const *list)
120 LONG res = ERROR_SUCCESS;
121 HKEY interface_key;
123 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
124 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
125 if (res != ERROR_SUCCESS) goto error_return;
127 for (; res == ERROR_SUCCESS && list->iid; ++list) {
128 WCHAR buf[39];
129 HKEY iid_key;
131 StringFromGUID2(list->iid, buf, 39);
132 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
133 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
134 if (res != ERROR_SUCCESS) goto error_close_interface_key;
136 if (list->name) {
137 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
138 (CONST BYTE*)(list->name),
139 strlen(list->name) + 1);
140 if (res != ERROR_SUCCESS) goto error_close_iid_key;
143 if (list->base_iid) {
144 res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
145 if (res != ERROR_SUCCESS) goto error_close_iid_key;
148 if (0 <= list->num_methods) {
149 static WCHAR const fmt[3] = { '%', 'd', 0 };
150 HKEY key;
152 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
153 KEY_READ | KEY_WRITE, NULL, &key, NULL);
154 if (res != ERROR_SUCCESS) goto error_close_iid_key;
156 sprintfW(buf, fmt, list->num_methods);
157 res = RegSetValueExW(key, NULL, 0, REG_SZ,
158 (CONST BYTE*)buf,
159 (lstrlenW(buf) + 1) * sizeof(WCHAR));
160 RegCloseKey(key);
162 if (res != ERROR_SUCCESS) goto error_close_iid_key;
165 if (list->ps_clsid) {
166 res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
167 if (res != ERROR_SUCCESS) goto error_close_iid_key;
170 if (list->ps_clsid32) {
171 res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
172 if (res != ERROR_SUCCESS) goto error_close_iid_key;
175 error_close_iid_key:
176 RegCloseKey(iid_key);
179 error_close_interface_key:
180 RegCloseKey(interface_key);
181 error_return:
182 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
185 /***********************************************************************
186 * unregister_interfaces
188 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
190 LONG res = ERROR_SUCCESS;
191 HKEY interface_key;
193 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
194 KEY_READ | KEY_WRITE, &interface_key);
195 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
196 if (res != ERROR_SUCCESS) goto error_return;
198 for (; res == ERROR_SUCCESS && list->iid; ++list) {
199 WCHAR buf[39];
201 StringFromGUID2(list->iid, buf, 39);
202 res = RegDeleteTreeW(interface_key, buf);
203 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
206 RegCloseKey(interface_key);
207 error_return:
208 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
211 /***********************************************************************
212 * register_coclasses
214 static HRESULT register_coclasses(struct regsvr_coclass const *list)
216 LONG res = ERROR_SUCCESS;
217 HKEY coclass_key;
219 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
220 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
221 if (res != ERROR_SUCCESS) goto error_return;
223 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
224 WCHAR buf[39];
225 HKEY clsid_key;
227 StringFromGUID2(list->clsid, buf, 39);
228 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
229 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
230 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
232 if (list->name) {
233 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
234 (CONST BYTE*)(list->name),
235 strlen(list->name) + 1);
236 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
239 if (list->ips) {
240 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
241 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
244 if (list->ips32) {
245 HKEY ips32_key;
247 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
248 KEY_READ | KEY_WRITE, NULL,
249 &ips32_key, NULL);
250 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
252 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
253 (CONST BYTE*)list->ips32,
254 lstrlenA(list->ips32) + 1);
255 if (res == ERROR_SUCCESS && list->ips32_tmodel)
256 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
257 (CONST BYTE*)list->ips32_tmodel,
258 strlen(list->ips32_tmodel) + 1);
259 RegCloseKey(ips32_key);
260 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
263 if (list->clsid_str) {
264 res = register_key_defvalueA(clsid_key, clsid_keyname,
265 list->clsid_str);
266 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
269 if (list->progid) {
270 HKEY progid_key;
272 res = register_key_defvalueA(clsid_key, progid_keyname,
273 list->progid);
274 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
276 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->progid, 0,
277 NULL, 0, KEY_READ | KEY_WRITE, NULL,
278 &progid_key, NULL);
279 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
281 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
282 RegCloseKey(progid_key);
283 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
286 error_close_clsid_key:
287 RegCloseKey(clsid_key);
290 error_close_coclass_key:
291 RegCloseKey(coclass_key);
292 error_return:
293 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
296 /***********************************************************************
297 * unregister_coclasses
299 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
301 LONG res = ERROR_SUCCESS;
302 HKEY coclass_key;
304 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
305 KEY_READ | KEY_WRITE, &coclass_key);
306 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
307 if (res != ERROR_SUCCESS) goto error_return;
309 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
310 WCHAR buf[39];
312 StringFromGUID2(list->clsid, buf, 39);
313 res = RegDeleteTreeW(coclass_key, buf);
314 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
315 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
317 if (list->progid) {
318 res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->progid);
319 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
320 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
324 error_close_coclass_key:
325 RegCloseKey(coclass_key);
326 error_return:
327 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
330 /***********************************************************************
331 * regsvr_key_guid
333 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
335 WCHAR buf[39];
337 StringFromGUID2(guid, buf, 39);
338 return register_key_defvalueW(base, name, buf);
341 /***********************************************************************
342 * regsvr_key_defvalueW
344 static LONG register_key_defvalueW(
345 HKEY base,
346 WCHAR const *name,
347 WCHAR const *value)
349 LONG res;
350 HKEY key;
352 res = RegCreateKeyExW(base, name, 0, NULL, 0,
353 KEY_READ | KEY_WRITE, NULL, &key, NULL);
354 if (res != ERROR_SUCCESS) return res;
355 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
356 (lstrlenW(value) + 1) * sizeof(WCHAR));
357 RegCloseKey(key);
358 return res;
361 /***********************************************************************
362 * regsvr_key_defvalueA
364 static LONG register_key_defvalueA(
365 HKEY base,
366 WCHAR const *name,
367 char const *value)
369 LONG res;
370 HKEY key;
372 res = RegCreateKeyExW(base, name, 0, NULL, 0,
373 KEY_READ | KEY_WRITE, NULL, &key, NULL);
374 if (res != ERROR_SUCCESS) return res;
375 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
376 lstrlenA(value) + 1);
377 RegCloseKey(key);
378 return res;
381 /***********************************************************************
382 * register_typelib
384 static HRESULT register_typelib( const WCHAR *name )
386 static const WCHAR backslash[] = {'\\',0};
387 HRESULT hr;
388 ITypeLib *typelib;
389 WCHAR *path;
390 DWORD len;
392 len = GetSystemDirectoryW( NULL, 0 ) + strlenW( name ) + 1;
393 if (!(path = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) ))) return E_OUTOFMEMORY;
394 GetSystemDirectoryW( path, len );
395 strcatW( path, backslash );
396 strcatW( path, name );
397 hr = LoadTypeLib( path, &typelib );
398 if (SUCCEEDED(hr))
400 hr = RegisterTypeLib( typelib, path, NULL );
401 ITypeLib_Release( typelib );
403 HeapFree( GetProcessHeap(), 0, path );
404 return hr;
407 /***********************************************************************
408 * coclass list
410 static GUID const CLSID_RecordInfo = {
411 0x0000002F, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
413 static GUID const CLSID_OldFont = {
414 0x46763EE0, 0xCAB2, 0x11CE, {0x8C,0x20,0x00,0xAA,0x00,0x51,0xE5,0xD4} };
416 static struct regsvr_coclass const coclass_list[] = {
417 { &CLSID_RecordInfo,
418 "CLSID_RecordInfo",
419 NULL,
420 "oleaut32.dll",
421 "Both"
423 { &CLSID_PSDispatch,
424 "PSDispatch",
425 "ole2disp.dll",
426 "oleaut32.dll",
427 "Both"
429 { &CLSID_StdFont,
430 "CLSID_StdFont",
431 NULL,
432 "oleaut32.dll",
433 "Both",
434 "Standard Font",
435 "StdFont"
437 { &CLSID_StdPicture,
438 "CLSID_StdPict",
439 NULL,
440 "oleaut32.dll",
441 "Apartment",
442 "Standard Picture",
443 "StdPicture"
445 { &CLSID_PSEnumVariant,
446 "PSEnumVariant",
447 "ole2disp.dll",
448 "oleaut32.dll",
449 "Both"
451 { &CLSID_PSTypeInfo,
452 "PSTypeInfo",
453 "ole2disp.dll",
454 "oleaut32.dll",
455 "Both"
457 { &CLSID_PSTypeLib,
458 "PSTypeLib",
459 "ole2disp.dll",
460 "oleaut32.dll",
461 "Both"
463 { &CLSID_PSOAInterface,
464 "PSOAInterface",
465 "ole2disp.dll",
466 "oleaut32.dll",
467 "Both"
469 { &CLSID_PSTypeComp,
470 "PSTypeComp",
471 "ole2disp.dll",
472 "oleaut32.dll",
473 "Both"
475 { &CLSID_OldFont,
476 "Obsolete Font",
477 NULL,
478 "oleaut32.dll",
479 NULL,
480 "Obsolete Font",
481 "OldFont"
483 { &IID_ISupportErrorInfo,
484 "PSSupportErrorInfo",
485 "ole2disp.dll",
486 "oleaut32.dll",
487 NULL
489 { NULL } /* list terminator */
492 /***********************************************************************
493 * interface list
495 #define INTERFACE_ENTRY(interface, clsid16, clsid32) { &IID_##interface, #interface, NULL, sizeof(interface##Vtbl)/sizeof(void*), clsid16, clsid32 }
496 #define LCL_INTERFACE_ENTRY(interface) INTERFACE_ENTRY(interface, NULL, NULL)
497 #define CLSID_INTERFACE_ENTRY(interface,clsid) INTERFACE_ENTRY(interface, clsid, clsid)
499 static struct regsvr_interface const interface_list[] = {
500 LCL_INTERFACE_ENTRY(ICreateTypeInfo),
501 LCL_INTERFACE_ENTRY(ICreateTypeLib),
502 CLSID_INTERFACE_ENTRY(IDispatch,&CLSID_PSDispatch),
503 CLSID_INTERFACE_ENTRY(IEnumVARIANT,&CLSID_PSEnumVariant),
504 CLSID_INTERFACE_ENTRY(ITypeComp,&CLSID_PSTypeComp),
505 CLSID_INTERFACE_ENTRY(ITypeInfo,&CLSID_PSTypeInfo),
506 CLSID_INTERFACE_ENTRY(ITypeLib,&CLSID_PSTypeLib),
507 INTERFACE_ENTRY(ISupportErrorInfo,NULL,&CLSID_PSDispatch),
508 INTERFACE_ENTRY(ITypeFactory,NULL,&CLSID_PSDispatch),
509 INTERFACE_ENTRY(ITypeInfo2,NULL,&CLSID_PSDispatch),
510 INTERFACE_ENTRY(ITypeLib2,NULL,&CLSID_PSDispatch),
511 { NULL } /* list terminator */
514 extern HRESULT WINAPI OLEAUTPS_DllRegisterServer(void) DECLSPEC_HIDDEN;
515 extern HRESULT WINAPI OLEAUTPS_DllUnregisterServer(void) DECLSPEC_HIDDEN;
517 /***********************************************************************
518 * DllRegisterServer (OLEAUT32.@)
520 HRESULT WINAPI DllRegisterServer(void)
522 HRESULT hr;
524 TRACE("\n");
526 hr = OLEAUTPS_DllRegisterServer();
527 if (SUCCEEDED(hr))
528 hr = register_coclasses(coclass_list);
529 if (SUCCEEDED(hr))
530 hr = register_interfaces(interface_list);
531 if (SUCCEEDED(hr))
533 const WCHAR stdole32W[] = {'s','t','d','o','l','e','3','2','.','t','l','b',0};
534 const WCHAR stdole2W[] = {'s','t','d','o','l','e','2','.','t','l','b',0};
535 hr = register_typelib( stdole2W );
536 if (SUCCEEDED(hr)) hr = register_typelib( stdole32W );
538 return hr;
541 /***********************************************************************
542 * DllUnregisterServer (OLEAUT32.@)
544 HRESULT WINAPI DllUnregisterServer(void)
546 HRESULT hr;
548 TRACE("\n");
550 hr = unregister_coclasses(coclass_list);
551 if (SUCCEEDED(hr))
552 hr = unregister_interfaces(interface_list);
553 if (SUCCEEDED(hr))
554 hr = OLEAUTPS_DllUnregisterServer();
555 return hr;