我维护的旧代码库使用waveOutGetDevCaps来获取系统上音频设备的名称。在Windows 7计算机上,这导致名称被截断,因为WAVEOUTCAPS.szPname受MAXPNAMELEN(31个字符)限制。
Win7这样做的方式是什么?
最佳答案
您可以使用以下一种核心音频API:
// get the device enumerator
IMMDeviceEnumerator* pEnumerator = NULL;
HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL,
CLSCTX_ALL,__uuidof(IMMDeviceEnumerator),
(void**)&pEnumerator);
// get the endpoint collection
IMMDeviceCollection* pCollection = NULL;
DWORD mask = DEVICE_STATE_ACTIVE || DEVICE_STATE_UNPLUGGED;
hr = pEnumerator->EnumAudioEndpoints(eRender, mask, &pCollection);
// get the size of the collection
UINT count = 0;
hr = pCollection->GetCount(&count);
for (int i = 0; i < (int)count; i++)
{
// get the endpoint
IMMDevice* pEndPoint = NULL;
hr = pCollection->Item(i, &pEndPoint);
// get the human readable name
String^ friendlyName;
IPropertyStore* pProps = NULL;
HRESULT hr = pEndPoint->OpenPropertyStore(STGM_READ, &pProps);
PROPVARIANT varName;
PropVariantInit(&varName);
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
friendlyName = gcnew String(varName.pwszVal);
PropVariantClear(&varName);
}
上面的代码中删除了错误处理,以提高可读性。 (我碰巧喜欢使用C++ / CLI在C#和Windows API之间移动。)
现在,更难的部分将是将端点名称与旧代码库中的MME设备相关联。
关于windows-7 - waveOutGetDevCaps,Win7和长设备名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5111797/