C + + gets multiple camera names and numbers connected to the computer

Source: Internet
Author: User

#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);

List hardware devices
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 would 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 << "Number of cameras:" << num << Endl;
for (int i = 0; i < num; i++) {
cout << "ID:" << i << ":" << "Name:" <<cameraname[i] << Endl;
}

Waitkey (0);
System ("pause");
return 0;
}

=================== Operation Effect ===============

Code copy and paste to run

C + + gets multiple camera names and numbers connected to the computer

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.