Warn the user if he is running the test in interactive mode but the
[wine] / dlls / mlang / regsvr.c
1 /*
2  *      self-registerable dll functions for mlang.dll
3  *
4  * Copyright (C) 2003 John K. Hohm
5  * Copyright (C) 2004 Steven Edwards for ReactOS
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include <stdarg.h>
23 #include <string.h>
24
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "wingdi.h"
29 #include "winreg.h"
30 #include "winerror.h"
31
32 #include "objbase.h"
33 #include "initguid.h"
34
35 #include "wine/debug.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mlang);
38
39 DEFINE_GUID( CLSID_MLANGSupport, 0x275c23e2,0x3747,0x11d0,0x9f,0xea,0x00,0xaa,0x00,0x3f,0x86,0x46 );
40 DEFINE_GUID( CLSID_MLANGString, 0x44ec053a,0xb70d,0x11d0,0xb1,0x88,0x00,0xaa,0x00,0x38,0xc9,0x69 );
41 DEFINE_GUID( CLSID_MLANGCharSet, 0xd66d6f99,0xcdaa,0x11d0,0xb8,0x22,0x00,0xC0,0x4f,0xc9,0xb3,0x1f );
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 progid;              /* can be NULL to omit */
72     LPCSTR viprogid;            /* can be NULL to omit */
73     LPCSTR progid_extra;        /* can be NULL to omit */
74 };
75
76 static HRESULT register_coclasses(struct regsvr_coclass const *list);
77 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
78
79 /***********************************************************************
80  *              static string constants
81  */
82 static WCHAR const interface_keyname[10] = {
83     'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
84 static WCHAR const base_ifa_keyname[14] = {
85     'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
86     'e', 0 };
87 static WCHAR const num_methods_keyname[11] = {
88     'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
89 static WCHAR const ps_clsid_keyname[15] = {
90     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
91     'i', 'd', 0 };
92 static WCHAR const ps_clsid32_keyname[17] = {
93     'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
94     'i', 'd', '3', '2', 0 };
95 static WCHAR const clsid_keyname[6] = {
96     'C', 'L', 'S', 'I', 'D', 0 };
97 static WCHAR const curver_keyname[7] = {
98     'C', 'u', 'r', 'V', 'e', 'r', 0 };
99 static WCHAR const ips_keyname[13] = {
100     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
101     0 };
102 static WCHAR const ips32_keyname[15] = {
103     'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
104     '3', '2', 0 };
105 static WCHAR const progid_keyname[7] = {
106     'P', 'r', 'o', 'g', 'I', 'D', 0 };
107 static WCHAR const viprogid_keyname[25] = {
108     'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
109     'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
110     0 };
111 static char const tmodel_valuename[] = "ThreadingModel";
112
113 /***********************************************************************
114  *              static helper functions
115  */
116 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
117 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
118                                    WCHAR const *value);
119 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
120                                    char const *value);
121 static LONG register_progid(WCHAR const *clsid,
122                             char const *progid, char const *curver_progid,
123                             char const *name, char const *extra);
124 static LONG recursive_delete_key(HKEY key);
125 static LONG recursive_delete_keyA(HKEY base, char const *name);
126 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name);
127
128 /***********************************************************************
129  *              register_interfaces
130  */
131 static HRESULT register_interfaces(struct regsvr_interface const *list)
132 {
133     LONG res = ERROR_SUCCESS;
134     HKEY interface_key;
135
136     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
137                           KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
138     if (res != ERROR_SUCCESS) goto error_return;
139
140     for (; res == ERROR_SUCCESS && list->iid; ++list) {
141         WCHAR buf[39];
142         HKEY iid_key;
143
144         StringFromGUID2(list->iid, buf, 39);
145         res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
146                               KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
147         if (res != ERROR_SUCCESS) goto error_close_interface_key;
148
149         if (list->name) {
150             res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
151                                  (CONST BYTE*)(list->name),
152                                  strlen(list->name) + 1);
153             if (res != ERROR_SUCCESS) goto error_close_iid_key;
154         }
155
156         if (list->base_iid) {
157             register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
158             if (res != ERROR_SUCCESS) goto error_close_iid_key;
159         }
160
161         if (0 <= list->num_methods) {
162             static WCHAR const fmt[3] = { '%', 'd', 0 };
163             HKEY key;
164
165             res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
166                                   KEY_READ | KEY_WRITE, NULL, &key, NULL);
167             if (res != ERROR_SUCCESS) goto error_close_iid_key;
168
169             wsprintfW(buf, fmt, list->num_methods);
170             res = RegSetValueExW(key, NULL, 0, REG_SZ,
171                                  (CONST BYTE*)buf,
172                                  (lstrlenW(buf) + 1) * sizeof(WCHAR));
173             RegCloseKey(key);
174
175             if (res != ERROR_SUCCESS) goto error_close_iid_key;
176         }
177
178         if (list->ps_clsid) {
179             register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
180             if (res != ERROR_SUCCESS) goto error_close_iid_key;
181         }
182
183         if (list->ps_clsid32) {
184             register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
185             if (res != ERROR_SUCCESS) goto error_close_iid_key;
186         }
187
188     error_close_iid_key:
189         RegCloseKey(iid_key);
190     }
191
192 error_close_interface_key:
193     RegCloseKey(interface_key);
194 error_return:
195     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
196 }
197
198 /***********************************************************************
199  *              unregister_interfaces
200  */
201 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
202 {
203     LONG res = ERROR_SUCCESS;
204     HKEY interface_key;
205
206     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
207                         KEY_READ | KEY_WRITE, &interface_key);
208     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
209     if (res != ERROR_SUCCESS) goto error_return;
210
211     for (; res == ERROR_SUCCESS && list->iid; ++list) {
212         WCHAR buf[39];
213
214         StringFromGUID2(list->iid, buf, 39);
215         res = recursive_delete_keyW(interface_key, buf);
216     }
217
218     RegCloseKey(interface_key);
219 error_return:
220     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
221 }
222
223 /***********************************************************************
224  *              register_coclasses
225  */
226 static HRESULT register_coclasses(struct regsvr_coclass const *list)
227 {
228     LONG res = ERROR_SUCCESS;
229     HKEY coclass_key;
230
231     res = RegCreateKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0, NULL, 0,
232                           KEY_READ | KEY_WRITE, NULL, &coclass_key, NULL);
233     if (res != ERROR_SUCCESS) goto error_return;
234
235     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
236         WCHAR buf[39];
237         HKEY clsid_key;
238
239         StringFromGUID2(list->clsid, buf, 39);
240         res = RegCreateKeyExW(coclass_key, buf, 0, NULL, 0,
241                               KEY_READ | KEY_WRITE, NULL, &clsid_key, NULL);
242         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
243
244         if (list->name) {
245             res = RegSetValueExA(clsid_key, NULL, 0, REG_SZ,
246                                  (CONST BYTE*)(list->name),
247                                  strlen(list->name) + 1);
248             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
249         }
250
251         if (list->ips) {
252             res = register_key_defvalueA(clsid_key, ips_keyname, list->ips);
253             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
254         }
255
256         if (list->ips32) {
257             HKEY ips32_key;
258
259             res = RegCreateKeyExW(clsid_key, ips32_keyname, 0, NULL, 0,
260                                   KEY_READ | KEY_WRITE, NULL,
261                                   &ips32_key, NULL);
262             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
263
264             res = RegSetValueExA(ips32_key, NULL, 0, REG_SZ,
265                                  (CONST BYTE*)list->ips32,
266                                  lstrlenA(list->ips32) + 1);
267             if (res == ERROR_SUCCESS && list->ips32_tmodel)
268                 res = RegSetValueExA(ips32_key, tmodel_valuename, 0, REG_SZ,
269                                      (CONST BYTE*)list->ips32_tmodel,
270                                      strlen(list->ips32_tmodel) + 1);
271             RegCloseKey(ips32_key);
272             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
273         }
274
275         if (list->progid) {
276             res = register_key_defvalueA(clsid_key, progid_keyname,
277                                          list->progid);
278             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
279
280             res = register_progid(buf, list->progid, NULL,
281                                   list->name, list->progid_extra);
282             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
283         }
284
285         if (list->viprogid) {
286             res = register_key_defvalueA(clsid_key, viprogid_keyname,
287                                          list->viprogid);
288             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
289
290             res = register_progid(buf, list->viprogid, list->progid,
291                                   list->name, list->progid_extra);
292             if (res != ERROR_SUCCESS) goto error_close_clsid_key;
293         }
294
295     error_close_clsid_key:
296         RegCloseKey(clsid_key);
297     }
298
299 error_close_coclass_key:
300     RegCloseKey(coclass_key);
301 error_return:
302     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
303 }
304
305 /***********************************************************************
306  *              unregister_coclasses
307  */
308 static HRESULT unregister_coclasses(struct regsvr_coclass const *list)
309 {
310     LONG res = ERROR_SUCCESS;
311     HKEY coclass_key;
312
313     res = RegOpenKeyExW(HKEY_CLASSES_ROOT, clsid_keyname, 0,
314                         KEY_READ | KEY_WRITE, &coclass_key);
315     if (res == ERROR_FILE_NOT_FOUND) return S_OK;
316     if (res != ERROR_SUCCESS) goto error_return;
317
318     for (; res == ERROR_SUCCESS && list->clsid; ++list) {
319         WCHAR buf[39];
320
321         StringFromGUID2(list->clsid, buf, 39);
322         res = recursive_delete_keyW(coclass_key, buf);
323         if (res != ERROR_SUCCESS) goto error_close_coclass_key;
324
325         if (list->progid) {
326             res = recursive_delete_keyA(HKEY_CLASSES_ROOT, list->progid);
327             if (res != ERROR_SUCCESS) goto error_close_coclass_key;
328         }
329
330         if (list->viprogid) {
331             res = recursive_delete_keyA(HKEY_CLASSES_ROOT, list->viprogid);
332             if (res != ERROR_SUCCESS) goto error_close_coclass_key;
333         }
334     }
335
336 error_close_coclass_key:
337     RegCloseKey(coclass_key);
338 error_return:
339     return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
340 }
341
342 /***********************************************************************
343  *              regsvr_key_guid
344  */
345 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
346 {
347     WCHAR buf[39];
348
349     StringFromGUID2(guid, buf, 39);
350     return register_key_defvalueW(base, name, buf);
351 }
352
353 /***********************************************************************
354  *              regsvr_key_defvalueW
355  */
356 static LONG register_key_defvalueW(
357     HKEY base,
358     WCHAR const *name,
359     WCHAR const *value)
360 {
361     LONG res;
362     HKEY key;
363
364     res = RegCreateKeyExW(base, name, 0, NULL, 0,
365                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
366     if (res != ERROR_SUCCESS) return res;
367     res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
368                          (lstrlenW(value) + 1) * sizeof(WCHAR));
369     RegCloseKey(key);
370     return res;
371 }
372
373 /***********************************************************************
374  *              regsvr_key_defvalueA
375  */
376 static LONG register_key_defvalueA(
377     HKEY base,
378     WCHAR const *name,
379     char const *value)
380 {
381     LONG res;
382     HKEY key;
383
384     res = RegCreateKeyExW(base, name, 0, NULL, 0,
385                           KEY_READ | KEY_WRITE, NULL, &key, NULL);
386     if (res != ERROR_SUCCESS) return res;
387     res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
388                          lstrlenA(value) + 1);
389     RegCloseKey(key);
390     return res;
391 }
392
393 /***********************************************************************
394  *              regsvr_progid
395  */
396 static LONG register_progid(
397     WCHAR const *clsid,
398     char const *progid,
399     char const *curver_progid,
400     char const *name,
401     char const *extra)
402 {
403     LONG res;
404     HKEY progid_key;
405
406     res = RegCreateKeyExA(HKEY_CLASSES_ROOT, progid, 0,
407                           NULL, 0, KEY_READ | KEY_WRITE, NULL,
408                           &progid_key, NULL);
409     if (res != ERROR_SUCCESS) return res;
410
411     if (name) {
412         res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
413                              (CONST BYTE*)name, strlen(name) + 1);
414         if (res != ERROR_SUCCESS) goto error_close_progid_key;
415     }
416
417     if (clsid) {
418         res = register_key_defvalueW(progid_key, clsid_keyname, clsid);
419         if (res != ERROR_SUCCESS) goto error_close_progid_key;
420     }
421
422     if (curver_progid) {
423         res = register_key_defvalueA(progid_key, curver_keyname,
424                                      curver_progid);
425         if (res != ERROR_SUCCESS) goto error_close_progid_key;
426     }
427
428     if (extra) {
429         HKEY extra_key;
430
431         res = RegCreateKeyExA(progid_key, extra, 0,
432                               NULL, 0, KEY_READ | KEY_WRITE, NULL,
433                               &extra_key, NULL);
434         if (res == ERROR_SUCCESS)
435             RegCloseKey(extra_key);
436     }
437
438 error_close_progid_key:
439     RegCloseKey(progid_key);
440     return res;
441 }
442
443 /***********************************************************************
444  *              recursive_delete_key
445  */
446 static LONG recursive_delete_key(HKEY key)
447 {
448     LONG res;
449     WCHAR subkey_name[MAX_PATH];
450     DWORD cName;
451     HKEY subkey;
452
453     for (;;) {
454         cName = sizeof(subkey_name) / sizeof(WCHAR);
455         res = RegEnumKeyExW(key, 0, subkey_name, &cName,
456                             NULL, NULL, NULL, NULL);
457         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) {
458             res = ERROR_SUCCESS; /* presumably we're done enumerating */
459             break;
460         }
461         res = RegOpenKeyExW(key, subkey_name, 0,
462                             KEY_READ | KEY_WRITE, &subkey);
463         if (res == ERROR_FILE_NOT_FOUND) continue;
464         if (res != ERROR_SUCCESS) break;
465
466         res = recursive_delete_key(subkey);
467         RegCloseKey(subkey);
468         if (res != ERROR_SUCCESS) break;
469     }
470
471     if (res == ERROR_SUCCESS) res = RegDeleteKeyW(key, 0);
472     return res;
473 }
474
475 /***********************************************************************
476  *              recursive_delete_keyA
477  */
478 static LONG recursive_delete_keyA(HKEY base, char const *name)
479 {
480     LONG res;
481     HKEY key;
482
483     res = RegOpenKeyExA(base, name, 0, KEY_READ | KEY_WRITE, &key);
484     if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
485     if (res != ERROR_SUCCESS) return res;
486     res = recursive_delete_key(key);
487     RegCloseKey(key);
488     return res;
489 }
490
491 /***********************************************************************
492  *              recursive_delete_keyW
493  */
494 static LONG recursive_delete_keyW(HKEY base, WCHAR const *name)
495 {
496     LONG res;
497     HKEY key;
498
499     res = RegOpenKeyExW(base, name, 0, KEY_READ | KEY_WRITE, &key);
500     if (res == ERROR_FILE_NOT_FOUND) return ERROR_SUCCESS;
501     if (res != ERROR_SUCCESS) return res;
502     res = recursive_delete_key(key);
503     RegCloseKey(key);
504     return res;
505 }
506
507 /***********************************************************************
508  *              coclass list
509  */
510 static struct regsvr_coclass const coclass_list[] = {
511     {
512         &CLSID_MLANGSupport,
513         "Multi Language Support",
514         NULL,
515         "mlang.dll",
516         "Both"
517     },
518     {
519         &CLSID_MLANGString,
520         "Multi Language String",
521         NULL,
522         "mlang.dll",
523         "Both"
524     },
525     {
526         &CLSID_MLANGCharSet,
527         "Multi Language ConvertCharset",
528         NULL,
529         "mlang.dll",
530         "Both"
531     },
532     { NULL }    /* list terminator */
533 };
534
535 /***********************************************************************
536  *              interface list
537  */
538
539 static struct regsvr_interface const interface_list[] = {
540     { NULL }                    /* list terminator */
541 };
542
543 /***********************************************************************
544  *              DllRegisterServer (MLANG.@)
545  */
546 HRESULT WINAPI MLANG_DllRegisterServer(void)
547 {
548     HRESULT hr;
549
550     TRACE("\n");
551
552     hr = register_coclasses(coclass_list);
553     if (SUCCEEDED(hr))
554         hr = register_interfaces(interface_list);
555     return hr;
556 }
557
558 /***********************************************************************
559  *              DllUnregisterServer (MLANG.@)
560  */
561 HRESULT WINAPI MLANG_DllUnregisterServer(void)
562 {
563     HRESULT hr;
564
565     TRACE("\n");
566
567     hr = unregister_coclasses(coclass_list);
568     if (SUCCEEDED(hr))
569         hr = unregister_interfaces(interface_list);
570     return hr;
571 }