| | #include "DeviceEnumerator.h" |
| | #include <iostream> |
| |
|
| | std::map<int, Device> DeviceEnumerator::getVideoDevicesMap() { |
| | return getDevicesMap(CLSID_VideoInputDeviceCategory); |
| | } |
| |
|
| | std::map<int, Device> DeviceEnumerator::getAudioDevicesMap() { |
| | return getDevicesMap(CLSID_AudioInputDeviceCategory); |
| | } |
| |
|
| | |
| | std::map<int, Device> DeviceEnumerator::getDevicesMap(const GUID deviceClass) |
| | { |
| | std::map<int, Device> deviceMap; |
| |
|
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | ICreateDevEnum *pDevEnum; |
| | HRESULT hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pDevEnum)); |
| |
|
| | |
| | IEnumMoniker *pEnum = NULL; |
| | if (SUCCEEDED(hr)) { |
| | hr = pDevEnum->CreateClassEnumerator(deviceClass, &pEnum, 0); |
| | if (hr == S_FALSE) { |
| | hr = VFW_E_NOT_FOUND; |
| | } |
| | pDevEnum->Release(); |
| | } |
| |
|
| | |
| | int deviceId = -1; |
| | if (SUCCEEDED(hr)) { |
| | |
| | IMoniker *pMoniker = NULL; |
| | while (pEnum->Next(1, &pMoniker, NULL) == S_OK) { |
| |
|
| | IPropertyBag *pPropBag; |
| | HRESULT hr = pMoniker->BindToStorage(0, 0, IID_PPV_ARGS(&pPropBag)); |
| | if (FAILED(hr)) { |
| | pMoniker->Release(); |
| | continue; |
| | } |
| |
|
| | |
| | VARIANT var; |
| | VariantInit(&var); |
| |
|
| | std::string deviceName; |
| | std::string devicePath; |
| |
|
| | |
| | hr = pPropBag->Read(L"Description", &var, 0); |
| | if (FAILED(hr)) { |
| | |
| | hr = pPropBag->Read(L"FriendlyName", &var, 0); |
| | } |
| | |
| | if (FAILED(hr)) { |
| | VariantClear(&var); |
| | continue; |
| | } |
| | |
| | else { |
| | deviceName = ConvertBSTRToMBS(var.bstrVal); |
| | } |
| |
|
| | VariantClear(&var); |
| |
|
| | |
| | hr = pPropBag->Read(L"DevicePath", &var, 0); |
| | if (FAILED(hr)) { |
| | VariantClear(&var); |
| | continue; |
| | } |
| | else { |
| | devicePath = ConvertBSTRToMBS(var.bstrVal); |
| | } |
| |
|
| | |
| | deviceId++; |
| | Device currentDevice; |
| | currentDevice.id = deviceId; |
| | currentDevice.deviceName = deviceName; |
| | currentDevice.devicePath = devicePath; |
| | deviceMap[deviceId] = currentDevice; |
| |
|
| | } |
| | pEnum->Release(); |
| | } |
| | CoUninitialize(); |
| | return deviceMap; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | std::string DeviceEnumerator::ConvertBSTRToMBS(BSTR bstr) |
| | { |
| | int wslen = ::SysStringLen(bstr); |
| | return ConvertWCSToMBS((wchar_t*)bstr, wslen); |
| | } |
| |
|
| | std::string DeviceEnumerator::ConvertWCSToMBS(const wchar_t* pstr, long wslen) |
| | { |
| | int len = ::WideCharToMultiByte(CP_ACP, 0, pstr, wslen, NULL, 0, NULL, NULL); |
| |
|
| | std::string dblstr(len, '\0'); |
| | len = ::WideCharToMultiByte(CP_ACP, 0 , |
| | pstr, wslen , |
| | &dblstr[0], len, |
| | NULL, NULL ); |
| |
|
| | return dblstr; |
| | } |
| |
|