Skip to content

Commit

Permalink
Don't uninitialize COM while an IMMDevice is open
Browse files Browse the repository at this point in the history
  • Loading branch information
kcat committed Jul 6, 2022
1 parent df644d3 commit 910cf95
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 22 deletions.
10 changes: 6 additions & 4 deletions dsound8.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ static HRESULT DSShare_Create(REFIID guid, DeviceShare **out)
IMMDevice *mmdev;
ALCint attrs[7];
void *temp;
HRESULT hr;
HRESULT hr, cohr;
ALsizei i;

share = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*share));
Expand All @@ -203,8 +203,10 @@ static HRESULT DSShare_Create(REFIID guid, DeviceShare **out)

TRACE("Creating shared device %p\n", share);

hr = get_mmdevice(eRender, guid, &mmdev);
if(SUCCEEDED(hr))
cohr = get_mmdevice(eRender, guid, &mmdev);
if(!mmdev)
hr = DSERR_INVALIDPARAM;
else
{
IPropertyStore *store;

Expand Down Expand Up @@ -272,7 +274,7 @@ static HRESULT DSShare_Create(REFIID guid, DeviceShare **out)
IPropertyStore_Release(store);
}

IMMDevice_Release(mmdev);
release_mmdevice(mmdev, cohr);
mmdev = NULL;
}

Expand Down
22 changes: 13 additions & 9 deletions dsound_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,8 @@ HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
UINT count, i;
HRESULT hr, init_hr;

*device = NULL;

init_hr = get_mmdevenum(&devenum);
if(!devenum) return init_hr;

Expand Down Expand Up @@ -607,20 +609,15 @@ HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
if(FAILED(hr)) continue;

hr = get_mmdevice_guid(*device, NULL, &guid);
if(FAILED(hr))
{
IMMDevice_Release(*device);
continue;
}

if(IsEqualGUID(&guid, tgt))
if(SUCCEEDED(hr) && IsEqualGUID(&guid, tgt))
{
IMMDeviceCollection_Release(coll);
release_mmdevenum(devenum, init_hr);
return DS_OK;
IMMDeviceEnumerator_Release(devenum);
return init_hr;
}

IMMDevice_Release(*device);
*device = NULL;
}

WARN("No device with GUID %s found!\n", debugstr_guid(tgt));
Expand All @@ -631,6 +628,13 @@ HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device)
return DSERR_INVALIDPARAM;
}

void release_mmdevice(IMMDevice *device, HRESULT init_hr)
{
IMMDevice_Release(device);
if(SUCCEEDED(init_hr))
CoUninitialize();
}

/* S_FALSE means the callback returned FALSE at some point
* S_OK means the callback always returned TRUE */
HRESULT enumerate_mmdevices(EDataFlow flow, PRVTENUMCALLBACK cb, void *user)
Expand Down
1 change: 1 addition & 0 deletions dsound_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,7 @@ HRESULT DSOUND_CaptureCreate8(REFIID riid, void **ppDSC);
typedef BOOL (CALLBACK *PRVTENUMCALLBACK)(EDataFlow flow, LPGUID guid, LPCWSTR descW, LPCWSTR modW, LPVOID data);
HRESULT enumerate_mmdevices(EDataFlow flow, PRVTENUMCALLBACK cb, void *user);
HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device);
void release_mmdevice(IMMDevice *device, HRESULT init_hr);

extern const WCHAR aldriver_name[];

Expand Down
18 changes: 9 additions & 9 deletions propset.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ static HRESULT DSPROPERTY_DescriptionW(
IMMDevice *mmdevice;
IPropertyStore *ps;
PROPVARIANT pv;
HRESULT hr;
HRESULT hr, cohr;

TRACE("pPropData=%p,cbPropData=%ld,pcbReturned=%p)\n",
pPropData,cbPropData,pcbReturned);
Expand All @@ -227,17 +227,17 @@ static HRESULT DSPROPERTY_DescriptionW(

DSOAL_GetDeviceID(&ppd->DeviceId, &dev_guid);

hr = get_mmdevice(eRender, &dev_guid, &mmdevice);
if(FAILED(hr)){
hr = get_mmdevice(eCapture, &dev_guid, &mmdevice);
if(FAILED(hr))
return hr;
cohr = get_mmdevice(eRender, &dev_guid, &mmdevice);
if(!mmdevice){
cohr = get_mmdevice(eCapture, &dev_guid, &mmdevice);
if(!mmdevice)
return DSERR_INVALIDPARAM;
}

hr = IMMDevice_OpenPropertyStore(mmdevice, STGM_READ, &ps);
if(FAILED(hr))
{
IMMDevice_Release(mmdevice);
release_mmdevice(mmdevice, cohr);
WARN("OpenPropertyStore failed: %08lx\n", hr);
return hr;
}
Expand All @@ -246,7 +246,7 @@ static HRESULT DSPROPERTY_DescriptionW(
if(FAILED(hr))
{
IPropertyStore_Release(ps);
IMMDevice_Release(mmdevice);
release_mmdevice(mmdevice, cohr);
WARN("GetValue(FriendlyName) failed: %08lx\n", hr);
return hr;
}
Expand All @@ -258,7 +258,7 @@ static HRESULT DSPROPERTY_DescriptionW(

PropVariantClear(&pv);
IPropertyStore_Release(ps);
IMMDevice_Release(mmdevice);
release_mmdevice(mmdevice, cohr);

if (pcbReturned)
{
Expand Down

0 comments on commit 910cf95

Please sign in to comment.