Revert the sync.
[reactos.git] / dll / win32 / oleaut32 / regsvr.c
1 /*
2 * self-registerable dll functions for oleaut32.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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdarg.h>
22 #include <string.h>
23
24 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winreg.h"
29 #include "winerror.h"
30
31 #include "ole2.h"
32 #include "olectl.h"
33 #include "oleauto.h"
34 #include "initguid.h"
35 #include "typelib.h"
36 #include "wincodec.h"
37
38 #include "wine/debug.h"
39 #include "wine/unicode.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(ole);
42
43 /*
44 * Near the bottom of this file are the exported DllRegisterServer and
45 * DllUnregisterServer, which make all this worthwhile.
46 */
47
48 /***********************************************************************
49 * interface for self-registering
50 */
51 struct regsvr_interface
52 {
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 */
59 };
60
61 static HRESULT register_interfaces(struct regsvr_interface const *list);
62 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
63
64 struct regsvr_coclass
65 {
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 */
73 };
74
75 static HRESULT register_coclasses(struct regsvr_coclass const *list);
76 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
77
78 /***********************************************************************
79 * static string constants
80 */
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";
105
106 /***********************************************************************
107 * static helper functions
108 */
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);
114
115 /***********************************************************************
116 * register_interfaces
117 */
118 static HRESULT register_interfaces(struct regsvr_interface const *list)
119 {
120 LONG res = ERROR_SUCCESS;
121 HKEY interface_key;
122
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;
126
127 for (; res == ERROR_SUCCESS && list->iid; ++list) {
128 WCHAR buf[39];
129 HKEY iid_key;
130
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;
135
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;
141 }
142
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;
146 }
147
148 if (0 <= list->num_methods) {
149 static WCHAR const fmt[3] = { '%', 'd', 0 };
150 HKEY key;
151
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;
155
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);
161
162 if (res != ERROR_SUCCESS) goto error_close_iid_key;
163 }
164
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;
168 }
169
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;
173 }
174
175 error_close_iid_key:
176 RegCloseKey(iid_key);
177 }
178
179 error_close_interface_key:
180 RegCloseKey(interface_key);
181 error_return:
182 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
183 }
184
185 /***********************************************************************
186 * unregister_interfaces
187 */
188 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
189 {
190 LONG res = ERROR_SUCCESS;
191 HKEY interface_key;
192
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;
197
198 for (; res == ERROR_SUCCESS && list->iid; ++list) {
199 WCHAR buf[39];
200
201 StringFromGUID2(list->iid, buf, 39);
202 res = RegDeleteTreeW(interface_key, buf);
203 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
204 }
205
206 RegCloseKey(interface_key);
207 error_return:
208 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
209 }
210
211 /***********************************************************************
212 * register_coclasses
213 */
214 static HRESULT register_coclasses(struct regsvr_coclass const *list)
215 {
216 LONG res = ERROR_SUCCESS;
217 HKEY coclass_key;
218
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;
222
223 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
224 WCHAR buf[39];
225 HKEY clsid_key;
226
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;
231
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;
237 }
238
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;
242 }
243
244 if (list->ips32) {
245 HKEY ips32_key;
246
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;
251
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;
261 }
262
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;
267 }
268
269 if (list->progid) {
270 HKEY progid_key;
271
272 res = register_key_defvalueA(clsid_key, progid_keyname,
273 list->progid);
274 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
275
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;
280
281 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
282 RegCloseKey(progid_key);
283 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
284 }
285
286 error_close_clsid_key:
287 RegCloseKey(clsid_key);
288 }
289
290 error_close_coclass_key:
291 RegCloseKey(coclass_key);
292 error_return:
293 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
294 }
295
296 /***********************************************************************
297 * unregister_coclasses
298 */
299 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
300 {
301 LONG res = ERROR_SUCCESS;
302 HKEY coclass_key;
303
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;
308
309 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
310 WCHAR buf[39];
311
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;
316
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;
321 }
322 }
323
324 error_close_coclass_key:
325 RegCloseKey(coclass_key);
326 error_return:
327 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
328 }
329
330 /***********************************************************************
331 * regsvr_key_guid
332 */
333 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
334 {
335 WCHAR buf[39];
336
337 StringFromGUID2(guid, buf, 39);
338 return register_key_defvalueW(base, name, buf);
339 }
340
341 /***********************************************************************
342 * regsvr_key_defvalueW
343 */
344 static LONG register_key_defvalueW(
345 HKEY base,
346 WCHAR const *name,
347 WCHAR const *value)
348 {
349 LONG res;
350 HKEY key;
351
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;
359 }
360
361 /***********************************************************************
362 * regsvr_key_defvalueA
363 */
364 static LONG register_key_defvalueA(
365 HKEY base,
366 WCHAR const *name,
367 char const *value)
368 {
369 LONG res;
370 HKEY key;
371
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;
379 }
380
381 /***********************************************************************
382 * register_typelib
383 */
384 static HRESULT register_typelib( const WCHAR *name )
385 {
386 static const WCHAR backslash[] = {'\\',0};
387 HRESULT hr;
388 ITypeLib *typelib;
389 WCHAR *path;
390 DWORD len;
391
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))
399 {
400 hr = RegisterTypeLib( typelib, path, NULL );
401 ITypeLib_Release( typelib );
402 }
403 HeapFree( GetProcessHeap(), 0, path );
404 return hr;
405 }
406
407 /***********************************************************************
408 * coclass list
409 */
410 static GUID const CLSID_RecordInfo = {
411 0x0000002F, 0x0000, 0x0000, {0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x46} };
412
413 static GUID const CLSID_OldFont = {
414 0x46763EE0, 0xCAB2, 0x11CE, {0x8C,0x20,0x00,0xAA,0x00,0x51,0xE5,0xD4} };
415
416 static struct regsvr_coclass const coclass_list[] = {
417 { &CLSID_RecordInfo,
418 "CLSID_RecordInfo",
419 NULL,
420 "oleaut32.dll",
421 "Both"
422 },
423 { &CLSID_PSDispatch,
424 "PSDispatch",
425 "ole2disp.dll",
426 "oleaut32.dll",
427 "Both"
428 },
429 { &CLSID_StdFont,
430 "CLSID_StdFont",
431 NULL,
432 "oleaut32.dll",
433 "Both",
434 "Standard Font",
435 "StdFont"
436 },
437 { &CLSID_StdPicture,
438 "CLSID_StdPict",
439 NULL,
440 "oleaut32.dll",
441 "Apartment",
442 "Standard Picture",
443 "StdPicture"
444 },
445 { &CLSID_PSEnumVariant,
446 "PSEnumVariant",
447 "ole2disp.dll",
448 "oleaut32.dll",
449 "Both"
450 },
451 { &CLSID_PSTypeInfo,
452 "PSTypeInfo",
453 "ole2disp.dll",
454 "oleaut32.dll",
455 "Both"
456 },
457 { &CLSID_PSTypeLib,
458 "PSTypeLib",
459 "ole2disp.dll",
460 "oleaut32.dll",
461 "Both"
462 },
463 { &CLSID_PSOAInterface,
464 "PSOAInterface",
465 "ole2disp.dll",
466 "oleaut32.dll",
467 "Both"
468 },
469 { &CLSID_PSTypeComp,
470 "PSTypeComp",
471 "ole2disp.dll",
472 "oleaut32.dll",
473 "Both"
474 },
475 { &CLSID_OldFont,
476 "Obsolete Font",
477 NULL,
478 "oleaut32.dll",
479 NULL,
480 "Obsolete Font",
481 "OldFont"
482 },
483 { NULL } /* list terminator */
484 };
485
486 /***********************************************************************
487 * interface list
488 */
489 #define INTERFACE_ENTRY(interface, clsid16, clsid32) { &IID_##interface, #interface, NULL, sizeof(interface##Vtbl)/sizeof(void*), clsid16, clsid32 }
490 #define LCL_INTERFACE_ENTRY(interface) INTERFACE_ENTRY(interface, NULL, NULL)
491 #define CLSID_INTERFACE_ENTRY(interface,clsid) INTERFACE_ENTRY(interface, clsid, clsid)
492
493 static struct regsvr_interface const interface_list[] = {
494 LCL_INTERFACE_ENTRY(ICreateTypeInfo),
495 LCL_INTERFACE_ENTRY(ICreateTypeLib),
496 CLSID_INTERFACE_ENTRY(IDispatch,&CLSID_PSDispatch),
497 CLSID_INTERFACE_ENTRY(IEnumVARIANT,&CLSID_PSEnumVariant),
498 CLSID_INTERFACE_ENTRY(ITypeComp,&CLSID_PSTypeComp),
499 CLSID_INTERFACE_ENTRY(ITypeInfo,&CLSID_PSTypeInfo),
500 CLSID_INTERFACE_ENTRY(ITypeLib,&CLSID_PSTypeLib),
501 INTERFACE_ENTRY(ISupportErrorInfo,NULL,&CLSID_PSDispatch),
502 INTERFACE_ENTRY(ITypeFactory,NULL,&CLSID_PSDispatch),
503 INTERFACE_ENTRY(ITypeInfo2,NULL,&CLSID_PSDispatch),
504 INTERFACE_ENTRY(ITypeLib2,NULL,&CLSID_PSDispatch),
505 { NULL } /* list terminator */
506 };
507
508 extern HRESULT WINAPI OLEAUTPS_DllRegisterServer(void) DECLSPEC_HIDDEN;
509 extern HRESULT WINAPI OLEAUTPS_DllUnregisterServer(void) DECLSPEC_HIDDEN;
510
511 /***********************************************************************
512 * DllRegisterServer (OLEAUT32.@)
513 */
514 HRESULT WINAPI DllRegisterServer(void)
515 {
516 HRESULT hr;
517
518 TRACE("\n");
519
520 hr = OLEAUTPS_DllRegisterServer();
521 if (SUCCEEDED(hr))
522 hr = register_coclasses(coclass_list);
523 if (SUCCEEDED(hr))
524 hr = register_interfaces(interface_list);
525 if (SUCCEEDED(hr))
526 {
527 const WCHAR stdole32W[] = {'s','t','d','o','l','e','3','2','.','t','l','b',0};
528 const WCHAR stdole2W[] = {'s','t','d','o','l','e','2','.','t','l','b',0};
529 hr = register_typelib( stdole2W );
530 if (SUCCEEDED(hr)) hr = register_typelib( stdole32W );
531 }
532 return hr;
533 }
534
535 /***********************************************************************
536 * DllUnregisterServer (OLEAUT32.@)
537 */
538 HRESULT WINAPI DllUnregisterServer(void)
539 {
540 HRESULT hr;
541
542 TRACE("\n");
543
544 hr = unregister_coclasses(coclass_list);
545 if (SUCCEEDED(hr))
546 hr = unregister_interfaces(interface_list);
547 if (SUCCEEDED(hr))
548 hr = OLEAUTPS_DllUnregisterServer();
549 return hr;
550 }