Listing video input device information
This page shows you how to get device information of video camera (desktop camera, DV camera, etc) and TV tuner device. Please note that error handling codes are omitted to keep the sample code simple.
Sample code
The previous video capture device listing sample listed FriendlyName information only. When multiple device with the same FriendlyName is connected to the PC, more infomation must be retrieved to specify a video device. This page shows you how to retrieve all available information using IPropertyBag. For example, if the first video device found is a DV camera, this sample will show a MessageBox with the following message.
FriendlyName : Microsoft DV Camera and VCR Description : AVC Compliant DV Device DevicePath : \\?\avc#aaa&xx-dv111&camcoder&dv#123456#{abcd-1111-aaa-ccc}\global
The following is the sample code.
#include <stdio.h>
#include <dshow.h>
int
main()
{
ICreateDevEnum *pCreateDevEnum = NULL;
IEnumMoniker *pEnumMoniker = NULL;
IMoniker *pMoniker = NULL;
ULONG nFetched = 0;
// Initialize COM
CoInitialize(NULL);
// Create CreateDevEnum to list device
CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER,
IID_ICreateDevEnum, (PVOID *)&pCreateDevEnum);
// Create EnumMoniker to list VideoInputDevice
pCreateDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,
&pEnumMoniker, 0);
if (pEnumMoniker == NULL) {
// this will be shown if there is no capture device
printf("no device\n");
return 0;
}
// reset EnumMoniker
pEnumMoniker->Reset();
// get each Moniker
while (pEnumMoniker->Next(1, &pMoniker, &nFetched) == S_OK) {
IPropertyBag *pPropertyBag;
TCHAR devname[256];
TCHAR description[256];
TCHAR devpath[256];
// bind to IPropertyBag
pMoniker->BindToStorage(0, 0, IID_IPropertyBag,
(void **)&pPropertyBag);
VARIANT var;
// get FriendlyName
// FrienlyName : The name of the device
var.vt = VT_BSTR;
pPropertyBag->Read(L"FriendlyName", &var, 0);
WideCharToMultiByte(CP_ACP, 0,
var.bstrVal, -1, devname, sizeof(devname), 0, 0);
VariantClear(&var);
// get Description
// Description : A description of the device
var.vt = VT_BSTR;
pPropertyBag->Read(L"Description", &var, 0);
WideCharToMultiByte(CP_ACP, 0,
var.bstrVal, -1, description, sizeof(description), 0, 0);
VariantClear(&var);
// get DevicePath
// DevicePath : A unique string
var.vt = VT_BSTR;
pPropertyBag->Read(L"DevicePath", &var, 0);
WideCharToMultiByte(CP_ACP, 0,
var.bstrVal, -1, devpath, sizeof(devpath), 0, 0);
VariantClear(&var);
// display result
TCHAR result[1024];
memset(result, 0, sizeof(result));
sprintf(result,
"FriendlyName : %s\r\n"
"Description : %s\r\n"
"DevicePath : %s\r\n",
devname, description, devpath);
MessageBox(NULL, result, "RESULT", MB_OK);
// release
pMoniker->Release();
pPropertyBag->Release();
}
// release
pEnumMoniker->Release();
pCreateDevEnum->Release();
// finalize COM
CoUninitialize();
return 0;
}