新建一个基于MFC对话框的工程
#include
#include
#include
#include
#pragma comment(lib,"Strmiids.lib")
using namespace std;
vector
int GetCameraName()
{
CString str ;
ICreateDevEnum *pDevEnum = NULL;
IEnumMoniker *pEnum = NULL;
int iCameraNum = 0;
HRESULT hr = NULL;
CoInitialize(NULL);
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, reinterpret_cast
if (SUCCEEDED(hr))
{
hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&pEnum,0);
if (hr == S_OK)
{
/ /枚举捕获设备
IMoniker *pMoniker = NULL;
ULONG cFetched;
while (pEnum->Next(1, &pMoniker, &cFetched) == S_OK)
{
IPropertyBag* pPropBag;
hr = pMoniker->BindToStorage(0,0,IID_IPropertyBag,reinterpret_cast
if (SUCCEEDED(hr))
{
//获取设备名称
VARIANT varName;
varName.vt = VT_BSTR;
VariantInit(&varName);
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
CString s(varName);
result.push_back(sReulst);
iCameraNum ++;
pPropBag->Release();
}
pMoniker->Release();
}
}
}
CoUninitialize();
return iCameraNum ;
}
void CGetCameraNameDlg::OnBnClickedOk()
{
CString str,strResult;
int ret = GetCameraName();
if(result.size() <= 0)
SetDlgItemText(IDC_EDIT1,_T("此设备没有摄像头"));
else
{
for(int i = 0;i
if(i == 0)
str.Format(_T("摄像头个数:%d\r\nID: %d Name: %s\r\n"),ret,i,result[i]);
else
str.Format(_T("ID:%d Name:%s\r\n"),i,toCString(result[i]));
strResult += str;
}
SetDlgItemText(IDC_EDIT1,strResult);
}
}
这样就实现了获取设备上所有摄像头名称和编号。