使用 DirectShow 获取筛选器(COM 设备)

本文关键字:COM 设备 DirectShow 获取 筛选 使用 | 更新日期: 2023-09-27 18:37:25

来自代码项目 我得到了这段代码,但我不明白

此函数应在其类别之后获取所有过滤器(com设备),并将其填充在内部列表中

有人可以详细解释我搜索的每个部分对我来说都是新的吗我不明白什么是ICreateDevEnum,UCOMIEnumMonikerUCOMIMoniker以及我们如何获得使用它们的过滤器

protected void getFilters(Guid category)
        {
            int                 hr;
            object              comObj = null;
            ICreateDevEnum      enumDev = null;
            UCOMIEnumMoniker    enumMon = null;
            UCOMIMoniker[]      mon = new UCOMIMoniker[1];
            try 
            {
                // Get the system device enumerator
                Type srvType = Type.GetTypeFromCLSID( Clsid.SystemDeviceEnum );
                if( srvType == null )
                    throw new NotImplementedException( "System Device Enumerator" );
                comObj = Activator.CreateInstance( srvType );
                enumDev = (ICreateDevEnum) comObj;
                // Create an enumerator to find filters in category
                hr = enumDev.CreateClassEnumerator( ref category, out enumMon, 0 );
                if( hr != 0 )
                    throw new NotSupportedException( "No devices of the category" );
                // Loop through the enumerator
                int f;
                do
                {
                    // Next filter
                    hr = enumMon.Next( 1, mon, out f );
                    if( (hr != 0) || (mon[0] == null) )
                        break;
                    // Add the filter
                    Filter filter = new Filter( mon[0] );
                    InnerList.Add( filter );
                    // Release resources
                    Marshal.ReleaseComObject( mon[0] );
                    mon[0] = null;
                }
                while(true);
                // Sort
                InnerList.Sort();
            }
            finally
            {
                enumDev = null;
                if( mon[0] != null )
                    Marshal.ReleaseComObject( mon[0] ); mon[0] = null;
                if( enumMon != null )
                    Marshal.ReleaseComObject( enumMon ); enumMon = null;
                if( comObj != null )
                    Marshal.ReleaseComObject( comObj ); comObj = null;
            }
        }

使用 DirectShow 获取筛选器(COM 设备)

您使用的是本机 API 上的 [未记录] 托管包装器,但是 API 本身在 MSDN 上有很好的文档记录,并且接口名称具有直接映射。

请参阅使用系统设备枚举器,其中描述了有问题的标识符。

若要使用系统设备枚举器,请执行以下操作:

  1. 通过调用 CoCreateInstance 创建系统设备枚举器。类标识符 (CLSID) CLSID_SystemDeviceEnum。
  2. 通过使用所需类别的 CLSID 调用 ICreateDevEnum::CreateClassEnumerator 来获取类别枚举器。此方法返回 IEnumMoniker 接口指针。如果类别为空(或不存在),该方法将返回S_FALSE而不是错误代码。如果是这样,则返回的 IEnumMoniker 指针为 NULL,取消引用它将导致异常。[...]