#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