C++ 如何将 USB 摄像头设备 names/paths 映射到 USB 端口

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

有没有办法让所有连接的USB Camera设备都按照USB接口的顺序排列? 我使用类似这样的方法来获取设备友好名称及其路径,但它没有排序(所以,我不知道哪个连接到哪个端口):

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;
}

日志输出:

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

你在上面看到的是函数给我的顺序,当它应该按这个顺序时:

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

一切正常,数组用std::reverse就可以了

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