quartz: Fix string buffer overflow.
[wine.git] / dlls / msxml3 / regsvr.c
blob05bf74d1d6f80921bbd3c10e09a9f7880b33fdde
1 /*
2 * MSXML3 self-registerable dll functions
4 * Copyright (C) 2003 John K. Hohm
5 * Copyright (C) 2006 Robert Shearman
6 * Copyright (C) 2008 Alistair Leslie-Hughes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <string.h>
28 #define COBJMACROS
30 #include "windef.h"
31 #include "winbase.h"
32 #include "winuser.h"
33 #include "winreg.h"
34 #include "winerror.h"
35 #include "ole2.h"
36 #include "msxml.h"
37 #include "xmldom.h"
38 #include "xmldso.h"
39 #include "msxml2.h"
41 /* undef the #define in msxml2 so that we can access the v.2 version
42 independent CLSID as well as the v.3 one. */
43 #undef CLSID_DOMDocument
45 #include "msxml_private.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(ole);
52 * Near the bottom of this file are the exported DllRegisterServer and
53 * DllUnregisterServer, which make all this worthwhile.
56 /***********************************************************************
57 * interface for self-registering
59 struct regsvr_interface
61 IID const *iid; /* NULL for end of list */
62 LPCSTR name; /* can be NULL to omit */
63 IID const *base_iid; /* can be NULL to omit */
64 int num_methods; /* can be <0 to omit */
65 CLSID const *ps_clsid; /* can be NULL to omit */
66 CLSID const *ps_clsid32; /* can be NULL to omit */
69 static HRESULT register_interfaces(struct regsvr_interface const *list);
70 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
72 struct regsvr_coclass
74 CLSID const *clsid; /* NULL for end of list */
75 LPCSTR name; /* can be NULL to omit */
76 LPCSTR ips; /* can be NULL to omit */
77 LPCSTR ips32; /* can be NULL to omit */
78 LPCSTR ips32_tmodel; /* can be NULL to omit */
79 LPCSTR progid; /* can be NULL to omit */
80 LPCSTR version; /* can be NULL to omit */
83 static HRESULT register_coclasses(struct regsvr_coclass const *list);
84 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
86 struct progid
88 LPCSTR name; /* NULL for end of list */
89 LPCSTR description; /* can be NULL to omit */
90 CLSID const *clsid;
91 LPCSTR curver; /* can be NULL to omit */
94 static HRESULT register_progids(struct progid const *list);
95 static HRESULT unregister_progids(struct progid const *list);
97 /***********************************************************************
98 * static string constants
100 static WCHAR const interface_keyname[10] = {
101 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
102 static WCHAR const base_ifa_keyname[14] = {
103 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
104 'e', 0 };
105 static WCHAR const num_methods_keyname[11] = {
106 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
107 static WCHAR const ps_clsid_keyname[15] = {
108 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
109 'i', 'd', 0 };
110 static WCHAR const ps_clsid32_keyname[17] = {
111 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
112 'i', 'd', '3', '2', 0 };
113 static WCHAR const clsid_keyname[6] = {
114 'C', 'L', 'S', 'I', 'D', 0 };
115 static WCHAR const ips_keyname[13] = {
116 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
117 0 };
118 static WCHAR const ips32_keyname[15] = {
119 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
120 '3', '2', 0 };
121 static WCHAR const progid_keyname[7] = {
122 'P', 'r', 'o', 'g', 'I', 'D', 0 };
123 static WCHAR const versionindependentprogid_keyname[] = {
124 'V', 'e', 'r', 's', 'i', 'o', 'n',
125 'I', 'n', 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 't',
126 'P', 'r', 'o', 'g', 'I', 'D', 0 };
127 static WCHAR const version_keyname[] = {
128 'V', 'e', 'r', 's', 'i', 'o', 'n', 0 };
129 static WCHAR const curver_keyname[] = {
130 'C', 'u', 'r', 'V', 'e', 'r', 0 };
131 static char const tmodel_valuename[] = "ThreadingModel";
133 /***********************************************************************
134 * static helper functions
136 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
137 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
138 WCHAR const *value);
139 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
140 char const *value);
142 /***********************************************************************
143 * register_interfaces
145 static HRESULT register_interfaces(struct regsvr_interface const *list)
147 LONG res = ERROR_SUCCESS;
148 HKEY interface_key;
150 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
151 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
152 if (res != ERROR_SUCCESS) goto error_return;
154 for (; res == ERROR_SUCCESS && list->iid; ++list) {
155 WCHAR buf[39];
156 HKEY iid_key;
158 StringFromGUID2(list->iid, buf, 39);
159 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
160 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
161 if (res != ERROR_SUCCESS) goto error_close_interface_key;
163 if (list->name) {
164 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
165 (CONST BYTE*)(list->name),
166 strlen(list->name) + 1);
167 if (res != ERROR_SUCCESS) goto error_close_iid_key;
170 if (list->base_iid) {
171 res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
172 if (res != ERROR_SUCCESS) goto error_close_iid_key;
175 if (0 <= list->num_methods) {
176 static WCHAR const fmt[3] = { '%', 'd', 0 };
177 HKEY key;
179 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
180 KEY_READ | KEY_WRITE, NULL, &key, NULL);
181 if (res != ERROR_SUCCESS) goto error_close_iid_key;
183 wsprintfW(buf, fmt, list->num_methods);
184 res = RegSetValueExW(key, NULL, 0, REG_SZ,
185 (CONST BYTE*)buf,
186 (lstrlenW(buf) + 1) * sizeof(WCHAR));
187 RegCloseKey(key);
189 if (res != ERROR_SUCCESS) goto error_close_iid_key;
192 if (list->ps_clsid) {
193 res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
194 if (res != ERROR_SUCCESS) goto error_close_iid_key;
197 if (list->ps_clsid32) {
198 res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
199 if (res != ERROR_SUCCESS) goto error_close_iid_key;
202 error_close_iid_key:
203 RegCloseKey(iid_key);
206 error_close_interface_key:
207 RegCloseKey(interface_key);
208 error_return:
209 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
212 /***********************************************************************
213 * unregister_interfaces
215 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
217 LONG res = ERROR_SUCCESS;
218 HKEY interface_key;
220 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
221 KEY_READ | KEY_WRITE, &interface_key);
222 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
223 if (res != ERROR_SUCCESS) goto error_return;
225 for (; res == ERROR_SUCCESS && list->iid; ++list) {
226 WCHAR buf[39];
228 StringFromGUID2(list->iid, buf, 39);
229 res = RegDeleteTreeW(interface_key, buf);
230 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
233 RegCloseKey(interface_key);
234 error_return:
235 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
238 /***********************************************************************
239 * register_coclasses
241 static HRESULT register_coclasses(struct regsvr_coclass const *list)
243 LONG res = ERROR_SUCCESS;
244 HKEY coclass_key;
246 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
247 KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
248 if (res != ERROR_SUCCESS) goto error_return;
250 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
251 WCHAR buf[39];
252 HKEY clsid_key;
254 StringFromGUID2(list->clsid, buf, 39);
255 res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
256 KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
257 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
259 if (list->name) {
260 res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
261 (CONST BYTE*)(list->name),
262 strlen(list->name) + 1);
263 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
266 if (list->ips) {
267 res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
268 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
271 if (list->ips32) {
272 HKEY ips32_key;
274 res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
275 KEY_READ | KEY_WRITE, NULL,
276 &ips32_key, NULL);
277 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
279 res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
280 (CONST BYTE*)list->ips32,
281 lstrlenA(list->ips32) + 1);
282 if (res == ERROR_SUCCESS && list->ips32_tmodel)
283 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
284 (CONST BYTE*)list->ips32_tmodel,
285 strlen(list->ips32_tmodel) + 1);
286 RegCloseKey(ips32_key);
287 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
290 if (list->progid) {
291 char *buffer = NULL;
292 LPCSTR progid;
294 if (list->version) {
295 buffer = HeapAlloc(GetProcessHeap(), 0, strlen(list->progid) + strlen(list->version) + 2);
296 if (!buffer) {
297 res = ERROR_OUTOFMEMORY;
298 goto error_close_clsid_key;
300 strcpy(buffer, list->progid);
301 strcat(buffer, ".");
302 strcat(buffer, list->version);
303 progid = buffer;
304 } else
305 progid = list->progid;
306 res = register_key_defvalueA(clsid_key, progid_keyname,
307 progid);
308 HeapFree(GetProcessHeap(), 0, buffer);
309 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
311 if (list->version) {
312 res = register_key_defvalueA(clsid_key, versionindependentprogid_keyname,
313 list->progid);
314 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
318 if (list->version) {
319 res = register_key_defvalueA(clsid_key, version_keyname,
320 list->version);
321 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
324 error_close_clsid_key:
325 RegCloseKey(clsid_key);
328 error_close_coclass_key:
329 RegCloseKey(coclass_key);
330 error_return:
331 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
334 /***********************************************************************
335 * unregister_coclasses
337 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
339 LONG res = ERROR_SUCCESS;
340 HKEY coclass_key;
342 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
343 KEY_READ | KEY_WRITE, &coclass_key);
344 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
345 if (res != ERROR_SUCCESS) goto error_return;
347 for (; res == ERROR_SUCCESS && list->clsid; ++list) {
348 WCHAR buf[39];
350 StringFromGUID2(list->clsid, buf, 39);
351 res = RegDeleteTreeW(coclass_key, buf);
352 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
353 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
356 error_close_coclass_key:
357 RegCloseKey(coclass_key);
358 error_return:
359 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
362 /***********************************************************************
363 * register_progids
365 static HRESULT register_progids(struct progid const *list)
367 LONG res = ERROR_SUCCESS;
369 for (; res == ERROR_SUCCESS && list->name; ++list) {
370 WCHAR buf[39];
371 HKEY progid_key;
373 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->name, 0,
374 NULL, 0, KEY_READ | KEY_WRITE, NULL,
375 &progid_key, NULL);
376 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
378 res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
379 (CONST BYTE*)list->description,
380 strlen(list->description) + 1);
381 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
383 StringFromGUID2(list->clsid, buf, 39);
385 res = register_key_defvalueW(progid_key, clsid_keyname, buf);
386 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
388 if (list->curver) {
389 res = register_key_defvalueA(progid_key, curver_keyname, list->curver);
390 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
393 error_close_clsid_key:
394 RegCloseKey(progid_key);
397 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
400 /***********************************************************************
401 * unregister_progids
403 static HRESULT unregister_progids(struct progid const *list)
405 LONG res = ERROR_SUCCESS;
407 for (; res == ERROR_SUCCESS && list->name; ++list) {
408 res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->name);
409 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
412 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
415 /***********************************************************************
416 * regsvr_key_guid
418 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
420 WCHAR buf[39];
422 StringFromGUID2(guid, buf, 39);
423 return register_key_defvalueW(base, name, buf);
426 /***********************************************************************
427 * regsvr_key_defvalueW
429 static LONG register_key_defvalueW(
430 HKEY base,
431 WCHAR const *name,
432 WCHAR const *value)
434 LONG res;
435 HKEY key;
437 res = RegCreateKeyExW(base, name, 0, NULL, 0,
438 KEY_READ | KEY_WRITE, NULL, &key, NULL);
439 if (res != ERROR_SUCCESS) return res;
440 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
441 (lstrlenW(value) + 1) * sizeof(WCHAR));
442 RegCloseKey(key);
443 return res;
446 /***********************************************************************
447 * regsvr_key_defvalueA
449 static LONG register_key_defvalueA(
450 HKEY base,
451 WCHAR const *name,
452 char const *value)
454 LONG res;
455 HKEY key;
457 res = RegCreateKeyExW(base, name, 0, NULL, 0,
458 KEY_READ | KEY_WRITE, NULL, &key, NULL);
459 if (res != ERROR_SUCCESS) return res;
460 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
461 lstrlenA(value) + 1);
462 RegCloseKey(key);
463 return res;
466 /***********************************************************************
467 * coclass list
469 static struct regsvr_coclass const coclass_list[] = {
470 { &CLSID_DOMDocument,
471 "XML DOM Document",
472 NULL,
473 "msxml3.dll",
474 "Both",
475 "Microsoft.XMLDOM",
476 "1.0"
478 { &CLSID_DOMDocument2,
479 "XML DOM Document",
480 NULL,
481 "msxml3.dll",
482 "Both",
483 "Msxml2.DOMDocument",
484 "3.0"
486 { &CLSID_DOMDocument30,
487 "XML DOM Document 3.0",
488 NULL,
489 "msxml3.dll",
490 "Both",
491 "Msxml2.DOMDocument",
492 "3.0"
494 { &CLSID_DOMFreeThreadedDocument,
495 "Free threaded XML DOM Document",
496 NULL,
497 "msxml3.dll",
498 "Both",
499 "Microsoft.FreeThreadedXMLDOM",
500 "1.0"
502 { &CLSID_DOMFreeThreadedDocument,
503 "Free threaded XML DOM Document",
504 NULL,
505 "msxml3.dll",
506 "Both",
507 "Microsoft.FreeThreadedXMLDOM",
508 NULL
510 { &CLSID_FreeThreadedDOMDocument,
511 "Free Threaded XML DOM Document",
512 NULL,
513 "msxml3.dll",
514 "Both",
515 "Microsoft.FreeThreadedXMLDOM.1.0",
516 "1.0"
519 { &CLSID_XMLHTTPRequest,
520 "XML HTTP Request",
521 NULL,
522 "msxml3.dll",
523 "Apartment",
524 "Microsoft.XMLHTTP",
525 "1.0"
527 { &CLSID_XMLDSOControl,
528 "XML Data Source Object",
529 NULL,
530 "msxml3.dll",
531 "Apartment",
532 "Microsoft.XMLDSO",
533 "1.0"
535 { &CLSID_XMLDocument,
536 "Msxml",
537 NULL,
538 "msxml3.dll",
539 "Both",
540 "Msxml"
542 { &CLSID_XMLSchemaCache,
543 "XML Schema Cache",
544 NULL,
545 "msxml3.dll",
546 "Both",
547 "Msxml2.XMLSchemaCache",
548 "3.0"
550 { &CLSID_XMLSchemaCache30,
551 "XML Schema Cache 3.0",
552 NULL,
553 "msxml3.dll",
554 "Both",
555 "Msxml2.XMLSchemaCache",
556 "3.0"
558 { &CLSID_SAXXMLReader,
559 "SAX XML Reader",
560 NULL,
561 "msxml3.dll",
562 "Both",
563 "Msxml2.SAXXMLReader",
564 "3.0"
566 { NULL } /* list terminator */
569 /***********************************************************************
570 * interface list
572 static struct regsvr_interface const interface_list[] = {
573 { NULL } /* list terminator */
576 /***********************************************************************
577 * progid list
579 static struct progid const progid_list[] = {
580 { "Microsoft.XMLDOM",
581 "XML DOM Document",
582 &CLSID_DOMDocument,
583 "Microsoft.XMLDOM.1.0"
585 { "Microsoft.XMLDOM.1.0",
586 "XML DOM Document",
587 &CLSID_DOMDocument,
588 NULL
590 { "MSXML.DOMDocument",
591 "XML DOM Document",
592 &CLSID_DOMDocument,
593 "Microsoft.XMLDOM.1.0"
595 { "Msxml2.DOMDocument",
596 "XML DOM Document",
597 &CLSID_DOMDocument2,
598 "Msxml2.DOMDocument.3.0"
600 { "Msxml2.DOMDocument.3.0",
601 "XML DOM Document 3.0",
602 &CLSID_DOMDocument30,
603 NULL
605 { "Microsoft.FreeThreadedXMLDOM",
606 "Free threaded XML DOM Document",
607 &CLSID_DOMFreeThreadedDocument,
608 "Microsoft.FreeThreadedXMLDOM.1.0"
610 { "Microsoft.FreeThreadedXMLDOM.1.0",
611 "Free threaded XML DOM Document",
612 &CLSID_DOMFreeThreadedDocument,
613 NULL
615 { "MSXML.FreeThreadedDOMDocument",
616 "Free threaded XML DOM Document",
617 &CLSID_DOMFreeThreadedDocument,
618 "Microsoft.FreeThreadedXMLDOM.1.0"
620 { "Microsoft.XMLHTTP",
621 "XML HTTP Request",
622 &CLSID_XMLHTTPRequest,
623 "Microsoft.XMLHTTP.1.0"
625 { "Microsoft.XMLHTTP.1.0",
626 "XML HTTP Request",
627 &CLSID_XMLHTTPRequest,
628 NULL
630 { "Microsoft.XMLDSO",
631 "XML Data Source Object",
632 &CLSID_XMLDSOControl,
633 "Microsoft.XMLDSO.1.0"
635 { "Microsoft.XMLDSO.1.0",
636 "XML Data Source Object",
637 &CLSID_XMLDSOControl,
638 NULL
640 { "Msxml",
641 "Msxml",
642 &CLSID_XMLDocument,
643 NULL
645 { "Msxml2.XMLSchemaCache",
646 "XML Schema Cache",
647 &CLSID_XMLSchemaCache,
648 "Msxml2.XMLSchemaCache.3.0"
650 { "Msxml2.XMLSchemaCache.3.0",
651 "XML Schema Cache 3.0",
652 &CLSID_XMLSchemaCache30,
653 NULL
656 { NULL } /* list terminator */
659 /***********************************************************************
660 * DllRegisterServer (OLEAUT32.@)
662 HRESULT WINAPI DllRegisterServer(void)
664 HRESULT hr;
665 ITypeLib *tl;
666 LPWSTR path = NULL;
668 TRACE("\n");
670 hr = register_coclasses(coclass_list);
671 if (SUCCEEDED(hr))
672 hr = register_interfaces(interface_list);
673 if (SUCCEEDED(hr))
674 hr = register_progids(progid_list);
676 tl = get_msxml3_typelib( &path );
677 if (tl)
679 hr = RegisterTypeLib( tl, path, NULL );
680 ITypeLib_Release( tl );
682 else
683 hr = E_FAIL;
685 return hr;
688 /***********************************************************************
689 * DllUnregisterServer (OLEAUT32.@)
691 HRESULT WINAPI DllUnregisterServer(void)
693 HRESULT hr;
695 TRACE("\n");
697 hr = unregister_coclasses(coclass_list);
698 if (SUCCEEDED(hr))
699 hr = unregister_interfaces(interface_list);
700 if (SUCCEEDED(hr))
701 hr = unregister_progids(progid_list);
702 if (SUCCEEDED(hr))
703 hr = UnRegisterTypeLib(&LIBID_MSXML2, 3, 0, LOCALE_SYSTEM_DEFAULT, SYS_WIN32);
705 return hr;