0

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?

s_om
  • 661
  • 2
  • 9
  • 24

1 Answers1

2

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.

Roman R.
  • 68,205
  • 6
  • 94
  • 158
  • Thank you for your response Roman! If that is the case, do you have any suggestions on how to handle the situation when I want to use multiple capture devices? Right now, both capture devices are showing the same video in the application due to this behavior. – s_om May 11 '22 at 08:19
  • 1
    Even though the names might be the same and there is little you can do about it, you should rather pay attention to moniker display name whcih is always unique. See [uniquiely differentiate between multiple cameras and the linked discussion there](https://stackoverflow.com/a/11806687/868014) for more insignts. Also, [Retiving the instance of the filter](https://social.msdn.microsoft.com/Forums/en-US/71e36157-c234-4f13-b99d-9ae0150592b8/retiving-the-instance-of-the-filter). – Roman R. May 11 '22 at 10:53