DirectShow之设备枚举器

//本机环境: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;
}

//注:本人文档仅个人工作,学习所记

//转载:请注明出处,谢谢!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值