通過DirectShow Api獲取系統(tǒng)麥克風绒瘦、揚聲器称簿、攝像頭信息
#include <windows.h>
#include <vector>
#include <dshow.h>
#pragma comment(lib, "Strmiids.lib")
#define MAX_FRIENDLY_NAME_LENGTH ? ?128
#define MAX_MONIKER_NAME_LENGTH ? ? 256
typedef struct _TDeviceName
{
? ? WCHAR FriendlyName[MAX_FRIENDLY_NAME_LENGTH]; ? // 設(shè)備友好名
? ? WCHAR MonikerName[MAX_MONIKER_NAME_LENGTH]; ? ? // 設(shè)備Moniker名
} TDeviceName;
//WCHAR ?轉(zhuǎn)換為 Char
char* WCharToChar(WCHAR *s)
{
?int w_nlen = WideCharToMultiByte(CP_ACP, 0, s, -1, NULL, 0, NULL, false);
?char *ret = new char[w_nlen];
?memset(ret, 0, w_nlen);
?WideCharToMultiByte(CP_ACP, 0, s, -1, ret, w_nlen, NULL, false);
return ret;
}
//REFGUID guidValue : CLSID_AudioInputDeviceCategory(麥克風); ?CLSID_AudioRendererCategory(揚聲器)椭坚; //CLSID_VideoInputDeviceCategory(攝像頭)
HRESULT GetAudioVideoInputDevices(std::vector<TDeviceName> &vectorDevices, REFGUID guidValue)
{
? ? TDeviceName name;
? ? HRESULT hr;
? ? // 初始化
? ? vectorDevices.clear();
?? // 初始化COM
? ? hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
? ? if (FAILED(hr))
? ? {
? ? ? ? return hr;
? ? }
// 創(chuàng)建系統(tǒng)設(shè)備枚舉器實例
? ? ICreateDevEnum *pSysDevEnum = NULL;
? ? hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void **)&pSysDevEnum);
? ? if (FAILED(hr))
? ? {
? ? ? ? CoUninitialize();
? ? ? ? return hr;
? ? }
? ? // 獲取設(shè)備類枚舉器
? ? IEnumMoniker *pEnumCat = NULL;
? ? hr = pSysDevEnum->CreateClassEnumerator(guidValue, &pEnumCat, 0);
? ? if (hr == S_OK)
? ? {
? ? ? ? // 枚舉設(shè)備名稱
? ? ? ? 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))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? // 獲取設(shè)備友好名
? ? ? ? ? ? ? ? VARIANT varName;
? ? ? ? ? ? ? ? VariantInit(&varName);
? ? ? ? ? ? ? ? hr = pPropBag->Read(L"FriendlyName", &varName, NULL);
? ? ? ? ? ? ? ? if (SUCCEEDED(hr))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? StringCchCopy(name.FriendlyName, MAX_FRIENDLY_NAME_LENGTH, varName.bstrVal);
? ? ? ? ? ? ? ? ? ? // 獲取設(shè)備Moniker名
? ? ? ? ? ? ? ? ? ? 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);
? ? ? ? ? ? ? ? pPropBag->Release();
? ? ? ? ? ? }
? ? ? ? ? ? pMoniker->Release();
? ? ? ? } // End for While
? ? ? ? pEnumCat->Release();
? ? }
? ? pSysDevEnum->Release();
? ? CoUninitialize();
? ? return hr;
}
int _tmain(int argc, _TCHAR* argv[])
{
? ? std::vector<TDeviceName> vectorDevices;
? ??vectorDevices.clear();
//獲取攝像頭信息
? ? GetAudioVideoInputDevices(vectorDevices, CLSID_VideoInputDeviceCategory);
? ? for (auto itor = vectorDevices.begin(); itor != vectorDevices.end(); ++itor)
? ? {
?? ? ? ?printf("FriendlyName = %s, MonikerName = %s\n", WCharToChar(itor->FriendlyName), WCharToChar(itor->MonikerName));
? ? }
? ? vectorDevices.clear();
//獲取麥克風設(shè)備信息
? ? GetAudioVideoInputDevices(vectorDevices, CLSID_AudioInputDeviceCategory);
? ? for (auto itor = vectorDevices.begin(); itor != vectorDevices.end(); ++itor)
? ? {
? ? ? ? //wprintf(L"FriendlyName = %ws, MonikerName = %ws\n", itor->FriendlyName, itor->MonikerName);
? ? ? ? printf("FriendlyName = %s, MonikerName = %s\n", WCharToChar(itor->FriendlyName), WCharToChar(itor->MonikerName));
? ? }
? ? vectorDevices.clear();
//獲取揚聲器設(shè)備信息
? ? GetAudioVideoInputDevices(vectorDevices, CLSID_AudioRendererCategory);
? ? for (auto itor = vectorDevices.begin(); itor != vectorDevices.end(); ++itor)
? ? {
? ? ? ? //wprintf(L"FriendlyName = %ws, MonikerName = %ws\n", itor->FriendlyName, itor->MonikerName);
? ? ? ? printf("FriendlyName = %s, MonikerName = %s\n", WCharToChar(itor->FriendlyName), WCharToChar(itor->MonikerName));
? ? }
? ? getchar();
? ? return 0;
}