zoukankan      html  css  js  c++  java
  • 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;
    }
    好的代码像粥一样,都是用时间熬出来的
  • 相关阅读:
    Longest Subsequence CodeForces
    Alyona and towers CodeForces
    Electric Charges CodeForces
    Array GCD CodeForces
    笔记(模拟)
    城堡 (spfa+cheng)
    YOU ARE MY SUNSHINE
    AC日记——潜伏者 洛谷 P1071 (模拟)
    AC日记——神奇的幻方 洛谷 P2615(大模拟)
    AC日记——机器翻译 洛谷 P1540
  • 原文地址:https://www.cnblogs.com/jijm123/p/14395915.html
Copyright © 2011-2022 走看看