这是我能够在 MSND 参考之后编写的代码:http: //msdn.microsoft.com/en-us/library/dd407269 (v=vs.85).aspx
#include <iostream>
#include <conio.h>
#include <dshow.h>
// Helper function for initializing ICaptureGraphBuilder2 and IGraphBuilder
// MSDN function: http://msdn.microsoft.com/en-us/library/dd373396(v=vs.85).aspx
// Arguments are passed by pointers
HRESULT InitCaptureGraphBuilder(ICaptureGraphBuilder2 **, IGraphBuilder **);
// Helper function for retriving an enumerator for a specified device category
// MSDN function: http://msdn.microsoft.com/en-us/library/dd377566(v=vs.85).aspx
// MSDN about moniker: http://msdn.microsoft.com/en-us/library/ms679705(v=vs.85).aspx
// MSDN about enumerator: http://msdn.microsoft.com/en-us/library/dd407292(v=vs.85).aspx
// You need to pass by reference the type of device a nd by pointer the IEnumMoniker
HRESULT EnumerateDevices(REFGUID, IEnumMoniker **);
// Helper function for preparing the output video file
// You need to pass by ponter ICaptureGraphBuilder2 and IBaseFilter
HRESULT PrepareOutputFile(ICaptureGraphBuilder2 **, IBaseFilter **);
int main()
{
ICaptureGraphBuilder2 *capture_graph = NULL;
IGraphBuilder *graph = NULL;
IEnumMoniker *device_enumerator;
IBaseFilter *capture_filter;
HRESULT hr;
hr = CoInitialize(NULL);
hr = InitCaptureGraphBuilder(&capture_graph, &graph);
hr = EnumerateDevices(CLSID_VideoInputDeviceCategory, &device_enumerator);
// hr = PrepareOutputFile(&capture_graph, &capture_filter);
// At the end:
// capture_graph->Release();
// graph->Release();
}
HRESULT InitCaptureGraphBuilder(ICaptureGraphBuilder2 **ppCapture_graph, IGraphBuilder **ppGraph)
{
if (!ppCapture_graph || !ppGraph) {
return E_POINTER;
}
ICaptureGraphBuilder2 *capture_graph = NULL;
IGraphBuilder *graph = NULL;
// Create the Capture Graph Builder
HRESULT hr = CoCreateInstance(CLSID_CaptureGraphBuilder2, NULL, CLSCTX_INPROC_SERVER, IID_ICaptureGraphBuilder2, (void**)&capture_graph);
if (SUCCEEDED(hr)) {
// Create the Filter Graph Manager
hr = CoCreateInstance(CLSID_FilterGraph, 0, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void**)&graph);
if (SUCCEEDED(hr)) {
// Initialize the Capture Graph Builder
capture_graph->SetFiltergraph(graph);
// Return both interface pointers to the caller
// !! The caller must release both interfaces !!
*ppCapture_graph = capture_graph;
*ppGraph = graph;
return S_OK;
} else {
capture_graph->Release();
}
}
// Failed
return hr;
}
HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppDevice_enumerator)
{
ICreateDevEnum *create_dev_enum;
// Create the System Device Enumerator
HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&create_dev_enum));
if (SUCCEEDED(hr)) {
// Create an enumerator for the selected category
hr = create_dev_enum->CreateClassEnumerator(category, ppDevice_enumerator, 0);
if (hr == S_FALSE) {
// The category is empty. Treat as an error
hr = VFW_E_NOT_FOUND;
}
create_dev_enum->Release();
}
return hr;
}
现在我仍然不明白如何选择我想要的视频设备。使用该函数,我返回枚举器(我认为是一种带有所有设备的向量),但是在(我必须编写的)EnumerateDevices()
之前如何使用它?!PrepareOutputFile()