动态枚举UVC设备索引号,方便openCV 接口调用

动态枚举UVC设备,返回索引号截图如下:

    int GetOrbUvcDevIdx(void)

{
 ICreateDevEnum *pDevEnum = NULL;
 IEnumMoniker *pEnum = NULL;
 int deviceCounter = 0;
 CoInitialize(NULL);
 HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL,
 CLSCTX_INPROC_SERVER, IID_ICreateDevEnum,
 reinterpret_cast(&pDevEnum));
 int idx = -1;
 int flagi= -1;
 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)
 {
 //get device idx
 flagi++;
 TCHAR szBuf[512];
 WCHAR *wszDisplayName = NULL;
 if (SUCCEEDED(pMoniker->GetDisplayName(0, 0, &wszDisplayName)))
 {
 if (wszDisplayName)
 {
 wcscpy(szBuf, wszDisplayName);
 CoTaskMemFree(wszDisplayName);
 if (wcsstr(szBuf, L"vid_005b"))
 {
 idx = flagi;
 }
 //wprintf(L"%s\n", szBuf);
 }
 }
 pMoniker->Release();
 pMoniker = NULL;
 deviceCounter++;
 }
 pDevEnum->Release();
 pDevEnum = NULL;
 pEnum->Release();
 pEnum = NULL;
 }
 }
 return idx;
}

你可能感兴趣的:(动态枚举UVC设备索引号,方便openCV 接口调用)