msi/test: Add tests for MsiGetFeatureState.
[wine] / dlls / msxml6 / regsvr.c
1 /*
2  *      MSXML6 self-registerable dll functions
3  *
4  * Copyright (C) 2003 John K. Hohm
5  * Copyright (C) 2006 Robert Shearman
6  * Copyright (C) 2008 Alistair Leslie-Hughes
7  * Copyright (C) 2010 Nikolay Sivov for CodeWeavers
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22  */
23
24 #include "config.h"
25
26 #include <stdarg.h>
27 #include <string.h>
28
29 #define COBJMACROS
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "winreg.h"
35 #include "winerror.h"
36 #include "ole2.h"
37
38 #include "initguid.h"
39 #include "msxml2.h"
40 #include "msxml6.h"
41
42 #include "wine/debug.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(msxml6);
45
46 /*
47  * Near the bottom of this file are the exported DllRegisterServer and
48  * DllUnregisterServer, which make all this worthwhile.
49  */
50
51 struct regsvr_coclass
52 {
53     CLSID const *clsid;         /* NULL for end of list */
54     LPCSTR name;                /* can be NULL to omit */
55     LPCSTR ips;                 /* can be NULL to omit */
56     LPCSTR ips32;               /* can be NULL to omit */
57     LPCSTR ips32_tmodel;        /* can be NULL to omit */
58     LPCSTR progid;              /* can be NULL to omit */
59     LPCSTR version;             /* can be NULL to omit */
60 };
61
62 static HRESULT register_coclasses(struct regsvr_coclass const *list);
63 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
64
65 struct progid
66 {
67     LPCSTR name;                /* NULL for end of list */
68     LPCSTR description;         /* can be NULL to omit */
69     CLSID const *clsid;
70     LPCSTR curver;              /* can be NULL to omit */
71 };
72
73 static HRESULT register_progids(struct progid const *list);
74 static HRESULT unregister_progids(struct progid const *list);
75
76 /***********************************************************************
77  *              static string constants
78  */
79 static WCHAR const interface_keyname[10] = {
80     'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
81 static WCHAR const base_ifa_keyname[14] = {
82     'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
83     'e', 0 };
84 static WCHAR const num_methods_keyname[11] = {
85     'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
86 static WCHAR const ps_clsid_keyname[15] = {
87     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
88     'i', 'd', 0 };
89 static WCHAR const ps_clsid32_keyname[17] = {
90     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
91     'i', 'd', '3', '2', 0 };
92 static WCHAR const clsid_keyname[6] = {
93     'C', 'L', 'S', 'I', 'D', 0 };
94 static WCHAR const ips_keyname[13] = {
95     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
96     0 };
97 static WCHAR const ips32_keyname[15] = {
98     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
99     '3', '2', 0 };
100 static WCHAR const progid_keyname[7] = {
101     'P', 'r', 'o', 'g', 'I', 'D', 0 };
102 static WCHAR const versionindependentprogid_keyname[] = {
103     'V', 'e', 'r', 's', 'i', 'o', 'n',
104     'I', 'n', 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 't',
105     'P', 'r', 'o', 'g', 'I', 'D', 0 };
106 static WCHAR const version_keyname[] = {
107     'V', 'e', 'r', 's', 'i', 'o', 'n', 0 };
108 static WCHAR const curver_keyname[] = {
109     'C', 'u', 'r', 'V', 'e', 'r', 0 };
110 static char const tmodel_valuename[] = "ThreadingModel";
111
112 /***********************************************************************
113  *              static helper functions
114  */
115 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
116                                    WCHAR const *value);
117 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
118                                    char const *value);
119
120 /***********************************************************************
121  *              register_coclasses
122  */
123 static HRESULT register_coclasses(struct regsvr_coclass const *list)
124 {
125     LONG res = ERROR_SUCCESS;
126     HKEY coclass_key;
127
128     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
129                           KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
130     if (res != ERROR_SUCCESS) goto error_return;
131
132     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
133         WCHAR buf[39];
134         HKEY clsid_key;
135
136         StringFromGUID2(list->clsid, buf, 39);
137         res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
138                               KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
139         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
140
141         if (list->name) {
142             res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
143                                  (CONST BYTE*)(list->name),
144                                  strlen(list->name) + 1);
145             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
146         }
147
148         if (list->ips) {
149             res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
150             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
151         }
152
153         if (list->ips32) {
154             HKEY ips32_key;
155
156             res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
157                                   KEY_READ | KEY_WRITE, NULL,
158                                   &ips32_key, NULL);
159             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
160
161             res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
162                                  (CONST BYTE*)list->ips32,
163                                  lstrlenA(list->ips32) + 1);
164             if (res == ERROR_SUCCESS && list->ips32_tmodel)
165                 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
166                                      (CONST BYTE*)list->ips32_tmodel,
167                                      strlen(list->ips32_tmodel) + 1);
168             RegCloseKey(ips32_key);
169             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
170         }
171
172         if (list->progid) {
173             char *buffer = NULL;
174             LPCSTR progid;
175
176             if (list->version) {
177                 buffer = HeapAlloc(GetProcessHeap(), 0, strlen(list->progid) + strlen(list->version) + 2);
178                 if (!buffer) {
179                     res = ERROR_OUTOFMEMORY;
180                     goto error_close_clsid_key;
181                 }
182                 strcpy(buffer, list->progid);
183                 strcat(buffer, ".");
184                 strcat(buffer, list->version);
185                 progid = buffer;
186             } else
187                 progid = list->progid;
188             res = register_key_defvalueA(clsid_key, progid_keyname,
189                                          progid);
190             HeapFree(GetProcessHeap(), 0, buffer);
191             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
192
193             if (list->version) {
194                 res = register_key_defvalueA(clsid_key, versionindependentprogid_keyname,
195                                              list->progid);
196                 if (res != ERROR_SUCCESS) goto error_close_clsid_key;
197             }
198         }
199
200         if (list->version) {
201             res = register_key_defvalueA(clsid_key, version_keyname,
202                                          list->version);
203             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
204         }
205
206     error_close_clsid_key:
207         RegCloseKey(clsid_key);
208     }
209
210 error_close_coclass_key:
211     RegCloseKey(coclass_key);
212 error_return:
213     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
214 }
215
216 /***********************************************************************
217  *              unregister_coclasses
218  */
219 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
220 {
221     LONG res = ERROR_SUCCESS;
222     HKEY coclass_key;
223
224     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
225                         KEY_READ | KEY_WRITE, &coclass_key);
226     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
227     if (res != ERROR_SUCCESS) goto error_return;
228
229     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
230         WCHAR buf[39];
231
232         StringFromGUID2(list->clsid, buf, 39);
233         res = RegDeleteTreeW(coclass_key, buf);
234         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
235         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
236     }
237
238 error_close_coclass_key:
239     RegCloseKey(coclass_key);
240 error_return:
241     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
242 }
243
244 /***********************************************************************
245  *              register_progids
246  */
247 static HRESULT register_progids(struct progid const *list)
248 {
249     LONG res = ERROR_SUCCESS;
250
251     for (; res == ERROR_SUCCESS && list->name; ++list) {
252         WCHAR buf[39];
253         HKEY progid_key;
254
255         res = RegCreateKeyExA(HKEY_CLASSES_ROOT, list->name, 0,
256                           NULL, 0, KEY_READ | KEY_WRITE, NULL,
257                           &progid_key, NULL);
258         if (res != ERROR_SUCCESS) goto error_close_clsid_key;
259
260         res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
261                          (CONST BYTE*)list->description,
262                          strlen(list->description) + 1);
263         if (res != ERROR_SUCCESS) goto error_close_clsid_key;
264
265         StringFromGUID2(list->clsid, buf, 39);
266
267         res = register_key_defvalueW(progid_key, clsid_keyname, buf);
268         if (res != ERROR_SUCCESS) goto error_close_clsid_key;
269
270         if (list->curver) {
271             res = register_key_defvalueA(progid_key, curver_keyname, list->curver);
272             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
273         }
274
275     error_close_clsid_key:
276         RegCloseKey(progid_key);
277     }
278
279     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
280 }
281
282 /***********************************************************************
283  *              unregister_progids
284  */
285 static HRESULT unregister_progids(struct progid const *list)
286 {
287     LONG res = ERROR_SUCCESS;
288
289     for (; res == ERROR_SUCCESS && list->name; ++list) {
290         res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->name);
291         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
292     }
293
294     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
295 }
296
297 /***********************************************************************
298  *              regsvr_key_defvalueW
299  */
300 static LONG register_key_defvalueW(
301     HKEY base,
302     WCHAR const *name,
303     WCHAR const *value)
304 {
305     LONG res;
306     HKEY key;
307
308     res = RegCreateKeyExW(base, name, 0, NULL, 0,
309                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
310     if (res != ERROR_SUCCESS) return res;
311     res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
312                          (lstrlenW(value) + 1) * sizeof(WCHAR));
313     RegCloseKey(key);
314     return res;
315 }
316
317 /***********************************************************************
318  *              regsvr_key_defvalueA
319  */
320 static LONG register_key_defvalueA(
321     HKEY base,
322     WCHAR const *name,
323     char const *value)
324 {
325     LONG res;
326     HKEY key;
327
328     res = RegCreateKeyExW(base, name, 0, NULL, 0,
329                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
330     if (res != ERROR_SUCCESS) return res;
331     res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
332                          lstrlenA(value) + 1);
333     RegCloseKey(key);
334     return res;
335 }
336
337 /***********************************************************************
338  *              coclass list
339  */
340 static struct regsvr_coclass const coclass_list[] = {
341     {   &CLSID_DOMDocument60,
342         "XML DOM Document 6.0",
343         NULL,
344         "msxml6.dll",
345         "Both",
346         "Msxml2.DOMDocument",
347         "6.0"
348     },
349     {   &CLSID_XMLSchemaCache60,
350     "XML Schema Cache 6.0",
351     NULL,
352     "msxml6.dll",
353     "Both",
354     "Msxml2.XMLSchemaCache",
355     "6.0"
356     },
357     {   &CLSID_MXXMLWriter60,
358     "IMXWriter interface 6.0",
359     NULL,
360     "msxml6.dll",
361     "Both",
362     "Msxml2.MXXMLWriter",
363     "6.0"
364     },
365     {   &CLSID_SAXAttributes60,
366     "SAX Attribute 6.0",
367     NULL,
368     "msxml6.dll",
369     "Both",
370     "Msxml2.SAXAttributes",
371     "6.0"
372     },
373     {   &CLSID_FreeThreadedDOMDocument60,
374     "Free Threaded XML DOM Document 6.0",
375     NULL,
376     "msxml6.dll",
377     "Both",
378     "Microsoft.FreeThreadedDOMDocument6.0",
379     "6.0"
380     },
381     { NULL }                    /* list terminator */
382 };
383
384 /***********************************************************************
385  *              progid list
386  */
387 static struct progid const progid_list[] = {
388     {   "Msxml2.DOMDocument.6.0",
389         "XML DOM Document 6.0",
390         &CLSID_DOMDocument60,
391         NULL
392     },
393     {   "Msxml2.XMLSchemaCache.6.0",
394     "XML Schema Cache 6.0",
395     &CLSID_XMLSchemaCache60,
396     NULL
397     },
398     {   "Msxml2.MXXMLWriter.6.0",
399     "MXXMLWriter 6.0",
400     &CLSID_MXXMLWriter60,
401     NULL
402     },
403     {   "Msxml2.SAXAttributes.6.0",
404     "SAX Attribute 6.0",
405     &CLSID_SAXAttributes60,
406     NULL
407     },
408     {   "MSXML.FreeThreadedDOMDocument60",
409     "Free threaded XML DOM Document 6.0",
410     &CLSID_FreeThreadedDOMDocument60,
411     NULL
412     },
413     { NULL }                    /* list terminator */
414 };
415
416 /***********************************************************************
417  *              DllRegisterServer (MSXML4.@)
418  */
419 HRESULT WINAPI DllRegisterServer(void)
420 {
421     HRESULT hr;
422
423     TRACE("\n");
424
425     hr = register_coclasses(coclass_list);
426     if (SUCCEEDED(hr))
427         hr = register_progids(progid_list);
428
429     return hr;
430 }
431
432 /***********************************************************************
433  *              DllUnregisterServer (MSXML4.@)
434  */
435 HRESULT WINAPI DllUnregisterServer(void)
436 {
437     HRESULT hr;
438
439     TRACE("\n");
440
441     hr = unregister_coclasses(coclass_list);
442     if (SUCCEEDED(hr))
443         hr = unregister_progids(progid_list);
444
445     return hr;
446 }