//枚举设备IMoniker
IEnumMoniker *pEm=NULL;
hr=pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&pEm,0);
pEm->Reset();
ULONG cFetched;
IMoniker *pM=NULL;
while(hr = pEm->Next(1, &pM, &cFetched), hr==S_OK) //获取下一个设备
{
IPropertyBag *pBag; //属性页接口指针
hr = pM->BindToStorage(0, 0, IID_IPropertyBag, (void **)&pBag);
//获取设备属性页
if(SUCCEEDED(hr))
{
VARIANT var;
var.vt = VT_BSTR; //保存的是二进制数据
hr = pBag->Read(L"FriendlyName", &var, NULL);
//获取FriendlyName形式的信息
if (hr == NOERROR)
{
// if (id == 1)
hr=pM->BindToObject(0, 0, IID_IBaseFilter, (void**)&pFilter);
SysFreeString(var.bstrVal); //释放二进制数据资源,必须释放
}
if (SUCCEEDED(hr))
{
hr = m_pGB->AddFilter(pFilter, L"Capture Filter");
if (FAILED(hr))
{
printf("2");
}
if (SUCCEEDED(hr))
{
printf("1");
break;
}
}
pBag->Release();
}
pM->Release();
}
HRESULT GetPin(IBaseFilter *pFilter, PIN_DIRECTION PinDir, IPin **ppPin) //查找一个输出和输入 pin。利用 PIN_DIRECTION 参数来制定 pin的类型(输入还是输出)。
{
IEnumPins *pEnum = NULL;
IPin *pPin = NULL;
HRESULT hr;
if (ppPin == NULL)
{
return E_POINTER;
}
hr = pFilter->EnumPins(&pEnum);
if (FAILED(hr))
{
return hr;
}
while(pEnum->Next(1, &pPin, 0) == S_OK)
{
PIN_DIRECTION PinDirThis;
hr = pPin->QueryDirection(&PinDirThis);
if (FAILED(hr))
{
pPin->Release();
pEnum->Release();
return hr;
}
if (PinDir == PinDirThis) //如果类型符合
{
// Found a match. Return the IPin pointer to the caller.
//**ppPin = pPin;
*ppPin = pPin;
pEnum->Release();
return S_OK;
}
// Release the pin for the next time through the loop.
pPin->Release();
}
// No more pins. We did not find a match.
pEnum->Release();
return E_FAIL;
}
//利用 CLSID 生成一个 filter,然后将其加入到 graph
//在你的应用程序中,你可以这样用这个函数
//IBaseFilter *pMux;
//hr = AddFilterByCLSID(pGraph, CLSID_AviDest, L"AVI Mux", &pMux);
//if (SUCCEEDED(hr))
//{
//pMux->Release();
//}
HRESULT AddFilterByCLSID(
IGraphBuilder *pGraph, // Pointer to the Filter Graph Manager.
const GUID& clsid, // CLSID of the filter to create.
LPCWSTR wszName, // A name for the filter.
IBaseFilter **ppF// Receives a pointer to the filter.
)
{
if (!pGraph || ! ppF)
return E_POINTER;
*ppF = 0;
IBaseFilter *pF = 0;
HRESULT hr = CoCreateInstance(clsid, 0, CLSCTX_INPROC_SERVER,IID_IBaseFilter, reinterpret_cast(&pF));
if (SUCCEEDED(hr))
{
hr = pGraph->AddFilter(pF, wszName);
if (SUCCEEDED(hr))
*ppF = pF;
else
pF->Release();
}
return hr;
}
//hr = SaveGraphFile(m_pGB, L"C:\\newMyGraph.grf");保存graphedit
HRESULT SaveGraphFile(IGraphBuilder *pGraph, WCHAR *wszPath)
{
const WCHAR wszStreamName[] = L"ActiveMovieGraph";
HRESULT hr;
IStorage *pStorage = NULL;
hr = StgCreateDocfile(
wszPath,
STGM_CREATE | STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
0, &pStorage
);
if(FAILED(hr))
{
return hr;
}
IStream *pStream;
hr = pStorage->CreateStream(
wszStreamName, STGM_WRITE | STGM_CREATE | STGM_SHARE_EXCLUSIVE,
0, 0, &pStream
);
if (FAILED(hr))
{
pStorage->Release();
return hr;
}
IPersistStream *pPersist = NULL;
pGraph->QueryInterface(IID_IPersistStream, (void**)&pPersist);
hr = pPersist->Save(pStream, TRUE);
pStream->Release();
pPersist->Release();
if (SUCCEEDED(hr))
{
hr = pStorage->Commit(STGC_DEFAULT);
}
pStorage->Release();
return hr;
}
//枚举Graph中filter
HRESULT EnumFilters (IFilterGraph *pGraph)
{
IEnumFilters *pEnum = NULL;
IBaseFilter *pFilter;
ULONG cFetched1;
HRESULT hr = pGraph->EnumFilters(&pEnum);
while(pEnum->Next(1, &pFilter, &cFetched1) == S_OK)
{
FILTER_INFO FilterInfo;
hr = pFilter->QueryFilterInfo(&FilterInfo);
printf("11");
FilterInfo.pGraph->Release();
}
pFilter->Release();
pEnum->Release();
return hr;
}