下面函数为通过
Directshow
API 获取设备列表的函数,
pEnumCat->Next的第0个设备为系统默认设备
REFGUID guidValue :
CLSID_AudioInputDeviceCategory(麦克风); CLSID_AudioRendererCategory(扬声器); CLSID_VideoInputDeviceCategory(Camera)
HRESULT GetAVDevices( std::list<av_device> &list_Devices, REFGUID guidValue )
{
TDeviceName name;
av_device avname;
HRESULT hr;
// 初始化
//list_Devices.clear();
// 初始化COM
hr = CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
if (FAILED(hr))
{
return hr;
}
// 创建系统设备枚举器实例
ICreateDevEnum *pSysDevEnum = NULL;
hr = CoCreateInstance( CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum );
if (FAILED(hr))
{
CoUninitialize();
return hr;
}
// 获取设备类枚举器
IEnumMoniker *pEnumCat = NULL;
int i =0;
hr = pSysDevEnum->CreateClassEnumerator( guidValue, &pEnumCat, 0 );
if (hr == S_OK)
{
// 枚举设备名称
IMoniker *pMoniker = NULL;
ULONG cFetched;
while(pEnumCat->Next( 1, &pMoniker, &cFetched ) == S_OK)
{
IPropertyBag *pPropBag;
hr = pMoniker->BindToStorage( NULL, NULL, IID_IPropertyBag, (void **)&pPropBag );
if (SUCCEEDED(hr))
{
// 获取设备友好名
VARIANT varName;
VariantInit( &varName );
hr = pPropBag->Read( L"FriendlyName", &varName, NULL );
if (SUCCEEDED(hr))
{
avname.device_name = Win_Utility::UnicodeToUtf8(varName.bstrVal);
avname.device_name_display = Win_Utility::UnicodeToANSI(varName.bstrVal);
// 获取设备Moniker名
LPOLESTR pOleDisplayName = reinterpret_cast<LPOLESTR>(CoTaskMemAlloc(MAX_MONIKER_NAME_LENGTH * 2));
if (pOleDisplayName != NULL)
{
hr = pMoniker->GetDisplayName( NULL, NULL, &pOleDisplayName );
if (SUCCEEDED(hr))
{
avname.unique_deviceid = Win_Utility::UnicodeToANSI(pOleDisplayName);
if(CLSID_AudioInputDeviceCategory==guidValue)
{
avname.device_type=AUDIO_CHANNEL;
}
else if(CLSID_VideoInputDeviceCategory==guidValue)
{
avname.device_type=VIDEO_CHANNEL;
}
if(i==0)
{
avname.windows_default=1;
}
else
{
avname.windows_default=0;
}
avname.user_default = 0;
list_Devices.push_back( avname );
i++;
}
CoTaskMemFree( pOleDisplayName );
}
}
VariantClear( &varName );
pPropBag->Release();
}
pMoniker->Release();
} // End for While
pEnumCat->Release();
}
pSysDevEnum->Release();
CoUninitialize();
return hr;
}