winejoystick: Fix a crash on accessing a CFArray past its end due to an off-by-one...
[wine/multimedia.git] / dlls / dsound / dsound_main.c
blobcb46301f92c2709dfd68e91bbb7a18319ae01fb5
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
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.
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.
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
21 * Most thread locking is complete. There may be a few race
22 * conditions still lurking.
24 * TODO:
25 * Implement SetCooperativeLevel properly (need to address focus issues)
26 * Implement DirectSound3DBuffers (stubs in place)
27 * Use hardware 3D support if available
28 * Add critical section locking inside Release and AddRef methods
29 * Handle static buffers - put those in hardware, non-static not in hardware
30 * Hardware DuplicateSoundBuffer
31 * Proper volume calculation for 3d buffers
32 * Remove DS_HEL_FRAGS and use mixer fragment length for it
35 #include <stdarg.h>
37 #define COBJMACROS
38 #define NONAMELESSUNION
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winuser.h"
43 #include "winnls.h"
44 #include "winreg.h"
45 #include "mmsystem.h"
46 #include "winternl.h"
47 #include "mmddk.h"
48 #include "wine/debug.h"
49 #include "dsound.h"
50 #include "dsconf.h"
51 #include "ks.h"
52 #include "rpcproxy.h"
53 #include "rpc.h"
54 #include "rpcndr.h"
55 #include "unknwn.h"
56 #include "oleidl.h"
57 #include "shobjidl.h"
58 #include "strmif.h"
60 #include "initguid.h"
61 #include "ksmedia.h"
62 #include "propkey.h"
63 #include "devpkey.h"
65 #include "dsound_private.h"
67 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
69 struct list DSOUND_renderers = LIST_INIT(DSOUND_renderers);
70 CRITICAL_SECTION DSOUND_renderers_lock;
71 static CRITICAL_SECTION_DEBUG DSOUND_renderers_lock_debug =
73 0, 0, &DSOUND_renderers_lock,
74 { &DSOUND_renderers_lock_debug.ProcessLocksList, &DSOUND_renderers_lock_debug.ProcessLocksList },
75 0, 0, { (DWORD_PTR)(__FILE__ ": DSOUND_renderers_lock") }
77 CRITICAL_SECTION DSOUND_renderers_lock = { &DSOUND_renderers_lock_debug, -1, 0, 0, 0, 0 };
79 struct list DSOUND_capturers = LIST_INIT(DSOUND_capturers);
80 CRITICAL_SECTION DSOUND_capturers_lock;
81 static CRITICAL_SECTION_DEBUG DSOUND_capturers_lock_debug =
83 0, 0, &DSOUND_capturers_lock,
84 { &DSOUND_capturers_lock_debug.ProcessLocksList, &DSOUND_capturers_lock_debug.ProcessLocksList },
85 0, 0, { (DWORD_PTR)(__FILE__ ": DSOUND_capturers_lock") }
87 CRITICAL_SECTION DSOUND_capturers_lock = { &DSOUND_capturers_lock_debug, -1, 0, 0, 0, 0 };
89 GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
90 GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
92 WCHAR wine_vxd_drv[] = { 'w','i','n','e','m','m','.','v','x','d', 0 };
94 /* All default settings, you most likely don't want to touch these, see wiki on UsefulRegistryKeys */
95 int ds_hel_buflen = 32768 * 2;
96 int ds_snd_queue_max = 10;
97 static HINSTANCE instance;
100 * Get a config key from either the app-specific or the default config
103 static inline DWORD get_config_key( HKEY defkey, HKEY appkey, const char *name,
104 char *buffer, DWORD size )
106 if (appkey && !RegQueryValueExA( appkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
107 if (defkey && !RegQueryValueExA( defkey, name, 0, NULL, (LPBYTE)buffer, &size )) return 0;
108 return ERROR_FILE_NOT_FOUND;
113 * Setup the dsound options.
116 void setup_dsound_options(void)
118 char buffer[MAX_PATH+16];
119 HKEY hkey, appkey = 0;
120 DWORD len;
122 buffer[MAX_PATH]='\0';
124 /* @@ Wine registry key: HKCU\Software\Wine\DirectSound */
125 if (RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\DirectSound", &hkey )) hkey = 0;
127 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
128 if (len && len < MAX_PATH)
130 HKEY tmpkey;
131 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\DirectSound */
132 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
134 char *p, *appname = buffer;
135 if ((p = strrchr( appname, '/' ))) appname = p + 1;
136 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
137 strcat( appname, "\\DirectSound" );
138 TRACE("appname = [%s]\n", appname);
139 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
140 RegCloseKey( tmpkey );
144 /* get options */
146 if (!get_config_key( hkey, appkey, "HelBuflen", buffer, MAX_PATH ))
147 ds_hel_buflen = atoi(buffer);
149 if (!get_config_key( hkey, appkey, "SndQueueMax", buffer, MAX_PATH ))
150 ds_snd_queue_max = atoi(buffer);
153 if (appkey) RegCloseKey( appkey );
154 if (hkey) RegCloseKey( hkey );
156 TRACE("ds_hel_buflen = %d\n", ds_hel_buflen);
157 TRACE("ds_snd_queue_max = %d\n", ds_snd_queue_max);
160 static const char * get_device_id(LPCGUID pGuid)
162 if (IsEqualGUID(&DSDEVID_DefaultPlayback, pGuid))
163 return "DSDEVID_DefaultPlayback";
164 else if (IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuid))
165 return "DSDEVID_DefaultVoicePlayback";
166 else if (IsEqualGUID(&DSDEVID_DefaultCapture, pGuid))
167 return "DSDEVID_DefaultCapture";
168 else if (IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuid))
169 return "DSDEVID_DefaultVoiceCapture";
170 return debugstr_guid(pGuid);
173 static HRESULT get_mmdevenum(IMMDeviceEnumerator **devenum)
175 HRESULT hr, init_hr;
177 init_hr = CoInitialize(NULL);
179 hr = CoCreateInstance(&CLSID_MMDeviceEnumerator, NULL,
180 CLSCTX_INPROC_SERVER, &IID_IMMDeviceEnumerator, (void**)devenum);
181 if(FAILED(hr)){
182 if(SUCCEEDED(init_hr))
183 CoUninitialize();
184 *devenum = NULL;
185 ERR("CoCreateInstance failed: %08x\n", hr);
186 return hr;
189 return init_hr;
192 static void release_mmdevenum(IMMDeviceEnumerator *devenum, HRESULT init_hr)
194 IMMDeviceEnumerator_Release(devenum);
195 if(SUCCEEDED(init_hr))
196 CoUninitialize();
199 static HRESULT get_mmdevice_guid(IMMDevice *device, IPropertyStore *ps,
200 GUID *guid)
202 PROPVARIANT pv;
203 HRESULT hr;
205 if(!ps){
206 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
207 if(FAILED(hr)){
208 WARN("OpenPropertyStore failed: %08x\n", hr);
209 return hr;
211 }else
212 IPropertyStore_AddRef(ps);
214 PropVariantInit(&pv);
216 hr = IPropertyStore_GetValue(ps, &PKEY_AudioEndpoint_GUID, &pv);
217 if(FAILED(hr)){
218 IPropertyStore_Release(ps);
219 WARN("GetValue(GUID) failed: %08x\n", hr);
220 return hr;
223 CLSIDFromString(pv.u.pwszVal, guid);
225 PropVariantClear(&pv);
226 IPropertyStore_Release(ps);
228 return S_OK;
231 /***************************************************************************
232 * GetDeviceID [DSOUND.9]
234 * Retrieves unique identifier of default device specified
236 * PARAMS
237 * pGuidSrc [I] Address of device GUID.
238 * pGuidDest [O] Address to receive unique device GUID.
240 * RETURNS
241 * Success: DS_OK
242 * Failure: DSERR_INVALIDPARAM
244 * NOTES
245 * pGuidSrc is a valid device GUID or DSDEVID_DefaultPlayback,
246 * DSDEVID_DefaultCapture, DSDEVID_DefaultVoicePlayback, or
247 * DSDEVID_DefaultVoiceCapture.
248 * Returns pGuidSrc if pGuidSrc is a valid device or the device
249 * GUID for the specified constants.
251 HRESULT WINAPI GetDeviceID(LPCGUID pGuidSrc, LPGUID pGuidDest)
253 IMMDeviceEnumerator *devenum;
254 EDataFlow flow = (EDataFlow)-1;
255 ERole role = (ERole)-1;
256 HRESULT hr, init_hr;
258 TRACE("(%s,%p)\n", get_device_id(pGuidSrc),pGuidDest);
260 if(!pGuidSrc || !pGuidDest)
261 return DSERR_INVALIDPARAM;
263 init_hr = get_mmdevenum(&devenum);
264 if(!devenum)
265 return init_hr;
267 if(IsEqualGUID(&DSDEVID_DefaultPlayback, pGuidSrc)){
268 role = eMultimedia;
269 flow = eRender;
270 }else if(IsEqualGUID(&DSDEVID_DefaultVoicePlayback, pGuidSrc)){
271 role = eCommunications;
272 flow = eRender;
273 }else if(IsEqualGUID(&DSDEVID_DefaultCapture, pGuidSrc)){
274 role = eMultimedia;
275 flow = eCapture;
276 }else if(IsEqualGUID(&DSDEVID_DefaultVoiceCapture, pGuidSrc)){
277 role = eCommunications;
278 flow = eCapture;
281 if(role != (ERole)-1 && flow != (EDataFlow)-1){
282 IMMDevice *device;
284 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum,
285 flow, role, &device);
286 if(FAILED(hr)){
287 WARN("GetDefaultAudioEndpoint failed: %08x\n", hr);
288 release_mmdevenum(devenum, init_hr);
289 return DSERR_NODRIVER;
292 hr = get_mmdevice_guid(device, NULL, pGuidDest);
293 IMMDevice_Release(device);
295 release_mmdevenum(devenum, init_hr);
297 return (hr == S_OK) ? DS_OK : hr;
300 release_mmdevenum(devenum, init_hr);
302 *pGuidDest = *pGuidSrc;
304 return DS_OK;
307 struct morecontext
309 LPDSENUMCALLBACKA callA;
310 LPVOID data;
313 static BOOL CALLBACK a_to_w_callback(LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data)
315 struct morecontext *context = data;
316 char descA[MAXPNAMELEN], modA[MAXPNAMELEN];
318 WideCharToMultiByte(CP_ACP, 0, descW, -1, descA, sizeof(descA), NULL, NULL);
319 WideCharToMultiByte(CP_ACP, 0, modW, -1, modA, sizeof(modA), NULL, NULL);
321 return context->callA(guid, descA, modA, context->data);
324 /***************************************************************************
325 * DirectSoundEnumerateA [DSOUND.2]
327 * Enumerate all DirectSound drivers installed in the system
329 * PARAMS
330 * lpDSEnumCallback [I] Address of callback function.
331 * lpContext [I] Address of user defined context passed to callback function.
333 * RETURNS
334 * Success: DS_OK
335 * Failure: DSERR_INVALIDPARAM
337 HRESULT WINAPI DirectSoundEnumerateA(
338 LPDSENUMCALLBACKA lpDSEnumCallback,
339 LPVOID lpContext)
341 struct morecontext context;
343 if (lpDSEnumCallback == NULL) {
344 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
345 return DSERR_INVALIDPARAM;
348 context.callA = lpDSEnumCallback;
349 context.data = lpContext;
351 return DirectSoundEnumerateW(a_to_w_callback, &context);
354 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
356 IMMDeviceEnumerator *devenum;
357 IMMDeviceCollection *coll;
358 UINT count, i;
359 HRESULT hr, init_hr;
361 init_hr = get_mmdevenum(&devenum);
362 if(!devenum)
363 return init_hr;
365 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
366 DEVICE_STATE_ACTIVE, &coll);
367 if(FAILED(hr)){
368 WARN("EnumAudioEndpoints failed: %08x\n", hr);
369 release_mmdevenum(devenum, init_hr);
370 return hr;
373 hr = IMMDeviceCollection_GetCount(coll, &count);
374 if(FAILED(hr)){
375 IMMDeviceCollection_Release(coll);
376 release_mmdevenum(devenum, init_hr);
377 WARN("GetCount failed: %08x\n", hr);
378 return hr;
381 for(i = 0; i < count; ++i){
382 GUID guid;
384 hr = IMMDeviceCollection_Item(coll, i, device);
385 if(FAILED(hr))
386 continue;
388 hr = get_mmdevice_guid(*device, NULL, &guid);
389 if(FAILED(hr)){
390 IMMDevice_Release(*device);
391 continue;
394 if(IsEqualGUID(&guid, tgt)){
395 IMMDeviceCollection_Release(coll);
396 release_mmdevenum(devenum, init_hr);
397 return DS_OK;
400 IMMDevice_Release(*device);
403 WARN("No device with GUID %s found!\n", wine_dbgstr_guid(tgt));
405 IMMDeviceCollection_Release(coll);
406 release_mmdevenum(devenum, init_hr);
408 return DSERR_INVALIDPARAM;
411 static BOOL send_device(IMMDevice *device, GUID *guid,
412 LPDSENUMCALLBACKW cb, void *user)
414 IPropertyStore *ps;
415 PROPVARIANT pv;
416 BOOL keep_going;
417 HRESULT hr;
419 PropVariantInit(&pv);
421 hr = IMMDevice_OpenPropertyStore(device, STGM_READ, &ps);
422 if(FAILED(hr)){
423 WARN("OpenPropertyStore failed: %08x\n", hr);
424 return TRUE;
427 hr = get_mmdevice_guid(device, ps, guid);
428 if(FAILED(hr)){
429 IPropertyStore_Release(ps);
430 return TRUE;
433 hr = IPropertyStore_GetValue(ps,
434 (const PROPERTYKEY *)&DEVPKEY_Device_FriendlyName, &pv);
435 if(FAILED(hr)){
436 IPropertyStore_Release(ps);
437 WARN("GetValue(FriendlyName) failed: %08x\n", hr);
438 return TRUE;
441 TRACE("Calling back with %s (%s)\n", wine_dbgstr_guid(guid),
442 wine_dbgstr_w(pv.u.pwszVal));
444 keep_going = cb(guid, pv.u.pwszVal, wine_vxd_drv, user);
446 PropVariantClear(&pv);
447 IPropertyStore_Release(ps);
449 return keep_going;
452 /* S_FALSE means the callback returned FALSE at some point
453 * S_OK means the callback always returned TRUE */
454 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
455 LPDSENUMCALLBACKW cb, void *user)
457 IMMDeviceEnumerator *devenum;
458 IMMDeviceCollection *coll;
459 IMMDevice *defdev = NULL;
460 UINT count, i, n;
461 BOOL keep_going;
462 HRESULT hr, init_hr;
464 static const WCHAR primary_desc[] = {'P','r','i','m','a','r','y',' ',
465 'S','o','u','n','d',' ','D','r','i','v','e','r',0};
466 static const WCHAR empty_drv[] = {0};
468 init_hr = get_mmdevenum(&devenum);
469 if(!devenum)
470 return init_hr;
472 hr = IMMDeviceEnumerator_EnumAudioEndpoints(devenum, flow,
473 DEVICE_STATE_ACTIVE, &coll);
474 if(FAILED(hr)){
475 release_mmdevenum(devenum, init_hr);
476 WARN("EnumAudioEndpoints failed: %08x\n", hr);
477 return DS_OK;
480 hr = IMMDeviceCollection_GetCount(coll, &count);
481 if(FAILED(hr)){
482 IMMDeviceCollection_Release(coll);
483 release_mmdevenum(devenum, init_hr);
484 WARN("GetCount failed: %08x\n", hr);
485 return DS_OK;
488 if(count == 0){
489 release_mmdevenum(devenum, init_hr);
490 return DS_OK;
493 TRACE("Calling back with NULL (%s)\n", wine_dbgstr_w(primary_desc));
494 keep_going = cb(NULL, primary_desc, empty_drv, user);
496 /* always send the default device first */
497 if(keep_going){
498 hr = IMMDeviceEnumerator_GetDefaultAudioEndpoint(devenum, flow,
499 eMultimedia, &defdev);
500 if(FAILED(hr)){
501 defdev = NULL;
502 n = 0;
503 }else{
504 keep_going = send_device(defdev, &guids[0], cb, user);
505 n = 1;
509 for(i = 0; keep_going && i < count; ++i){
510 IMMDevice *device;
512 hr = IMMDeviceCollection_Item(coll, i, &device);
513 if(FAILED(hr)){
514 WARN("Item failed: %08x\n", hr);
515 continue;
518 if(device != defdev){
519 send_device(device, &guids[n], cb, user);
520 ++n;
523 IMMDevice_Release(device);
526 if(defdev)
527 IMMDevice_Release(defdev);
528 IMMDeviceCollection_Release(coll);
530 release_mmdevenum(devenum, init_hr);
532 return keep_going ? S_OK : S_FALSE;
535 /***************************************************************************
536 * DirectSoundEnumerateW [DSOUND.3]
538 * Enumerate all DirectSound drivers installed in the system
540 * PARAMS
541 * lpDSEnumCallback [I] Address of callback function.
542 * lpContext [I] Address of user defined context passed to callback function.
544 * RETURNS
545 * Success: DS_OK
546 * Failure: DSERR_INVALIDPARAM
548 HRESULT WINAPI DirectSoundEnumerateW(
549 LPDSENUMCALLBACKW lpDSEnumCallback,
550 LPVOID lpContext )
552 HRESULT hr;
554 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext);
556 if (lpDSEnumCallback == NULL) {
557 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
558 return DSERR_INVALIDPARAM;
561 setup_dsound_options();
563 hr = enumerate_mmdevices(eRender, DSOUND_renderer_guids,
564 lpDSEnumCallback, lpContext);
565 return SUCCEEDED(hr) ? DS_OK : hr;
568 /***************************************************************************
569 * DirectSoundCaptureEnumerateA [DSOUND.7]
571 * Enumerate all DirectSound drivers installed in the system.
573 * PARAMS
574 * lpDSEnumCallback [I] Address of callback function.
575 * lpContext [I] Address of user defined context passed to callback function.
577 * RETURNS
578 * Success: DS_OK
579 * Failure: DSERR_INVALIDPARAM
581 HRESULT WINAPI DirectSoundCaptureEnumerateA(
582 LPDSENUMCALLBACKA lpDSEnumCallback,
583 LPVOID lpContext)
585 struct morecontext context;
587 if (lpDSEnumCallback == NULL) {
588 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
589 return DSERR_INVALIDPARAM;
592 context.callA = lpDSEnumCallback;
593 context.data = lpContext;
595 return DirectSoundCaptureEnumerateW(a_to_w_callback, &context);
598 /***************************************************************************
599 * DirectSoundCaptureEnumerateW [DSOUND.8]
601 * Enumerate all DirectSound drivers installed in the system.
603 * PARAMS
604 * lpDSEnumCallback [I] Address of callback function.
605 * lpContext [I] Address of user defined context passed to callback function.
607 * RETURNS
608 * Success: DS_OK
609 * Failure: DSERR_INVALIDPARAM
611 HRESULT WINAPI
612 DirectSoundCaptureEnumerateW(
613 LPDSENUMCALLBACKW lpDSEnumCallback,
614 LPVOID lpContext)
616 HRESULT hr;
618 TRACE("(%p,%p)\n", lpDSEnumCallback, lpContext );
620 if (lpDSEnumCallback == NULL) {
621 WARN("invalid parameter: lpDSEnumCallback == NULL\n");
622 return DSERR_INVALIDPARAM;
625 setup_dsound_options();
627 hr = enumerate_mmdevices(eCapture, DSOUND_capture_guids,
628 lpDSEnumCallback, lpContext);
629 return SUCCEEDED(hr) ? DS_OK : hr;
632 /*******************************************************************************
633 * DirectSound ClassFactory
636 typedef HRESULT (*FnCreateInstance)(REFIID riid, LPVOID *ppobj);
638 typedef struct {
639 IClassFactory IClassFactory_iface;
640 REFCLSID rclsid;
641 FnCreateInstance pfnCreateInstance;
642 } IClassFactoryImpl;
644 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
646 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
649 static HRESULT WINAPI
650 DSCF_QueryInterface(IClassFactory *iface, REFIID riid, LPVOID *ppobj)
652 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
653 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppobj);
654 if (ppobj == NULL)
655 return E_POINTER;
656 if (IsEqualIID(riid, &IID_IUnknown) ||
657 IsEqualIID(riid, &IID_IClassFactory))
659 *ppobj = iface;
660 IClassFactory_AddRef(iface);
661 return S_OK;
663 *ppobj = NULL;
664 return E_NOINTERFACE;
667 static ULONG WINAPI DSCF_AddRef(LPCLASSFACTORY iface)
669 return 2;
672 static ULONG WINAPI DSCF_Release(LPCLASSFACTORY iface)
674 /* static class, won't be freed */
675 return 1;
678 static HRESULT WINAPI DSCF_CreateInstance(
679 LPCLASSFACTORY iface,
680 LPUNKNOWN pOuter,
681 REFIID riid,
682 LPVOID *ppobj)
684 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
685 TRACE("(%p, %p, %s, %p)\n", This, pOuter, debugstr_guid(riid), ppobj);
687 if (pOuter)
688 return CLASS_E_NOAGGREGATION;
690 if (ppobj == NULL) {
691 WARN("invalid parameter\n");
692 return DSERR_INVALIDPARAM;
694 *ppobj = NULL;
695 return This->pfnCreateInstance(riid, ppobj);
698 static HRESULT WINAPI DSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
700 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
701 FIXME("(%p, %d) stub!\n", This, dolock);
702 return S_OK;
705 static const IClassFactoryVtbl DSCF_Vtbl = {
706 DSCF_QueryInterface,
707 DSCF_AddRef,
708 DSCF_Release,
709 DSCF_CreateInstance,
710 DSCF_LockServer
713 static IClassFactoryImpl DSOUND_CF[] = {
714 { { &DSCF_Vtbl }, &CLSID_DirectSound, DSOUND_Create },
715 { { &DSCF_Vtbl }, &CLSID_DirectSound8, DSOUND_Create8 },
716 { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture, DSOUND_CaptureCreate },
717 { { &DSCF_Vtbl }, &CLSID_DirectSoundCapture8, DSOUND_CaptureCreate8 },
718 { { &DSCF_Vtbl }, &CLSID_DirectSoundFullDuplex, DSOUND_FullDuplexCreate },
719 { { &DSCF_Vtbl }, &CLSID_DirectSoundPrivate, IKsPrivatePropertySetImpl_Create },
720 { { NULL }, NULL, NULL }
723 /*******************************************************************************
724 * DllGetClassObject [DSOUND.@]
725 * Retrieves class object from a DLL object
727 * NOTES
728 * Docs say returns STDAPI
730 * PARAMS
731 * rclsid [I] CLSID for the class object
732 * riid [I] Reference to identifier of interface for class object
733 * ppv [O] Address of variable to receive interface pointer for riid
735 * RETURNS
736 * Success: S_OK
737 * Failure: CLASS_E_CLASSNOTAVAILABLE, E_OUTOFMEMORY, E_INVALIDARG,
738 * E_UNEXPECTED
740 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
742 int i = 0;
743 TRACE("(%s, %s, %p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
745 if (ppv == NULL) {
746 WARN("invalid parameter\n");
747 return E_INVALIDARG;
750 *ppv = NULL;
752 if (!IsEqualIID(riid, &IID_IClassFactory) &&
753 !IsEqualIID(riid, &IID_IUnknown)) {
754 WARN("no interface for %s\n", debugstr_guid(riid));
755 return E_NOINTERFACE;
758 while (NULL != DSOUND_CF[i].rclsid) {
759 if (IsEqualGUID(rclsid, DSOUND_CF[i].rclsid)) {
760 DSCF_AddRef(&DSOUND_CF[i].IClassFactory_iface);
761 *ppv = &DSOUND_CF[i];
762 return S_OK;
764 i++;
767 WARN("(%s, %s, %p): no class found.\n", debugstr_guid(rclsid),
768 debugstr_guid(riid), ppv);
769 return CLASS_E_CLASSNOTAVAILABLE;
773 /*******************************************************************************
774 * DllCanUnloadNow [DSOUND.4]
775 * Determines whether the DLL is in use.
777 * RETURNS
778 * Can unload now: S_OK
779 * Cannot unload now (the DLL is still active): S_FALSE
781 HRESULT WINAPI DllCanUnloadNow(void)
783 return S_FALSE;
786 #define INIT_GUID(guid, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
787 guid.Data1 = l; guid.Data2 = w1; guid.Data3 = w2; \
788 guid.Data4[0] = b1; guid.Data4[1] = b2; guid.Data4[2] = b3; \
789 guid.Data4[3] = b4; guid.Data4[4] = b5; guid.Data4[5] = b6; \
790 guid.Data4[6] = b7; guid.Data4[7] = b8;
792 /***********************************************************************
793 * DllMain (DSOUND.init)
795 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpvReserved)
797 TRACE("(%p %d %p)\n", hInstDLL, fdwReason, lpvReserved);
799 switch (fdwReason) {
800 case DLL_PROCESS_ATTACH:
801 instance = hInstDLL;
802 DisableThreadLibraryCalls(hInstDLL);
803 /* Increase refcount on dsound by 1 */
804 GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, (LPCWSTR)hInstDLL, &hInstDLL);
805 break;
806 case DLL_PROCESS_DETACH:
807 if (lpvReserved) break;
808 DeleteCriticalSection(&DSOUND_renderers_lock);
809 DeleteCriticalSection(&DSOUND_capturers_lock);
810 break;
812 return TRUE;
815 /***********************************************************************
816 * DllRegisterServer (DSOUND.@)
818 HRESULT WINAPI DllRegisterServer(void)
820 return __wine_register_resources( instance );
823 /***********************************************************************
824 * DllUnregisterServer (DSOUND.@)
826 HRESULT WINAPI DllUnregisterServer(void)
828 return __wine_unregister_resources( instance );