DirectShowLibがうまく動いた

勘違いを直したらうまく動かせた。参考は


以下はDirectShowフィルタを列挙するコード。

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

namespace listfilters {
    class Program {
        static void Main(string[] args) {
            Console.WriteLine("Start");
            
            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,
            };
            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) {
                    object bagObj;
                    Guid propertyBagId = typeof(IPropertyBag).GUID;
                    moniker[0].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;
                    Console.WriteLine(name);

                    //object filterObj;
                    //Guid baseFilterId = typeof(IBaseFilter).GUID;
                    //moniker[0].BindToObject(null, null, ref baseFilterId, out filterObj);
                    //IBaseFilter filter = filterObj as IBaseFilter;
                }
            }
            Console.WriteLine("Start");
        }

        static object GetFromClsid(Guid clsid) {
            Type comType = Type.GetTypeFromCLSID(clsid);
            return Activator.CreateInstance(comType);
        }
    }
}

ビルドは

csc /r:DirectShowLib-2005.dll /platform:x86 /out:listfilters.exe Program.cs

ポイントは/platformオプションです。これがないと64ビット版では、64ビットのフィルタを、32ビット版では32ビットのフィルタを使うようになります。DirectShowフィルタはまだ32ビット版のほうが多いので、強制的に32ビットで動かすために/platformオプションをつけます。このオプション、Visual C# 2005 Express Editionだとどうやって指定するのかな。