oledb32: Fix IDBProperties::GetProperties test on Windows.
[wine] / dlls / oledb32 / regsvr.c
1 /*
2  * self-registerable dll functions for oledb32.dll
3  *
4  * Copyright (C) 2004 Raphael Junqueira
5  *               2009 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 "msdaguid.h"
40
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
45
46 /*
47  * Near the bottom of this file are the exported DllRegisterServer and
48  * DllUnregisterServer, which make all this worthwhile.
49  */
50
51 /***********************************************************************
52  *                interface for self-registering
53  */
54 struct regsvr_interface
55 {
56     IID const *iid;             /* NULL for end of list */
57     LPCSTR name;                /* can be NULL to omit */
58     IID const *base_iid;        /* can be NULL to omit */
59     int num_methods;            /* can be <0 to omit */
60     CLSID const *ps_clsid;      /* can be NULL to omit */
61     CLSID const *ps_clsid32;    /* can be NULL to omit */
62 };
63
64 static HRESULT register_interfaces(struct regsvr_interface const *list);
65 static HRESULT unregister_interfaces(struct regsvr_interface const *list);
66
67 struct regsvr_coclass
68 {
69     CLSID const *clsid;         /* NULL for end of list */
70     LPCSTR name;                /* can be NULL to omit */
71     LPCSTR iph32;               /* can be NULL to omit */
72     LPCSTR ips;                 /* can be NULL to omit */
73     LPCSTR ips32;               /* can be NULL to omit */
74     LPCSTR ips32_tmodel;        /* can be NULL to omit, if apartment, iph32 must be set */
75     DWORD flags;                /* set defines below */
76     LPCSTR progid;              /* can be NULL to omit */
77     LPCSTR viprogid;            /* can be NULL to omit */
78     LPCSTR progid_extra;        /* can be NULL to omit */
79     LPCSTR dllversion;          /* can be NULL to omit */
80 };
81
82 /* flags for regsvr_coclass.flags */
83 #define PROGID_CLSID                  0x00000010  /* add a clsid key to the progid */
84
85 static HRESULT register_coclasses(struct regsvr_coclass const *list);
86 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
87
88 /***********************************************************************
89  *                static string constants
90  */
91 static WCHAR const interface_keyname[10] = {
92     'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
93 static WCHAR const base_ifa_keyname[14] = {
94     'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
95     'e', 0 };
96 static WCHAR const num_methods_keyname[11] = {
97     'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
98 static WCHAR const ps_clsid_keyname[15] = {
99     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
100     'i', 'd', 0 };
101 static WCHAR const ps_clsid32_keyname[17] = {
102     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
103     'i', 'd', '3', '2', 0 };
104 static WCHAR const clsid_keyname[6] = {
105     'C', 'L', 'S', 'I', 'D', 0 };
106 static WCHAR const curver_keyname[7] = {
107     'C', 'u', 'r', 'V', 'e', 'r', 0 };
108 static WCHAR const iph32_keyname[] = {
109     'I', 'n', 'P', 'r', 'o', 'c', 'H', 'a', 'n', 'd', 'l', 'e', 'r',
110     '3', '2', 0 };
111 static WCHAR const ips_keyname[13] = {
112     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
113     0 };
114 static WCHAR const ips32_keyname[15] = {
115     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
116     '3', '2', 0 };
117 static WCHAR const progid_keyname[7] = {
118     'P', 'r', 'o', 'g', 'I', 'D', 0 };
119 static WCHAR const viprogid_keyname[25] = {
120     'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
121     'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
122     0 };
123 static WCHAR const dllversion_keyname[11] = {
124     'D', 'l', 'l', 'V', 'e', 'r', 's', 'i', 'o', 'n', 0 };
125 static char const tmodel_valuename[] = "ThreadingModel";
126
127 /***********************************************************************
128  *                static helper functions
129  */
130 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
131 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
132                                    WCHAR const *value);
133 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
134                                    char const *value);
135 static LONG register_progid(WCHAR const *clsid,
136                             char const *progid, char const *curver_progid,
137                             char const *name, char const *extra);
138
139 /***********************************************************************
140  *                register_interfaces
141  */
142 static HRESULT register_interfaces(struct regsvr_interface const *list)
143 {
144     LONG res = ERROR_SUCCESS;
145     HKEY interface_key;
146
147     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
148                           KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
149     if (res != ERROR_SUCCESS) goto error_return;
150
151     for (; res == ERROR_SUCCESS && list->iid; ++list)
152     {
153         WCHAR buf[39];
154         HKEY iid_key;
155
156         StringFromGUID2(list->iid, buf, 39);
157         res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
158                               KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
159         if (res != ERROR_SUCCESS) goto error_close_interface_key;
160
161         if (list->name)
162         {
163             res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
164                                  (CONST BYTE*)(list->name),
165                                  strlen(list->name) + 1);
166             if (res != ERROR_SUCCESS) goto error_close_iid_key;
167         }
168
169         if (list->base_iid)
170         {
171             res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
172             if (res != ERROR_SUCCESS) goto error_close_iid_key;
173         }
174
175         if (0 <= list->num_methods)
176         {
177             static WCHAR const fmt[3] = { '%', 'd', 0 };
178             HKEY key;
179
180             res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
181                                   KEY_READ | KEY_WRITE, NULL, &key, NULL);
182             if (res != ERROR_SUCCESS) goto error_close_iid_key;
183
184             sprintfW(buf, fmt, list->num_methods);
185             res = RegSetValueExW(key, NULL, 0, REG_SZ,
186                                  (CONST BYTE*)buf,
187                                  (lstrlenW(buf) + 1) * sizeof(WCHAR));
188             RegCloseKey(key);
189
190             if (res != ERROR_SUCCESS) goto error_close_iid_key;
191         }
192
193         if (list->ps_clsid)
194         {
195             res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
196             if (res != ERROR_SUCCESS) goto error_close_iid_key;
197         }
198
199         if (list->ps_clsid32)
200         {
201             res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
202             if (res != ERROR_SUCCESS) goto error_close_iid_key;
203         }
204
205     error_close_iid_key:
206         RegCloseKey(iid_key);
207     }
208
209 error_close_interface_key:
210     RegCloseKey(interface_key);
211 error_return:
212     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
213 }
214
215 /***********************************************************************
216  *                unregister_interfaces
217  */
218 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
219 {
220     LONG res = ERROR_SUCCESS;
221     HKEY interface_key;
222
223     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
224                         KEY_READ | KEY_WRITE, &interface_key);
225     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
226     if (res != ERROR_SUCCESS) goto error_return;
227
228     for (; res == ERROR_SUCCESS && list->iid; ++list)
229     {
230         WCHAR buf[39];
231
232         StringFromGUID2(list->iid, buf, 39);
233         res = RegDeleteTreeW(interface_key, buf);
234         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
235     }
236
237     RegCloseKey(interface_key);
238 error_return:
239     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
240 }
241
242 /***********************************************************************
243  *                register_coclasses
244  */
245 static HRESULT register_coclasses(struct regsvr_coclass const *list)
246 {
247     LONG res = ERROR_SUCCESS;
248     HKEY coclass_key;
249
250     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
251                           KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
252     if (res != ERROR_SUCCESS) goto error_return;
253
254     for (; res == ERROR_SUCCESS && list->clsid; ++list)
255     {
256         WCHAR buf[39];
257         HKEY clsid_key;
258
259         StringFromGUID2(list->clsid, buf, 39);
260         res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
261                               KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
262         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
263
264         if (list->name)
265         {
266             res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
267                                  (CONST BYTE*)(list->name),
268                                  strlen(list->name) + 1);
269             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
270         }
271
272         if (list->iph32)
273         {
274             HKEY iph32_key;
275
276             res = RegCreateKeyExW(clsid_key, iph32_keyname, 0, NULL, 0,
277                                   KEY_READ | KEY_WRITE, NULL,
278                                   &iph32_key, NULL);
279             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
280
281             res = RegSetValueExA(iph32_key, NULL, 0, REG_SZ,
282                                  (CONST BYTE*)list->iph32,
283                                  lstrlenA(list->iph32) + 1);
284             RegCloseKey(iph32_key);
285             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
286         }
287
288         if (list->ips)
289         {
290             res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
291             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
292         }
293
294         if (list->ips32)
295         {
296             HKEY ips32_key;
297
298             res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
299                                   KEY_READ | KEY_WRITE, NULL,
300                                   &ips32_key, NULL);
301             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
302
303             res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
304                                  (CONST BYTE*)list->ips32,
305                                  lstrlenA(list->ips32) + 1);
306             if (res == ERROR_SUCCESS && list->ips32_tmodel)
307                 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
308                                      (CONST BYTE*)list->ips32_tmodel,
309                                      strlen(list->ips32_tmodel) + 1);
310             RegCloseKey(ips32_key);
311             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
312         }
313
314         if (list->progid)
315         {
316             res = register_key_defvalueA(clsid_key, progid_keyname,
317                                          list->progid);
318             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
319
320             res = register_progid(list->flags & PROGID_CLSID ? buf : NULL,
321                                   list->progid, NULL,
322                                   list->name, list->progid_extra);
323             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
324         }
325
326         if (list->viprogid)
327         {
328             res = register_key_defvalueA(clsid_key, viprogid_keyname,
329                                          list->viprogid);
330             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
331
332             res = register_progid(list->flags & PROGID_CLSID ? buf : NULL,
333                                   list->viprogid, list->progid,
334                                   list->name, list->progid_extra);
335             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
336         }
337
338         if (list->dllversion)
339         {
340             HKEY dllver_key;
341
342             res = RegCreateKeyExW(clsid_key, dllversion_keyname, 0, NULL, 0,
343                                   KEY_READ | KEY_WRITE, NULL,
344                                   &dllver_key, NULL);
345             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
346
347             res = RegSetValueExA(dllver_key, NULL, 0, REG_SZ,
348                                  (CONST BYTE*)list->dllversion,
349                                  lstrlenA(list->dllversion) + 1);
350             RegCloseKey(dllver_key);
351             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
352         }
353
354
355     error_close_clsid_key:
356         RegCloseKey(clsid_key);
357     }
358
359 error_close_coclass_key:
360     RegCloseKey(coclass_key);
361 error_return:
362     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
363 }
364
365 /***********************************************************************
366  *                unregister_coclasses
367  */
368 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
369 {
370     LONG res = ERROR_SUCCESS;
371     HKEY coclass_key;
372
373     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
374                         KEY_READ | KEY_WRITE, &coclass_key);
375     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
376     if (res != ERROR_SUCCESS) goto error_return;
377
378     for (; res == ERROR_SUCCESS && list->clsid; ++list)
379     {
380         WCHAR buf[39];
381
382         StringFromGUID2(list->clsid, buf, 39);
383         res = RegDeleteTreeW(coclass_key, buf);
384         if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
385         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
386
387         if (list->progid)
388         {
389             res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->progid);
390             if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
391             if (res != ERROR_SUCCESS) goto error_close_coclass_key;
392         }
393
394         if (list->viprogid)
395         {
396             res = RegDeleteTreeA(HKEY_CLASSES_ROOT, list->viprogid);
397             if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
398             if (res != ERROR_SUCCESS) goto error_close_coclass_key;
399         }
400     }
401
402 error_close_coclass_key:
403     RegCloseKey(coclass_key);
404 error_return:
405     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
406 }
407
408 /***********************************************************************
409  *                register_key_guid
410  */
411 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
412 {
413     WCHAR buf[39];
414
415     StringFromGUID2(guid, buf, 39);
416     return register_key_defvalueW(base, name, buf);
417 }
418
419 /***********************************************************************
420  *                register_key_defvalueW
421  */
422 static LONG register_key_defvalueW(HKEY base, WCHAR const *name, WCHAR const *value)
423 {
424     LONG res;
425     HKEY key;
426
427     res = RegCreateKeyExW(base, name, 0, NULL, 0,
428                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
429     if (res != ERROR_SUCCESS) return res;
430     res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
431                          (lstrlenW(value) + 1) * sizeof(WCHAR));
432     RegCloseKey(key);
433     return res;
434 }
435
436 /***********************************************************************
437  *                register_key_defvalueA
438  */
439 static LONG register_key_defvalueA(HKEY base, WCHAR const *name, char const *value)
440 {
441     LONG res;
442     HKEY key;
443
444     res = RegCreateKeyExW(base, name, 0, NULL, 0,
445                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
446     if (res != ERROR_SUCCESS) return res;
447     res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
448                          lstrlenA(value) + 1);
449     RegCloseKey(key);
450     return res;
451 }
452
453 /***********************************************************************
454  *                register_progid
455  */
456 static LONG register_progid(WCHAR const *clsid, char const *progid,
457                             char const *curver_progid, char const *name,
458                             char const *extra)
459 {
460     LONG res;
461     HKEY progid_key;
462
463     res = RegCreateKeyExA(HKEY_CLASSES_ROOT, progid, 0,
464                           NULL, 0, KEY_READ | KEY_WRITE, NULL,
465                           &progid_key, NULL);
466     if (res != ERROR_SUCCESS) return res;
467
468     if (name)
469     {
470         res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
471                              (CONST BYTE*)name, strlen(name) + 1);
472         if (res != ERROR_SUCCESS) goto error_close_progid_key;
473     }
474
475     if (clsid)
476     {
477         res = register_key_defvalueW(progid_key, clsid_keyname, clsid);
478         if (res != ERROR_SUCCESS) goto error_close_progid_key;
479     }
480
481     if (curver_progid)
482     {
483         res = register_key_defvalueA(progid_key, curver_keyname, curver_progid);
484         if (res != ERROR_SUCCESS) goto error_close_progid_key;
485     }
486
487     if (extra)
488     {
489         HKEY extra_key;
490
491         res = RegCreateKeyExA(progid_key, extra, 0,
492                               NULL, 0, KEY_READ | KEY_WRITE, NULL,
493                               &extra_key, NULL);
494         if (res == ERROR_SUCCESS)
495             RegCloseKey(extra_key);
496     }
497
498 error_close_progid_key:
499     RegCloseKey(progid_key);
500     return res;
501 }
502
503 /***********************************************************************
504  *                coclass list
505  */
506 static struct regsvr_coclass const coclass_list[] =
507 {
508     {
509         &CLSID_OLEDB_CONVERSIONLIBRARY,
510         "OLE DB Data Conversion Library",
511         NULL,
512         NULL,
513         "oledb32.dll",
514         "Both",
515         PROGID_CLSID,
516         "MSDADC.1",
517         "MSDADC",
518         NULL,
519         NULL
520     },
521     { NULL }                        /* list terminator */
522 };
523
524 /***********************************************************************
525  *                interface list
526  */
527 static struct regsvr_interface const interface_list[] =
528 {
529     { NULL } /* list terminator */
530 };
531
532 /***********************************************************************
533  *                DllRegisterServer
534  */
535 HRESULT WINAPI DllRegisterServer(void)
536 {
537     HRESULT hr;
538
539     TRACE("\n");
540
541     hr = register_coclasses(coclass_list);
542     if (SUCCEEDED(hr))
543         hr = register_interfaces(interface_list);
544     return hr;
545 }
546
547 /***********************************************************************
548  *                DllUnregisterServer
549  */
550 HRESULT WINAPI DllUnregisterServer(void)
551 {
552     HRESULT hr;
553
554     TRACE("\n");
555
556     hr = unregister_coclasses(coclass_list);
557     if (SUCCEEDED(hr))
558         hr = unregister_interfaces(interface_list);
559     return hr;
560 }