//指定视频采集设备的友好名字,为它创建一个Filter
IBaseFilter * CTestPreviewDlg::CreateVideoDevice(const char * inFriendlyName)
{
return CreateHardwareFilter(CLSID_VideoInputDeviceCategory,inFriendlyName);
}
//根据设备的友好名字,创建一个代表该设备的Filter
IBaseFilter * CTestPreviewDlg::CreateHardwareFilter(GUID inCategory,const char * inFriendlyName)
{
//创建一个系统枚举组件对象
ICreateDevEnum * enumHardware = NULL;
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum,NULL,CLSCTX_ALL,
IID_ICreateDevEnum,(void**)&enumHardware);
if(FAILED(hr))
{
return NULL;
}
IBaseFilter * hardwareFilter = NULL;
IEnumMoniker * enumMoniker = NULL;
//为指定的目录创建枚举器
hr = enumHardware->CreateClassEnumerator(inCategory,&enumMoniker,0);
if(enumMoniker)
{
enumMoniker->Reset();
ULONG fetched = 0;
IMoniker * moniker = NULL;
char friendlyName[256];
//枚举得到该目录下所有的设备,逐个进行名字匹配
while(!hardwareFilter && SUCCEEDED(enumMoniker->Next(1,&moniker,
&fetched)) && fetched)
{
if(moniker)
{
IPropertyBag * propertyBag = NULL;
VARIANT name;
friendlyName[0] = 0;
hr = moniker->BindToStorage(0,0,IID_IPropertyBag,(void**)&propertyBag);
//读取设备的友好名字
if(SUCCEEDED(hr))
{
name.vt = VT_BSTR;
hr = propertyBag->Read(L"Friendlyname",&name,NULL);
}
if(SUCCEEDED(hr))
{
WideCharToMultiByte(CP_ACP,0,name.bstrVal,-1,
friendlyName,256,NULL,NULL);
//如果当前设备的友好名字与用户指定的设备名字相同,
//则将当前设备标识绑定为Filter形式
if(strcmp(friendlyName,inFriendlyName) == 0)
{
moniker->BindToObject(0,0,IID_IBaseFilter,
(void**)&hardwareFilter);
}
}
//释放使用过的接口
if(propertyBag)
{
propertyBag->Release();
propertyBag = NULL;
}
moniker->Release();
}
}
enumMoniker->Release();
}
enumHardware->Release();
return hardwareFilter;
}
1