Hatena::ブログ(Diary)

ラシウラ このページをアンテナに追加 RSSフィード Twitter

2007-07-19

DirectShowフィルタとPinの名前を列挙するコード

完成版↓

はてなグループが重いので一時退避

using System;
using System.Security.Permissions;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using DirectShowLib;

namespace listfilters {
  class Program {
    //[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    static void Main(string[] args) {
      Guid clsidSystemDeviceEnum = new Guid("62BE5D10-60EB-11d0-BD3B-00A0C911CE86");
      ICreateDevEnum devEnum = GetFromClsid(clsidSystemDeviceEnum) as ICreateDevEnum;
      Guid[] categories = new Guid[] {
        FilterCategory.LegacyAmFilterCategory,
        FilterCategory.AudioCompressorCategory,
        FilterCategory.VideoCompressorCategory,
        //FilterCategory.BDASourceFiltersCategory,
      };
      foreach (Guid category in categories) {
        //Console.WriteLine();
        //Console.WriteLine("new category: " + category);
        
        IEnumMoniker monikers;
        devEnum.CreateClassEnumerator(category, out monikers, CDef.None);
        monikers.Reset();
        IntPtr count = new IntPtr();
        IMoniker[] moniker = new IMoniker[1];
        while (monikers.Next(1, moniker, count) == 0) {
          Console.WriteLine("Filter: \"" + GetFilterName(moniker[0]) + "\"");
          try {
            IBaseFilter filter = GetFilter(moniker[0]);
            IEnumPins pins;
            filter.EnumPins(out pins);
            int pinCount;
            IPin[] pin = new IPin[1];
            while (pins.Next(1, pin, out pinCount) == 0) {
              PinInfo info;
              pin[0].QueryPinInfo(out info);
              Console.WriteLine("  Pin " + info.dir + ": \"" + info.name + "\"");
            }
          } catch (Exception ex) {
            //Console.WriteLine(ex);
          } finally {
            Marshal.ReleaseComObject(moniker[0]);
          }
        }
        Marshal.ReleaseComObject(monikers);
      }
      Marshal.ReleaseComObject(devEnum);
    }
    
    static IBaseFilter GetFilter(IMoniker filterMoniker) {
      object filterObj;
      Guid baseFilterId = typeof(IBaseFilter).GUID;
      filterMoniker.BindToObject(null, null, ref baseFilterId, out filterObj);
      IBaseFilter filter = filterObj as IBaseFilter;
      return filter;
    }
    
    static string GetFilterName(IMoniker filterMoniker) {
      object bagObj;
      Guid propertyBagId = typeof(IPropertyBag).GUID;
      filterMoniker.BindToStorage(null, null, ref propertyBagId, out bagObj);
      IPropertyBag bag = bagObj as IPropertyBag;
      object nameObj;
      bag.Read("FriendlyName", out nameObj, null);
      string name = nameObj as string;
      Marshal.ReleaseComObject(bagObj);
      return name;
    }
    
    static object GetFromClsid(Guid clsid) {
      Type comType = Type.GetTypeFromCLSID(clsid);
      return Activator.CreateInstance(comType);
    }
  }
}

はてなユーザーのみコメントできます。はてなへログインもしくは新規登録をおこなってください。

リンク元