d3dxof: Do not expect a separator when there is no element.
[wine] / dlls / msdaps / regsvr.c
1 /*
2  * self-registerable dll functions for msdaps.dll
3  *
4  * Copyright (C) 2004 Raphael Junqueira
5  *               2010 Huw Davies
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 #define COBJMACROS
23
24 #include "config.h"
25
26 #include <stdarg.h>
27 #include <string.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "winreg.h"
33 #include "winerror.h"
34
35 #include "ole2.h"
36 #include "olectl.h"
37 #include "oleauto.h"
38
39 #include "row_server.h"
40
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
45
46 extern HRESULT WINAPI msdaps_DllRegisterServer(void) DECLSPEC_HIDDEN;
47 extern HRESULT WINAPI msdaps_DllUnregisterServer(void) DECLSPEC_HIDDEN;
48
49
50 /***********************************************************************
51  *                interface for self-registering
52  */
53 struct regsvr_interface
54 {
55     IID const *iid;             /* NULL for end of list */
56     LPCSTR name;                /* can be NULL to omit */
57     IID const *base_iid;        /* can be NULL to omit */
58     int num_methods;            /* can be <0 to omit */
59     CLSID const *ps_clsid;      /* can be NULL to omit */
60     CLSID const *ps_clsid32;    /* can be NULL to omit */
61 };
62
63 static HRESULT register_interfaces(struct regsvr_interface const *list);
64 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
65
66 struct regsvr_coclass
67 {
68     CLSID const *clsid;         /* NULL for end of list */
69     LPCSTR name;                /* can be NULL to omit */
70     LPCSTR iph32;               /* can be NULL to omit */
71     LPCSTR ips;                 /* can be NULL to omit */
72     LPCSTR ips32;               /* can be NULL to omit */
73     LPCSTR ips32_tmodel;        /* can be NULL to omit, if apartment, iph32 must be set */
74     DWORD flags;                /* set defines below */
75     LPCSTR progid;              /* can be NULL to omit */
76     LPCSTR viprogid;            /* can be NULL to omit */
77     LPCSTR progid_extra;        /* can be NULL to omit */
78     LPCSTR dllversion;          /* can be NULL to omit */
79 };
80
81 /* flags for regsvr_coclass.flags */
82 #define PROGID_CLSID                  0x00000010  /* add a clsid key to the progid */
83
84 static HRESULT register_coclasses(struct regsvr_coclass const *list);
85 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
86
87 /***********************************************************************
88  *                static string constants
89  */
90 static WCHAR const interface_keyname[10] = {
91     'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
92 static WCHAR const base_ifa_keyname[14] = {
93     'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
94     'e', 0 };
95 static WCHAR const num_methods_keyname[11] = {
96     'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
97 static WCHAR const ps_clsid_keyname[15] = {
98     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
99     'i', 'd', 0 };
100 static WCHAR const ps_clsid32_keyname[17] = {
101     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
102     'i', 'd', '3', '2', 0 };
103 static WCHAR const clsid_keyname[6] = {
104     'C', 'L', 'S', 'I', 'D', 0 };
105 static WCHAR const curver_keyname[7] = {
106     'C', 'u', 'r', 'V', 'e', 'r', 0 };
107 static WCHAR const iph32_keyname[] = {
108     'I', 'n', 'P', 'r', 'o', 'c', 'H', 'a', 'n', 'd', 'l', 'e', 'r',
109     '3', '2', 0 };
110 static WCHAR const ips_keyname[13] = {
111     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
112     0 };
113 static WCHAR const ips32_keyname[15] = {
114     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
115     '3', '2', 0 };
116 static WCHAR const progid_keyname[7] = {
117     'P', 'r', 'o', 'g', 'I', 'D', 0 };
118 static WCHAR const viprogid_keyname[25] = {
119     'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
120     'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
121     0 };
122 static WCHAR const dllversion_keyname[11] = {
123     'D', 'l', 'l', 'V', 'e', 'r', 's', 'i', 'o', 'n', 0 };
124 static char const tmodel_valuename[] = "ThreadingModel";
125
126 /***********************************************************************
127  *                static helper functions
128  */
129 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
130 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
131                                    WCHAR const *value);
132 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
133                                    char const *value);
134 static LONG register_progid(WCHAR const *clsid,
135                             char const *progid, char const *curver_progid,
136                             char const *name, char const *extra);
137
138 /***********************************************************************
139  *                register_interfaces
140  */
141 static HRESULT register_interfaces(struct regsvr_interface const *list)
142 {
143     LONG res = ERROR_SUCCESS;
144     HKEY interface_key;
145
146     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
147                           KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
148     if (res != ERROR_SUCCESS) goto error_return;
149
150     for (; res == ERROR_SUCCESS && list->iid; ++list)
151     {
152         WCHAR buf[39];
153         HKEY iid_key;
154
155         StringFromGUID2(list->iid, buf, 39);
156         res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
157                               KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
158         if (res != ERROR_SUCCESS) goto error_close_interface_key;
159
160         if (list->name)
161         {
162             res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
163                                  (CONST BYTE*)(list->name),
164                                  strlen(list->name) + 1);
165             if (res != ERROR_SUCCESS) goto error_close_iid_key;
166         }
167
168         if (list->base_iid)
169         {
170             res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
171             if (res != ERROR_SUCCESS) goto error_close_iid_key;
172         }
173
174         if (0 <= list->num_methods)
175         {
176             static WCHAR const fmt[3] = { '%', 'd', 0 };
177             HKEY key;
178
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;
182
183             sprintfW(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);
188
189             if (res != ERROR_SUCCESS) goto error_close_iid_key;
190         }
191
192         if (list->ps_clsid)
193         {
194             res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
195             if (res != ERROR_SUCCESS) goto error_close_iid_key;
196         }
197
198         if (list->ps_clsid32)
199         {
200             res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
201             if (res != ERROR_SUCCESS) goto error_close_iid_key;
202         }
203
204     error_close_iid_key:
205         RegCloseKey(iid_key);
206     }
207
208 error_close_interface_key:
209     RegCloseKey(interface_key);
210 error_return:
211     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
212 }
213
214 /***********************************************************************
215  *                unregister_interfaces
216  */
217 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
218 {
219     LONG res = ERROR_SUCCESS;
220     HKEY interface_key;
221
222     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
223                         KEY_READ | KEY_WRITE, &interface_key);
224     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
225     if (res != ERROR_SUCCESS) goto error_return;
226
227     for (; res == ERROR_SUCCESS && list->iid; ++list)
228     {
229         WCHAR buf[39];
230
231         StringFromGUID2(list->iid, buf, 39);
232         res = RegDeleteTreeW(interface_key, buf);
233         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
234     }
235
236     RegCloseKey(interface_key);
237 error_return:
238     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
239 }
240
241 /***********************************************************************
242  *                register_coclasses
243  */
244 static HRESULT register_coclasses(struct regsvr_coclass const *list)
245 {
246     LONG res = ERROR_SUCCESS;
247     HKEY coclass_key;
248
249     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
250                           KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
251     if (res != ERROR_SUCCESS) goto error_return;
252
253     for (; res == ERROR_SUCCESS && list->clsid; ++list)
254     {
255         WCHAR buf[39];
256         HKEY clsid_key;
257
258         StringFromGUID2(list->clsid, buf, 39);
259         res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
260                               KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
261         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
262
263         if (list->name)
264         {
265             res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
266                                  (CONST BYTE*)(list->name),
267                                  strlen(list->name) + 1);
268             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
269         }
270
271         if (list->iph32)
272         {
273             HKEY iph32_key;
274
275             res = RegCreateKeyExW(clsid_key, iph32_keyname, 0, NULL, 0,
276                                   KEY_READ | KEY_WRITE, NULL,
277                                   &iph32_key, NULL);
278             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
279
280             res = RegSetValueExA(iph32_key, NULL, 0, REG_SZ,
281                                  (CONST BYTE*)list->iph32,
282                                  lstrlenA(list->iph32) + 1);
283             RegCloseKey(iph32_key);
284             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
285         }
286
287         if (list->ips)
288         {
289             res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
290             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
291         }
292
293         if (list->ips32)
294         {
295             HKEY ips32_key;
296
297             res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
298                                   KEY_READ | KEY_WRITE, NULL,
299                                   &ips32_key, NULL);
300             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
301
302             res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
303                                  (CONST BYTE*)list->ips32,
304                                  lstrlenA(list->ips32) + 1);
305             if (res == ERROR_SUCCESS && list->ips32_tmodel)
306                 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
307                                      (CONST BYTE*)list->ips32_tmodel,
308                                      strlen(list->ips32_tmodel) + 1);
309             RegCloseKey(ips32_key);
310             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
311         }
312
313         if (list->progid)
314         {
315             res = register_key_defvalueA(clsid_key, progid_keyname,
316                                          list->progid);
317             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
318
319             res = register_progid(list->flags & PROGID_CLSID ? buf : NULL,
320                                   list->progid, NULL,
321                                   list->name, list->progid_extra);
322             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
323         }
324
325         if (list->viprogid)
326         {
327             res = register_key_defvalueA(clsid_key, viprogid_keyname,
328                                          list->viprogid);
329             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
330
331             res = register_progid(list->flags & PROGID_CLSID ? buf : NULL,
332                                   list->viprogid, list->progid,
333                                   list->name, list->progid_extra);
334             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
335         }
336
337         if (list->dllversion)
338         {
339             HKEY dllver_key;
340
341             res = RegCreateKeyExW(clsid_key, dllversion_keyname, 0, NULL, 0,
342                                   KEY_READ | KEY_WRITE, NULL,
343                                   &dllver_key, NULL);
344             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
345
346             res = RegSetValueExA(dllver_key, NULL, 0, REG_SZ,
347                                  (CONST BYTE*)list->dllversion,
348                                  lstrlenA(list->dllversion) + 1);
349             RegCloseKey(dllver_key);
350             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
351         }
352
353
354     error_close_clsid_key:
355         RegCloseKey(clsid_key);
356     }
357
358 error_close_coclass_key:
359     RegCloseKey(coclass_key);
360 error_return:
361     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
362 }
363
364 /***********************************************************************
365  *                unregister_coclasses
366  */
367 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
368 {
369     LONG res = ERROR_SUCCESS;
370     HKEY coclass_key;
371
372     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
373                         KEY_READ | KEY_WRITE, &coclass_key);
374     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
375     if (res != ERROR_SUCCESS) goto error_return;
376
377     for (; res == ERROR_SUCCESS && list->clsid; ++list)
378     {
379         WCHAR buf[39];
380
381         StringFromGUID2(list->clsid, buf, 39);
382         res = RegDeleteTreeW(coclass_key, buf);
383         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
384         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
385
386         if (list->progid)
387         {
388             res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->progid);
389             if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
390             if (res != ERROR_SUCCESS) goto error_close_coclass_key;
391         }
392
393         if (list->viprogid)
394         {
395             res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->viprogid);
396             if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
397             if (res != ERROR_SUCCESS) goto error_close_coclass_key;
398         }
399     }
400
401 error_close_coclass_key:
402     RegCloseKey(coclass_key);
403 error_return:
404     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
405 }
406
407 /***********************************************************************
408  *                register_key_guid
409  */
410 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
411 {
412     WCHAR buf[39];
413
414     StringFromGUID2(guid, buf, 39);
415     return register_key_defvalueW(base, name, buf);
416 }
417
418 /***********************************************************************
419  *                register_key_defvalueW
420  */
421 static LONG register_key_defvalueW(HKEY base, WCHAR const *name, WCHAR const *value)
422 {
423     LONG res;
424     HKEY key;
425
426     res = RegCreateKeyExW(base, name, 0, NULL, 0,
427                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
428     if (res != ERROR_SUCCESS) return res;
429     res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
430                          (lstrlenW(value) + 1) * sizeof(WCHAR));
431     RegCloseKey(key);
432     return res;
433 }
434
435 /***********************************************************************
436  *                register_key_defvalueA
437  */
438 static LONG register_key_defvalueA(HKEY base, WCHAR const *name, char const *value)
439 {
440     LONG res;
441     HKEY key;
442
443     res = RegCreateKeyExW(base, name, 0, NULL, 0,
444                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
445     if (res != ERROR_SUCCESS) return res;
446     res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
447                          lstrlenA(value) + 1);
448     RegCloseKey(key);
449     return res;
450 }
451
452 /***********************************************************************
453  *                register_progid
454  */
455 static LONG register_progid(WCHAR const *clsid, char const *progid,
456                             char const *curver_progid, char const *name,
457                             char const *extra)
458 {
459     LONG res;
460     HKEY progid_key;
461
462     res = RegCreateKeyExA(HKEY_CLASSES_ROOT, progid, 0,
463                           NULL, 0, KEY_READ | KEY_WRITE, NULL,
464                           &progid_key, NULL);
465     if (res != ERROR_SUCCESS) return res;
466
467     if (name)
468     {
469         res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
470                              (CONST BYTE*)name, strlen(name) + 1);
471         if (res != ERROR_SUCCESS) goto error_close_progid_key;
472     }
473
474     if (clsid)
475     {
476         res = register_key_defvalueW(progid_key, clsid_keyname, clsid);
477         if (res != ERROR_SUCCESS) goto error_close_progid_key;
478     }
479
480     if (curver_progid)
481     {
482         res = register_key_defvalueA(progid_key, curver_keyname, curver_progid);
483         if (res != ERROR_SUCCESS) goto error_close_progid_key;
484     }
485
486     if (extra)
487     {
488         HKEY extra_key;
489
490         res = RegCreateKeyExA(progid_key, extra, 0,
491                               NULL, 0, KEY_READ | KEY_WRITE, NULL,
492                               &extra_key, NULL);
493         if (res == ERROR_SUCCESS)
494             RegCloseKey(extra_key);
495     }
496
497 error_close_progid_key:
498     RegCloseKey(progid_key);
499     return res;
500 }
501
502 /***********************************************************************
503  *                coclass list
504  */
505 static struct regsvr_coclass const coclass_list[] =
506 {
507     {
508         &CLSID_wine_row_proxy,
509         "Wine OLE DB Row Proxy",
510         NULL,
511         NULL,
512         "msdaps.dll",
513         "Both",
514         PROGID_CLSID,
515         "WINEDBROWPRX.AsProxy.1",
516         "WINEDBROWPRX.AsProxy",
517         NULL,
518         NULL
519     },
520     {
521         &CLSID_wine_row_server,
522         "Wine OLE DB Row Server",
523         NULL,
524         NULL,
525         "msdaps.dll",
526         "Both",
527         PROGID_CLSID,
528         "WINEDBROWPRX.AsServer.1",
529         "WINEDBROWPRX.AsServer",
530         NULL,
531         NULL
532     },
533     {
534         &CLSID_wine_rowset_proxy,
535         "Wine OLE DB Rowset Proxy",
536         NULL,
537         NULL,
538         "msdaps.dll",
539         "Both",
540         PROGID_CLSID,
541         "WINEDBRSTPRX.AsProxy.1",
542         "WINEDBRSTPRX.AsProxy",
543         NULL,
544         NULL
545     },
546     {
547         &CLSID_wine_rowset_server,
548         "Wine OLE DB Rowset Server",
549         NULL,
550         NULL,
551         "msdaps.dll",
552         "Both",
553         PROGID_CLSID,
554         "WINEDBRSTPRX.AsServer.1",
555         "WINEDBRSTPRX.AsServer",
556         NULL,
557         NULL
558     },
559     { NULL }                        /* list terminator */
560 };
561
562 /***********************************************************************
563  *                interface list
564  */
565 static struct regsvr_interface const interface_list[] =
566 {
567     { NULL } /* list terminator */
568 };
569
570 /***********************************************************************
571  *                DllRegisterServer
572  */
573 HRESULT WINAPI DllRegisterServer(void)
574 {
575     HRESULT hr;
576
577     TRACE("\n");
578
579     hr = register_coclasses(coclass_list);
580
581     if (SUCCEEDED(hr))
582         hr = register_interfaces(interface_list);
583
584     if (SUCCEEDED(hr))
585         hr = msdaps_DllRegisterServer();
586
587     return hr;
588 }
589
590 /***********************************************************************
591  *                DllUnregisterServer
592  */
593 HRESULT WINAPI DllUnregisterServer(void)
594 {
595     HRESULT hr;
596
597     TRACE("\n");
598
599     hr = unregister_coclasses(coclass_list);
600
601     if (SUCCEEDED(hr))
602         hr = unregister_interfaces(interface_list);
603
604     if (SUCCEEDED(hr))
605         hr = msdaps_DllUnregisterServer();
606
607     return hr;
608 }