- Sync with trunk r58248 to bring the latest changes from Amine (headers) and others...
[reactos.git] / dll / directx / dsound / regsvr.c
1 /*
2 * self-registerable dll functions for dsound.dll
3 *
4 * Copyright (C) 2003 John K. Hohm
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <stdarg.h>
22
23 #define WIN32_NO_STATUS
24 #define _INC_WINDOWS
25 #define COM_NO_WINDOWS_H
26
27 #define NONAMELESSSTRUCT
28 #define NONAMELESSUNION
29 #include <windef.h>
30 #include <winbase.h>
31 //#include "winuser.h"
32 #include <winreg.h>
33
34 #include <mmsystem.h>
35 #include <dsound.h>
36
37 #include <wine/debug.h>
38 #include <wine/unicode.h>
39
40 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
41
42 static LSTATUS (WINAPI *pRegDeleteTreeW)(HKEY,LPCWSTR);
43 static LSTATUS (WINAPI *pRegDeleteTreeA)(HKEY,LPCSTR);
44
45 /*
46 * Near the bottom of this file are the exported DllRegisterServer and
47 * DllUnregisterServer, which make all this worthwhile.
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 ips; /* can be NULL to omit */
71 LPCSTR ips32; /* can be NULL to omit */
72 LPCSTR ips32_tmodel; /* can be NULL to omit */
73 LPCSTR progid; /* can be NULL to omit */
74 LPCSTR viprogid; /* can be NULL to omit */
75 LPCSTR progid_extra; /* can be NULL to omit */
76 };
77
78 static HRESULT register_coclasses(struct regsvr_coclass const *list);
79 static HRESULT unregister_coclasses(struct regsvr_coclass const *list);
80
81 /***********************************************************************
82 * static string constants
83 */
84 static WCHAR const interface_keyname[10] = {
85 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
86 static WCHAR const base_ifa_keyname[14] = {
87 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
88 'e', 0 };
89 static WCHAR const num_methods_keyname[11] = {
90 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
91 static WCHAR const ps_clsid_keyname[15] = {
92 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
93 'i', 'd', 0 };
94 static WCHAR const ps_clsid32_keyname[17] = {
95 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
96 'i', 'd', '3', '2', 0 };
97 static WCHAR const clsid_keyname[6] = {
98 'C', 'L', 'S', 'I', 'D', 0 };
99 static WCHAR const curver_keyname[7] = {
100 'C', 'u', 'r', 'V', 'e', 'r', 0 };
101 static WCHAR const ips_keyname[13] = {
102 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
103 0 };
104 static WCHAR const ips32_keyname[15] = {
105 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
106 '3', '2', 0 };
107 static WCHAR const progid_keyname[7] = {
108 'P', 'r', 'o', 'g', 'I', 'D', 0 };
109 static WCHAR const viprogid_keyname[25] = {
110 'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
111 'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
112 0 };
113 static char const tmodel_valuename[] = "ThreadingModel";
114
115 /***********************************************************************
116 * static helper functions
117 */
118 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid);
119 static LONG register_key_defvalueW(HKEY base, WCHAR const *name,
120 WCHAR const *value);
121 static LONG register_key_defvalueA(HKEY base, WCHAR const *name,
122 char const *value);
123 static LONG register_progid(WCHAR const *clsid,
124 char const *progid, char const *curver_progid,
125 char const *name, char const *extra);
126
127 /***********************************************************************
128 * register_interfaces
129 */
130 static HRESULT register_interfaces(struct regsvr_interface const *list)
131 {
132 LONG res = ERROR_SUCCESS;
133 HKEY interface_key;
134
135 res = RegCreateKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0, NULL, 0,
136 KEY_READ | KEY_WRITE, NULL, &interface_key, NULL);
137 if (res != ERROR_SUCCESS) goto error_return;
138
139 for (; res == ERROR_SUCCESS && list->iid; ++list) {
140 WCHAR buf[39];
141 HKEY iid_key;
142
143 StringFromGUID2(list->iid, buf, 39);
144 res = RegCreateKeyExW(interface_key, buf, 0, NULL, 0,
145 KEY_READ | KEY_WRITE, NULL, &iid_key, NULL);
146 if (res != ERROR_SUCCESS) goto error_close_interface_key;
147
148 if (list->name) {
149 res = RegSetValueExA(iid_key, NULL, 0, REG_SZ,
150 (CONST BYTE*)(list->name),
151 strlen(list->name) + 1);
152 if (res != ERROR_SUCCESS) goto error_close_iid_key;
153 }
154
155 if (list->base_iid) {
156 res = register_key_guid(iid_key, base_ifa_keyname, list->base_iid);
157 if (res != ERROR_SUCCESS) goto error_close_iid_key;
158 }
159
160 if (0 <= list->num_methods) {
161 static WCHAR const fmt[3] = { '%', 'd', 0 };
162 HKEY key;
163
164 res = RegCreateKeyExW(iid_key, num_methods_keyname, 0, NULL, 0,
165 KEY_READ | KEY_WRITE, NULL, &key, NULL);
166 if (res != ERROR_SUCCESS) goto error_close_iid_key;
167
168 sprintfW(buf, fmt, list->num_methods);
169 res = RegSetValueExW(key, NULL, 0, REG_SZ,
170 (CONST BYTE*)buf,
171 (lstrlenW(buf) + 1) * sizeof(WCHAR));
172 RegCloseKey(key);
173
174 if (res != ERROR_SUCCESS) goto error_close_iid_key;
175 }
176
177 if (list->ps_clsid) {
178 res = register_key_guid(iid_key, ps_clsid_keyname, list->ps_clsid);
179 if (res != ERROR_SUCCESS) goto error_close_iid_key;
180 }
181
182 if (list->ps_clsid32) {
183 res = register_key_guid(iid_key, ps_clsid32_keyname, list->ps_clsid32);
184 if (res != ERROR_SUCCESS) goto error_close_iid_key;
185 }
186
187 error_close_iid_key:
188 RegCloseKey(iid_key);
189 }
190
191 error_close_interface_key:
192 RegCloseKey(interface_key);
193 error_return:
194 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
195 }
196
197 /***********************************************************************
198 * unregister_interfaces
199 */
200 static HRESULT unregister_interfaces(struct regsvr_interface const *list)
201 {
202 LONG res = ERROR_SUCCESS;
203 HKEY interface_key;
204
205 res = RegOpenKeyExW(HKEY_CLASSES_ROOT, interface_keyname, 0,
206 KEY_READ | KEY_WRITE, &interface_key);
207 if (res == ERROR_FILE_NOT_FOUND) return S_OK;
208 if (res != ERROR_SUCCESS) goto error_return;
209
210 for (; res == ERROR_SUCCESS && list->iid; ++list) {
211 WCHAR buf[39];
212
213 StringFromGUID2(list->iid, buf, 39);
214 res = pRegDeleteTreeW(interface_key, buf);
215 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
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 = pRegDeleteTreeW(coclass_key, buf);
323 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
324 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
325
326 if (list->progid) {
327 res = pRegDeleteTreeA(HKEY_CLASSES_ROOT, list->progid);
328 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
329 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
330 }
331
332 if (list->viprogid) {
333 res = pRegDeleteTreeA(HKEY_CLASSES_ROOT, list->viprogid);
334 if (res == ERROR_FILE_NOT_FOUND) res = ERROR_SUCCESS;
335 if (res != ERROR_SUCCESS) goto error_close_coclass_key;
336 }
337 }
338
339 error_close_coclass_key:
340 RegCloseKey(coclass_key);
341 error_return:
342 return res != ERROR_SUCCESS ? HRESULT_FROM_WIN32(res) : S_OK;
343 }
344
345 /***********************************************************************
346 * regsvr_key_guid
347 */
348 static LONG register_key_guid(HKEY base, WCHAR const *name, GUID const *guid)
349 {
350 WCHAR buf[39];
351
352 StringFromGUID2(guid, buf, 39);
353 return register_key_defvalueW(base, name, buf);
354 }
355
356 /***********************************************************************
357 * regsvr_key_defvalueW
358 */
359 static LONG register_key_defvalueW(
360 HKEY base,
361 WCHAR const *name,
362 WCHAR const *value)
363 {
364 LONG res;
365 HKEY key;
366
367 res = RegCreateKeyExW(base, name, 0, NULL, 0,
368 KEY_READ | KEY_WRITE, NULL, &key, NULL);
369 if (res != ERROR_SUCCESS) return res;
370 res = RegSetValueExW(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
371 (lstrlenW(value) + 1) * sizeof(WCHAR));
372 RegCloseKey(key);
373 return res;
374 }
375
376 /***********************************************************************
377 * regsvr_key_defvalueA
378 */
379 static LONG register_key_defvalueA(
380 HKEY base,
381 WCHAR const *name,
382 char const *value)
383 {
384 LONG res;
385 HKEY key;
386
387 res = RegCreateKeyExW(base, name, 0, NULL, 0,
388 KEY_READ | KEY_WRITE, NULL, &key, NULL);
389 if (res != ERROR_SUCCESS) return res;
390 res = RegSetValueExA(key, NULL, 0, REG_SZ, (CONST BYTE*)value,
391 lstrlenA(value) + 1);
392 RegCloseKey(key);
393 return res;
394 }
395
396 /***********************************************************************
397 * regsvr_progid
398 */
399 static LONG register_progid(
400 WCHAR const *clsid,
401 char const *progid,
402 char const *curver_progid,
403 char const *name,
404 char const *extra)
405 {
406 LONG res;
407 HKEY progid_key;
408
409 res = RegCreateKeyExA(HKEY_CLASSES_ROOT, progid, 0,
410 NULL, 0, KEY_READ | KEY_WRITE, NULL,
411 &progid_key, NULL);
412 if (res != ERROR_SUCCESS) return res;
413
414 if (name) {
415 res = RegSetValueExA(progid_key, NULL, 0, REG_SZ,
416 (CONST BYTE*)name, strlen(name) + 1);
417 if (res != ERROR_SUCCESS) goto error_close_progid_key;
418 }
419
420 if (clsid) {
421 res = register_key_defvalueW(progid_key, clsid_keyname, clsid);
422 if (res != ERROR_SUCCESS) goto error_close_progid_key;
423 }
424
425 if (curver_progid) {
426 res = register_key_defvalueA(progid_key, curver_keyname,
427 curver_progid);
428 if (res != ERROR_SUCCESS) goto error_close_progid_key;
429 }
430
431 if (extra) {
432 HKEY extra_key;
433
434 res = RegCreateKeyExA(progid_key, extra, 0,
435 NULL, 0, KEY_READ | KEY_WRITE, NULL,
436 &extra_key, NULL);
437 if (res == ERROR_SUCCESS)
438 RegCloseKey(extra_key);
439 }
440
441 error_close_progid_key:
442 RegCloseKey(progid_key);
443 return res;
444 }
445
446 /***********************************************************************
447 * coclass list
448 */
449 static GUID const CLSID_DirectSoundBufferConfig = {
450 0xB2F586D4, 0x5558, 0x49D1, {0xA0,0x7B,0x32,0x49,0xDB,0xBB,0x33,0xC2} };
451
452 static struct regsvr_coclass const coclass_list[] = {
453 { &CLSID_DirectSound,
454 "DirectSound Object",
455 NULL,
456 "dsound.dll",
457 "Both"
458 },
459 { &CLSID_DirectSound8,
460 "DirectSound 8.0 Object",
461 NULL,
462 "dsound.dll",
463 "Both"
464 },
465 { &CLSID_DirectSoundBufferConfig,
466 "DirectSoundBufferConfig Object",
467 NULL,
468 "dsound.dll",
469 "Both"
470 },
471 { &CLSID_DirectSoundCapture,
472 "DirectSoundCapture Object",
473 NULL,
474 "dsound.dll",
475 "Both"
476 },
477 { &CLSID_DirectSoundCapture8,
478 "DirectSoundCapture 8.0 Object",
479 NULL,
480 "dsound.dll",
481 "Both"
482 },
483 { &CLSID_DirectSoundFullDuplex,
484 "DirectSoundFullDuplex Object",
485 NULL,
486 "dsound.dll",
487 "Both"
488 },
489 { NULL } /* list terminator */
490 };
491
492 /***********************************************************************
493 * interface list
494 */
495
496 static struct regsvr_interface const interface_list[] = {
497 { NULL } /* list terminator */
498 };
499
500 /***********************************************************************
501 * DllRegisterServer (DSOUND.@)
502 */
503 HRESULT WINAPI DllRegisterServer(void)
504 {
505 HRESULT hr;
506
507 TRACE("\n");
508
509 hr = register_coclasses(coclass_list);
510 if (SUCCEEDED(hr))
511 hr = register_interfaces(interface_list);
512 return hr;
513 }
514
515 /***********************************************************************
516 * DllUnregisterServer (DSOUND.@)
517 */
518 HRESULT WINAPI DllUnregisterServer(void)
519 {
520 HRESULT hr;
521
522 HMODULE advapi32 = GetModuleHandleA("advapi32");
523 if (!advapi32) return E_FAIL;
524 pRegDeleteTreeA = (void *) GetProcAddress(advapi32, "RegDeleteTreeA");
525 pRegDeleteTreeW = (void *) GetProcAddress(advapi32, "RegDeleteTreeW");
526 if (!pRegDeleteTreeA || !pRegDeleteTreeW) return E_FAIL;
527
528 TRACE("\n");
529
530 hr = unregister_coclasses(coclass_list);
531 if (SUCCEEDED(hr))
532 hr = unregister_interfaces(interface_list);
533 return hr;
534 }