简体   繁体   中英

C++ How to map USB Camera device names/paths to USB ports

Is there any way to get all connected USB Camera devices in accordance with the USB port order? I use something like this to get device friendly names and their paths but it's not ordered(So, I don't know which one connected to which port):

int _GetUSBCameraDevicesList(std::vector<std::string>& list, std::vector<std::string>& devicePaths)
{
    //COM Library Initialization
    //comInit();

    ICreateDevEnum* pDevEnum = NULL;
    IEnumMoniker* pEnum = NULL;
    int deviceCounter = 0;
    CoInitialize(NULL);

    HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
        CLSCTX_INPROC_SERVER, IID_ICreateDevEnum,
        reinterpret_cast<void**>(&pDevEnum));


    if (SUCCEEDED(hr))
    {
        // Create an enumerator for the video capture category.
        hr = pDevEnum->CreateClassEnumerator(
            CLSID_VideoInputDeviceCategory,
            &pEnum, 0);

        if (hr == S_OK) {

            printf("SETUP: Looking For Capture Devices\n");
            IMoniker* pMoniker = NULL;

            while (pEnum->Next(1, &pMoniker, NULL) == S_OK) {

                IPropertyBag* pPropBag;
                hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag,
                    (void**)(&pPropBag));

                if (FAILED(hr)) {
                    pMoniker->Release();
                    continue;  // Skip this one, maybe the next one will work.
                }


                // Find the description or friendly name.
                VARIANT varName;
                VariantInit(&varName);
                hr = pPropBag->Read(L"Description", &varName, 0);

                if (FAILED(hr)) hr = pPropBag->Read(L"FriendlyName", &varName, 0);

                if (SUCCEEDED(hr))
                {

                    hr = pPropBag->Read(L"FriendlyName", &varName, 0);

                    int count = 0;
                    char tmp[255] = { 0 };
                    while (varName.bstrVal[count] != 0x00 && count < 255)
                    {
                        tmp[count] = (char)varName.bstrVal[count];
                        count++;
                    }
                    list.emplace_back(tmp);

                    //if (!silent) DebugPrintOut("SETUP: %i) %s\n", deviceCounter, deviceNames[deviceCounter]);


                    // then read Device Path
                    {
                        VARIANT DP_Path;
                        VariantInit(&DP_Path);

                        hr = pPropBag->Read(L"DevicePath", &DP_Path, 0);

                        if (SUCCEEDED(hr))
                        {
                            int __count = 0;
                            char __tmp[255] = { 0 };
                            while (DP_Path.bstrVal[__count] != 0x00 && __count < 255)
                            {
                                __tmp[__count] = (char)DP_Path.bstrVal[__count];
                                __count++;
                            }

                            devicePaths.emplace_back(__tmp);
                        }
                    }
                }

                pPropBag->Release();
                pPropBag = NULL;

                pMoniker->Release();
                pMoniker = NULL;

                deviceCounter++;
            }

            pDevEnum->Release();
            pDevEnum = NULL;

            pEnum->Release();
            pEnum = NULL;
        }

        //if (!silent) DebugPrintOut("SETUP: %i Device(s) found\n\n", deviceCounter);
    }

    //comUnInit();

    return deviceCounter;
}

Log output:

1. USB Camera
2. USB Camera
3. FaceCam VGA

What you see above is what order the function gives me when it should be in this order:

1. USB Camera
2. FaceCam VGA
3. USB Camera

Everything is okay, just use std::reverse for the array.

#include <algorithm>
std::reverse(devicePaths.begin(), devicePaths.end());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM