C++擷取電腦上串連的多個網路攝影機名稱與編號

來源:互聯網
上載者:User

標籤:===   work   enum   def   pap   ini   hresult   namespace   png   

#include<iostream>
#include "strmif.h"
#include <initguid.h>
#include<vector>
#include<string>

#pragma comment(lib, "setupapi.lib")

using namespace std;

#define VI_MAX_CAMERAS 20
DEFINE_GUID(CLSID_SystemDeviceEnum, 0x62be5d10, 0x60eb, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
DEFINE_GUID(CLSID_VideoInputDeviceCategory, 0x860bb310, 0x5d01, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);
DEFINE_GUID(IID_ICreateDevEnum, 0x29840822, 0x5b84, 0x11d0, 0xbd, 0x3b, 0x00, 0xa0, 0xc9, 0x11, 0xce, 0x86);

//列出硬體裝置
int listDevices(vector<string>& list)
{
  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))
  {
    hr = pDevEnum->CreateClassEnumerator(CLSID_VideoInputDeviceCategory,&pEnum, 0);
    if (hr == S_OK){

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

        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.push_back(tmp);
        }

        pPropBag->Release();
        pPropBag = NULL;
        pMoniker->Release();
        pMoniker = NULL;

        deviceCounter++;
      }

      pDevEnum->Release();
      pDevEnum = NULL;
      pEnum->Release();
      pEnum = NULL;
    }
  }
  return deviceCounter;
}

 

int main()
{
  vector<string> CameraName;
  vector<string> serialName;
  vector<string> serialPortName;

  int num = listDevices(CameraName);
  cout << "網路攝影機個數:" << num << endl;
  for (int i = 0; i < num; i++) {
    cout << " ID: "<< i << " : " << " Name: "<<CameraName[i] << endl;
  }

  //waitKey(0);
  system("pause");
  return 0;
}

 

 ===================運行效果===============

 

 代碼複製粘貼即可運行

 

C++擷取電腦上串連的多個網路攝影機名稱與編號

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.