//本机环境:vs2013+编译好的:BaseClasses
一.新建项目---配置
新建Win32项目->Unicode配置->删除_tWinMain自动生成代码即可!
二.引入头文件目录
三.引入库文件目录
四.导入头文件
五.枚举音频设备友好名(视频同理)
// DirectShowEnumDevice.cpp : 定义应用程序的入口点。
//
#include "stdafx.h"
#include "DirectShowEnumDevice.h"
#include <string>
using namespace std;
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPTSTR lpCmdLine,
_In_ int nCmdShow)
{
//CLSID_SystemDeviceEnum -> IID_ICreateDevEnum
//IID_ICreateDevEnum + CLSID_AudioInputDeviceCategory -> IEnumMoniker
//IEnumMoniker -> while -> IMoniker -> IPropertyBag -> Read -> FriendlyName
// -> IBaseFilter
//
HRESULT hr = ::CoInitialize(NULL);
if (FAILED(hr))
{
MessageBox(NULL, L"CoInitialize failed!", L"FailedMessageBox",NULL);
return 0;
}
//创建系统枚举器
ICreateDevEnum* pEnumDeviceEnum = NULL;
hr = CoCreateInstance(CLSID_SystemDeviceEnum, NULL, CLSCTX_INPROC_SERVER, IID_ICreateDevEnum, (void**)&pEnumDeviceEnum);
if (FAILED(hr))
{
MessageBox(NULL, L"CLSID_SystemDeviceEnum failed!", L"FailedMessageBox", NULL);
return 0;
}
//创建设备枚举器
IEnumMoniker* pEnumMoniker = NULL;
hr = pEnumDeviceEnum->CreateClassEnumerator(CLSID_AudioInputDeviceCategory, &pEnumMoniker, 0);
if (SUCCEEDED(hr))
{
//枚举Moniker
IMoniker* pMoniker = NULL;
IBaseFilter* pDeviceFilter = NULL;
ULONG ulCelt;
while (pEnumMoniker->Next(1, &pMoniker, &ulCelt) == S_OK)
{
//设备属性信息
IPropertyBag* pPropertyBag = NULL;
hr = pMoniker->BindToStorage(0, 0, IID_IPropertyBag, (void**)&pPropertyBag);
if (SUCCEEDED(hr))
{
VARIANT varName;
VariantInit(&varName); //初始化
//获取友好名
hr = pPropertyBag->Read(L"FriendlyName", &varName, 0);
if (SUCCEEDED(hr))
{
wstring strFriendlyName = varName.bstrVal;
MessageBox(NULL, strFriendlyName.c_str(), L"FriendlyNameMessageBox", NULL);
}
VariantClear(&varName); //清理
//IMoniker -> CreateDiviceFilter
hr = pMoniker->BindToObject(NULL, NULL, IID_IBaseFilter, (void**)pDeviceFilter);
if (FAILED(hr))
{
MessageBox(NULL, L"IMoniker IID_IBaseFilter failed!", L"FailedMessageBox", NULL);
continue;
}
MessageBox(NULL, L"IMoniker IID_IBaseFilter sucess!", L"SucessMessageBox", NULL);
pDeviceFilter->Release();
}
pPropertyBag->Release();
}
pMoniker->Release();
}
pEnumMoniker->Release();
pEnumDeviceEnum->Release();
CoUninitialize();
system("pause");
return 0;
}
//注:本人文档仅个人工作,学习所记
//转载:请注明出处,谢谢!