Reputation: 681
I am using some capture devices for my application, which keep getting detected with the same name by my code which I have written following the link : https://learn.microsoft.com/en-us/windows/win32/directshow/using-the-system-device-enumerator
Here's my version of the code :
std::string CSDirectShow::GetCaptureDevice()
{
const char* funcName = "CSDirectShow::GetCaptureDevice()";
HRESULT hr = S_OK;
std::string devices = "";
CComPtr< ICreateDevEnum > pDevEnum;
if (FAILED(hr = pDevEnum.CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC)))
{
TraceMsg("%s: couldn't create system device enumerator (0x%x)", funcName, hr);
return "";
}
CComPtr< IEnumMoniker > pClassEnum;
if (FAILED(hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory, &pClassEnum, 0)))
{
TraceMsg("%s: CreateClassEnumerator(CLSID_VideoInputDeviceCategory) failed (0x%x)", funcName, hr);
return "";
}
if (!pClassEnum)
{
TraceMsg("%s: no video capture device detected", funcName);
return "";
}
ULONG cFetched = 0;
std::wstring device;
bool found = 0;
int counter = 0;
while (1)
{
CComPtr< IMoniker > pMoniker;
if ((hr = pClassEnum->Next(1, &pMoniker, &cFetched)) != S_OK)
{
break;
}
CComPtr<IPropertyBag> pPropBag;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)& pPropBag);
if (FAILED(hr))
{
continue;
}
// retrieve the filter's friendly name
_variant_t varName;
hr = pPropBag->Read(L"FriendlyName", &varName, 0);
//hr = pPropBag->Read(L"DevicePath", &varName, 0);
if (SUCCEEDED(hr))
{
_bstr_t val(varName.bstrVal);
device = (wchar_t*)val;
}
if (!device.empty()) {
std::string devicename(device.begin(), device.end());
//devicename += "_" + to_string(counter);
devices += devicename.c_str();
counter += 1;
}
else
{
break;
}
devices += ";";
}
return devices;
}
I am using two Cam Link capture devices, both of whom are returned as 'Cam Link 4K
' in my devices
value that I am returning.
Can someone please tell me if there's an error in the code due to which the names get duplicated?
Upvotes: 0
Views: 103
Reputation: 69642
Duplicate names can happen, there is no API limit or design to make it impossible. In most cases it is a question to the hardware driver which fails to assign distinct names in the case of multiple devices.
Upvotes: 2