2 * self-registerable dll functions for dsound.dll
4 * Copyright (C) 2003 John K. Hohm
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.
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.
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
23 static LSTATUS (WINAPI
*pRegDeleteTreeW
)(HKEY
,LPCWSTR
);
24 static LSTATUS (WINAPI
*pRegDeleteTreeA
)(HKEY
,LPCSTR
);
27 * Near the bottom of this file are the exported DllRegisterServer and
28 * DllUnregisterServer, which make all this worthwhile.
31 /***********************************************************************
32 * interface for self-registering
34 struct regsvr_interface
36 IID
const *iid
; /* NULL for end of list */
37 LPCSTR name
; /* can be NULL to omit */
38 IID
const *base_iid
; /* can be NULL to omit */
39 int num_methods
; /* can be <0 to omit */
40 CLSID
const *ps_clsid
; /* can be NULL to omit */
41 CLSID
const *ps_clsid32
; /* can be NULL to omit */
44 static HRESULT
register_interfaces(struct regsvr_interface
const *list
);
45 static HRESULT
unregister_interfaces(struct regsvr_interface
const *list
);
49 CLSID
const *clsid
; /* NULL for end of list */
50 LPCSTR name
; /* can be NULL to omit */
51 LPCSTR ips
; /* can be NULL to omit */
52 LPCSTR ips32
; /* can be NULL to omit */
53 LPCSTR ips32_tmodel
; /* can be NULL to omit */
54 LPCSTR progid
; /* can be NULL to omit */
55 LPCSTR viprogid
; /* can be NULL to omit */
56 LPCSTR progid_extra
; /* can be NULL to omit */
59 static HRESULT
register_coclasses(struct regsvr_coclass
const *list
);
60 static HRESULT
unregister_coclasses(struct regsvr_coclass
const *list
);
62 /***********************************************************************
63 * static string constants
65 static WCHAR
const interface_keyname
[10] = {
66 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c', 'e', 0 };
67 static WCHAR
const base_ifa_keyname
[14] = {
68 'B', 'a', 's', 'e', 'I', 'n', 't', 'e', 'r', 'f', 'a', 'c',
70 static WCHAR
const num_methods_keyname
[11] = {
71 'N', 'u', 'm', 'M', 'e', 't', 'h', 'o', 'd', 's', 0 };
72 static WCHAR
const ps_clsid_keyname
[15] = {
73 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
75 static WCHAR
const ps_clsid32_keyname
[17] = {
76 'P', 'r', 'o', 'x', 'y', 'S', 't', 'u', 'b', 'C', 'l', 's',
77 'i', 'd', '3', '2', 0 };
78 static WCHAR
const clsid_keyname
[6] = {
79 'C', 'L', 'S', 'I', 'D', 0 };
80 static WCHAR
const curver_keyname
[7] = {
81 'C', 'u', 'r', 'V', 'e', 'r', 0 };
82 static WCHAR
const ips_keyname
[13] = {
83 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
85 static WCHAR
const ips32_keyname
[15] = {
86 'I', 'n', 'P', 'r', 'o', 'c', 'S', 'e', 'r', 'v', 'e', 'r',
88 static WCHAR
const progid_keyname
[7] = {
89 'P', 'r', 'o', 'g', 'I', 'D', 0 };
90 static WCHAR
const viprogid_keyname
[25] = {
91 'V', 'e', 'r', 's', 'i', 'o', 'n', 'I', 'n', 'd', 'e', 'p',
92 'e', 'n', 'd', 'e', 'n', 't', 'P', 'r', 'o', 'g', 'I', 'D',
94 static char const tmodel_valuename
[] = "ThreadingModel";
96 /***********************************************************************
97 * static helper functions
99 static LONG
register_key_guid(HKEY base
, WCHAR
const *name
, GUID
const *guid
);
100 static LONG
register_key_defvalueW(HKEY base
, WCHAR
const *name
,
102 static LONG
register_key_defvalueA(HKEY base
, WCHAR
const *name
,
104 static LONG
register_progid(WCHAR
const *clsid
,
105 char const *progid
, char const *curver_progid
,
106 char const *name
, char const *extra
);
108 /***********************************************************************
109 * register_interfaces
111 static HRESULT
register_interfaces(struct regsvr_interface
const *list
)
113 LONG res
= ERROR_SUCCESS
;
116 res
= RegCreateKeyExW(HKEY_CLASSES_ROOT
, interface_keyname
, 0, NULL
, 0,
117 KEY_READ
| KEY_WRITE
, NULL
, &interface_key
, NULL
);
118 if (res
!= ERROR_SUCCESS
) goto error_return
;
120 for (; res
== ERROR_SUCCESS
&& list
->iid
; ++list
) {
124 StringFromGUID2(list
->iid
, buf
, 39);
125 res
= RegCreateKeyExW(interface_key
, buf
, 0, NULL
, 0,
126 KEY_READ
| KEY_WRITE
, NULL
, &iid_key
, NULL
);
127 if (res
!= ERROR_SUCCESS
) goto error_close_interface_key
;
130 res
= RegSetValueExA(iid_key
, NULL
, 0, REG_SZ
,
131 (CONST BYTE
*)(list
->name
),
132 strlen(list
->name
) + 1);
133 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
136 if (list
->base_iid
) {
137 res
= register_key_guid(iid_key
, base_ifa_keyname
, list
->base_iid
);
138 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
141 if (0 <= list
->num_methods
) {
142 static WCHAR
const fmt
[3] = { '%', 'd', 0 };
145 res
= RegCreateKeyExW(iid_key
, num_methods_keyname
, 0, NULL
, 0,
146 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
147 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
149 swprintf(buf
, fmt
, list
->num_methods
);
150 res
= RegSetValueExW(key
, NULL
, 0, REG_SZ
,
152 (lstrlenW(buf
) + 1) * sizeof(WCHAR
));
155 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
158 if (list
->ps_clsid
) {
159 res
= register_key_guid(iid_key
, ps_clsid_keyname
, list
->ps_clsid
);
160 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
163 if (list
->ps_clsid32
) {
164 res
= register_key_guid(iid_key
, ps_clsid32_keyname
, list
->ps_clsid32
);
165 if (res
!= ERROR_SUCCESS
) goto error_close_iid_key
;
169 RegCloseKey(iid_key
);
172 error_close_interface_key
:
173 RegCloseKey(interface_key
);
175 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
178 /***********************************************************************
179 * unregister_interfaces
181 static HRESULT
unregister_interfaces(struct regsvr_interface
const *list
)
183 LONG res
= ERROR_SUCCESS
;
186 res
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, interface_keyname
, 0,
187 KEY_READ
| KEY_WRITE
, &interface_key
);
188 if (res
== ERROR_FILE_NOT_FOUND
) return S_OK
;
189 if (res
!= ERROR_SUCCESS
) goto error_return
;
191 for (; res
== ERROR_SUCCESS
&& list
->iid
; ++list
) {
194 StringFromGUID2(list
->iid
, buf
, 39);
195 res
= pRegDeleteTreeW(interface_key
, buf
);
196 if (res
== ERROR_FILE_NOT_FOUND
) res
= ERROR_SUCCESS
;
199 RegCloseKey(interface_key
);
201 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
204 /***********************************************************************
207 static HRESULT
register_coclasses(struct regsvr_coclass
const *list
)
209 LONG res
= ERROR_SUCCESS
;
212 res
= RegCreateKeyExW(HKEY_CLASSES_ROOT
, clsid_keyname
, 0, NULL
, 0,
213 KEY_READ
| KEY_WRITE
, NULL
, &coclass_key
, NULL
);
214 if (res
!= ERROR_SUCCESS
) goto error_return
;
216 for (; res
== ERROR_SUCCESS
&& list
->clsid
; ++list
) {
220 StringFromGUID2(list
->clsid
, buf
, 39);
221 res
= RegCreateKeyExW(coclass_key
, buf
, 0, NULL
, 0,
222 KEY_READ
| KEY_WRITE
, NULL
, &clsid_key
, NULL
);
223 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
226 res
= RegSetValueExA(clsid_key
, NULL
, 0, REG_SZ
,
227 (CONST BYTE
*)(list
->name
),
228 strlen(list
->name
) + 1);
229 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
233 res
= register_key_defvalueA(clsid_key
, ips_keyname
, list
->ips
);
234 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
240 res
= RegCreateKeyExW(clsid_key
, ips32_keyname
, 0, NULL
, 0,
241 KEY_READ
| KEY_WRITE
, NULL
,
243 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
245 res
= RegSetValueExA(ips32_key
, NULL
, 0, REG_SZ
,
246 (CONST BYTE
*)list
->ips32
,
247 lstrlenA(list
->ips32
) + 1);
248 if (res
== ERROR_SUCCESS
&& list
->ips32_tmodel
)
249 res
= RegSetValueExA(ips32_key
, tmodel_valuename
, 0, REG_SZ
,
250 (CONST BYTE
*)list
->ips32_tmodel
,
251 strlen(list
->ips32_tmodel
) + 1);
252 RegCloseKey(ips32_key
);
253 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
257 res
= register_key_defvalueA(clsid_key
, progid_keyname
,
259 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
261 res
= register_progid(buf
, list
->progid
, NULL
,
262 list
->name
, list
->progid_extra
);
263 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
266 if (list
->viprogid
) {
267 res
= register_key_defvalueA(clsid_key
, viprogid_keyname
,
269 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
271 res
= register_progid(buf
, list
->viprogid
, list
->progid
,
272 list
->name
, list
->progid_extra
);
273 if (res
!= ERROR_SUCCESS
) goto error_close_clsid_key
;
276 error_close_clsid_key
:
277 RegCloseKey(clsid_key
);
280 error_close_coclass_key
:
281 RegCloseKey(coclass_key
);
283 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
286 /***********************************************************************
287 * unregister_coclasses
289 static HRESULT
unregister_coclasses(struct regsvr_coclass
const *list
)
291 LONG res
= ERROR_SUCCESS
;
294 res
= RegOpenKeyExW(HKEY_CLASSES_ROOT
, clsid_keyname
, 0,
295 KEY_READ
| KEY_WRITE
, &coclass_key
);
296 if (res
== ERROR_FILE_NOT_FOUND
) return S_OK
;
297 if (res
!= ERROR_SUCCESS
) goto error_return
;
299 for (; res
== ERROR_SUCCESS
&& list
->clsid
; ++list
) {
302 StringFromGUID2(list
->clsid
, buf
, 39);
303 res
= pRegDeleteTreeW(coclass_key
, buf
);
304 if (res
== ERROR_FILE_NOT_FOUND
) res
= ERROR_SUCCESS
;
305 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
308 res
= pRegDeleteTreeA(HKEY_CLASSES_ROOT
, list
->progid
);
309 if (res
== ERROR_FILE_NOT_FOUND
) res
= ERROR_SUCCESS
;
310 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
313 if (list
->viprogid
) {
314 res
= pRegDeleteTreeA(HKEY_CLASSES_ROOT
, list
->viprogid
);
315 if (res
== ERROR_FILE_NOT_FOUND
) res
= ERROR_SUCCESS
;
316 if (res
!= ERROR_SUCCESS
) goto error_close_coclass_key
;
320 error_close_coclass_key
:
321 RegCloseKey(coclass_key
);
323 return res
!= ERROR_SUCCESS
? HRESULT_FROM_WIN32(res
) : S_OK
;
326 /***********************************************************************
329 static LONG
register_key_guid(HKEY base
, WCHAR
const *name
, GUID
const *guid
)
333 StringFromGUID2(guid
, buf
, 39);
334 return register_key_defvalueW(base
, name
, buf
);
337 /***********************************************************************
338 * regsvr_key_defvalueW
340 static LONG
register_key_defvalueW(
348 res
= RegCreateKeyExW(base
, name
, 0, NULL
, 0,
349 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
350 if (res
!= ERROR_SUCCESS
) return res
;
351 res
= RegSetValueExW(key
, NULL
, 0, REG_SZ
, (CONST BYTE
*)value
,
352 (lstrlenW(value
) + 1) * sizeof(WCHAR
));
357 /***********************************************************************
358 * regsvr_key_defvalueA
360 static LONG
register_key_defvalueA(
368 res
= RegCreateKeyExW(base
, name
, 0, NULL
, 0,
369 KEY_READ
| KEY_WRITE
, NULL
, &key
, NULL
);
370 if (res
!= ERROR_SUCCESS
) return res
;
371 res
= RegSetValueExA(key
, NULL
, 0, REG_SZ
, (CONST BYTE
*)value
,
372 lstrlenA(value
) + 1);
377 /***********************************************************************
380 static LONG
register_progid(
383 char const *curver_progid
,
390 res
= RegCreateKeyExA(HKEY_CLASSES_ROOT
, progid
, 0,
391 NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
,
393 if (res
!= ERROR_SUCCESS
) return res
;
396 res
= RegSetValueExA(progid_key
, NULL
, 0, REG_SZ
,
397 (CONST BYTE
*)name
, strlen(name
) + 1);
398 if (res
!= ERROR_SUCCESS
) goto error_close_progid_key
;
402 res
= register_key_defvalueW(progid_key
, clsid_keyname
, clsid
);
403 if (res
!= ERROR_SUCCESS
) goto error_close_progid_key
;
407 res
= register_key_defvalueA(progid_key
, curver_keyname
,
409 if (res
!= ERROR_SUCCESS
) goto error_close_progid_key
;
415 res
= RegCreateKeyExA(progid_key
, extra
, 0,
416 NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
,
418 if (res
== ERROR_SUCCESS
)
419 RegCloseKey(extra_key
);
422 error_close_progid_key
:
423 RegCloseKey(progid_key
);
427 /***********************************************************************
430 static GUID
const CLSID_DirectSoundBufferConfig
= {
431 0xB2F586D4, 0x5558, 0x49D1, {0xA0,0x7B,0x32,0x49,0xDB,0xBB,0x33,0xC2} };
433 static struct regsvr_coclass
const coclass_list
[] = {
434 { &CLSID_DirectSound
,
435 "DirectSound Object",
440 { &CLSID_DirectSound8
,
441 "DirectSound 8.0 Object",
446 { &CLSID_DirectSoundBufferConfig
,
447 "DirectSoundBufferConfig Object",
452 { &CLSID_DirectSoundCapture
,
453 "DirectSoundCapture Object",
458 { &CLSID_DirectSoundCapture8
,
459 "DirectSoundCapture 8.0 Object",
464 { &CLSID_DirectSoundFullDuplex
,
465 "DirectSoundFullDuplex Object",
470 { NULL
} /* list terminator */
473 /***********************************************************************
477 static struct regsvr_interface
const interface_list
[] = {
478 { NULL
} /* list terminator */
481 /***********************************************************************
482 * DllRegisterServer (DSOUND.@)
484 HRESULT WINAPI
DllRegisterServer(void)
488 hr
= register_coclasses(coclass_list
);
490 hr
= register_interfaces(interface_list
);
494 /***********************************************************************
495 * DllUnregisterServer (DSOUND.@)
497 HRESULT WINAPI
DllUnregisterServer(void)
501 HMODULE advapi32
= GetModuleHandleA("advapi32");
502 if (!advapi32
) return E_FAIL
;
503 pRegDeleteTreeA
= (void *) GetProcAddress(advapi32
, "RegDeleteTreeA");
504 pRegDeleteTreeW
= (void *) GetProcAddress(advapi32
, "RegDeleteTreeW");
505 if (!pRegDeleteTreeA
|| !pRegDeleteTreeW
) return E_FAIL
;
507 hr
= unregister_coclasses(coclass_list
);
509 hr
= unregister_interfaces(interface_list
);