避免在使用WinAPI(Windows Core Audio)时无效的指针

Avoiding invalidated pointers while using WinAPI (Windows Core Audio)

本文关键字:Audio 无效 指针 Core Windows WinAPI      更新时间:2023-10-16

我们使用IMMNotificationClient的实例化来通知我们特定音频设备(Foo sound blaster(发生的变化,但是当尚未安装在计算机上的Foo冲击波插入时,程序会在IMMNotificationClient::OnDeviceStateChanged函数内部出现段错误(抛出访问冲突(

据我所知,以下是导致这种情况发生的原因:

  1. 插入新的 Foo 冲击波
  2. Windows 向 IMMNotificationClient 触发设备状态更改通知
  3. IMMNotificationClient::OnDeviceStateChanged函数可以完成执行之前,Windows 会使 COM 对象失效。

此失效发生在IMMNotificationClient::OnDeviceStateChanged函数中看似随机的点。

下面是一些示例代码:

#include <mmdeviceapi.h>
#include <functiondiscoverykeys_devpkey.h>
#include <string>
class FooSoundBlasterNotifier: public IMMNotificationClient {
//private member variables
const char * FOO_BLASTER_NAME = "Foo Blaster";

public:
HRESULT STDMETHODCALLTYPE OnDeviceStateChanged(LPCWSTR pDeviceId, DWORD newState)
{
IMMDeviceEnumerator *pDeviceEnumerator;
IMMDevice *pDevice;
IPropertyStore *pStore;
HRESULT hr = CoInitialize(NULL);
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pDeviceEnumerator);
if(SUCCEEDED(hr))
{
hr = pDeviceEnumerator->GetDevice(pDeviceId, &pDevice);
if(SUCCEEDED(hr))
{
hr = pDevice->OpenPropertyStore(STGM_READ, &pStore);
if (SUCCEEDED(hr))
{
PROPVARIANT variant;
PropVariantInit(&variant);
hr = pStore->GetValue(PKEY_Device_FriendlyName, &variant);
if (SUCCEEDED(hr))
{
//Code usually crashes about right here
std::wstring friendlyNameW(variant.pwszVal);
std::string friendlyName(friendlyNameW.begin(), friendlyNameW.end());
if(friendlyName.find(FOO_BLASTER_NAME) != std::string::npos)
{
//Log the information about state change
}
//release
}
//all
}
//COM
}
//Objects
}
return S_OK;
}
//Declare other needed functions
};

如何避免使用无效的 Windows COM 对象?除此之外,我如何从访问冲突中成功恢复而无需关闭整个程序?

编辑

下面是代码失败位置的调用跟踪:

common_strnlen_simd<1,1,unsigned short>(const unsigned short * const string, const unsigned __int64 maximum_count) Line 152
at minkernelcrtsucrtsrcappcrtstringstrnlen.cpp(152)
common_strnlen<1,unsigned short>(const unsigned short * const string, const unsigned __int64 maximum_count) Line 185
at minkernelcrtsucrtsrcappcrtstringstrnlen.cpp(185)
wcslen(const wchar_t * string) Line 219
at minkernelcrtsucrtsrcappcrtstringstrnlen.cpp(219)
[External Code]
FooSoundBlaster::OnDeviceStateChanged(const wchar_t * pwstrDeviceId, unsigned long dwNewState)
[External Code]

真正的问题不是IMMDevice对象的无效,而是variant.pwszVal为空。像这样的简单检查:

if(variant.pwszVal /* != NULL, there you go Paul! :) */)
{
friendlyNameW = variant.pwszVal;
}

应该解决上面代码中的问题。