Tag:directshow c++ console camera
#include "windows.h" #include "TCHAR.h" #include <dshow.h> #include <vector> #include <iostream># Include <cwctype> #define MAX_FRIENDLY_NAME_LENGTH #define MAX_MONIKER_NAME_LENGTH 256typedef struct _TDE Vicename{wchar Friendlyname[max_friendly_name_length]; Device friendly name WCHAR monikername[max_moniker_name_length]; Device moniker name} tdevicename;int _tmain (int argc, _tchar* argv[]) {tdevicename name; HRESULT hr;//Initialize std::vector<tdevicename> vectordevices;vectordevices.clear ();//Initialize com hr = CoInitializeEx ( NULL, coinit_apartmentthreaded), if (FAILED (HR)) {return hr;} Create a System device Enumerator instance icreatedevenum *psysdevenum = Null;hr = CoCreateInstance (Clsid_systemdeviceenum, NULL, CLSCTX_INPROC_ SERVER, Iid_icreatedevenum, (void * *) &psysdevenum), if (FAILED (HR)) {couninitialize (); return HR;} Get device class Enumerator ienummoniker *penumcat = Null;hr = Psysdevenum->createclassenumerator (clsid_videoinputdevicecategory, & Amp;penumcat, 0); if (hr = = S_OK) {//Enumerate device names IMoniker *pmOniker = NULL; ULONG Cfetched;while (Penumcat->next (1, &pmoniker, &cfetched) = = S_OK) {IPropertyBag *ppropbag;hr = pMoniker- >bindtostorage (null, NULL, Iid_ipropertybag, (void *) &ppropbag), if (SUCCEEDED (HR)) {//Get device friendly name VARIANT varName; VariantInit (&varname); hr = Ppropbag->read (L "FriendlyName", &varname, NULL); if (SUCCEEDED (HR)) { StringCchCopy (name. FriendlyName, Max_friendly_name_length, varname.bstrval); Std::wcout << NAME. FriendlyName << std::endl;//get device moniker name Lpolestr poledisplayname = reinterpret_cast<lpolestr> ( CoTaskMemAlloc (Max_moniker_name_length * 2)); if (poledisplayname! = null) {hr = Pmoniker->getdisplayname (null, NULL, &poledisplayname), if (SUCCEEDED (HR)) {stringcchcopy (name). Monikername, Max_moniker_name_length, poledisplayname); Vectordevices.push_back (NAME);} CoTaskMemFree (Poledisplayname);}} VariantClear (&varname);pP ropbag->release ();} Pmoniker->release ();} End for while Penumcat->release ();} Psysdevenum->rElease (); CoUninitialize ();}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
DirectShow console output and save video device name