如题。
在网上搜索了一下,很多人提出DirectShow可以实现。DirectShow在枚举设备时需要Filter的参数,但这些帖子却没有说明这个参数是什么。
用其它方式可以解决也非常感谢,请不吝赐教。

解决方案 »

  1.   


    /// <summary>视频输入设备信息</summary>
        internal class Device : IDisposable
        {
            #region API
            [DllImport("ole32.dll")]
            static extern int CreateBindCtx(int reserved, out IBindCtx ppbc);        [DllImport("ole32.dll", CharSet = CharSet.Unicode)]
            static extern int MkParseDisplayName(IBindCtx pbc, string szUserName, ref int pchEaten, out IMoniker ppmk);
            #endregion        #region GUID
            static readonly Guid SystemDeviceEnum = new Guid(0x62BE5D10, 0x60EB, 0x11D0, 0xBD, 0x3B, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86);        static readonly Guid VideoInputDevice = new Guid(0x860BB310, 0x5D01, 0x11D0, 0xBD, 0x3B, 0x00, 0xA0, 0xC9, 0x11, 0xCE, 0x86);
            #endregion        /// <summary>枚举本机上的视频设备</summary>
            public static IEnumerable<Device> Devices
            {
                get
                {
                    IMoniker[] ms = new IMoniker[5];
                    ICreateDevEnum enumD = Activator.CreateInstance(Type.GetTypeFromCLSID(SystemDeviceEnum)) as ICreateDevEnum;
                    IEnumMoniker moniker;
                    Guid g = VideoInputDevice;
                    if (enumD.CreateClassEnumerator(g, out moniker, 0) == 0)
                    {
                        while (true)
                        {
                            int r = moniker.Next(1, ms, IntPtr.Zero);
                            if (r != 0 || ms[0] == null)
                                break;
                            yield return new Device(ms[0]);
                            ms[0] = null;
                        }
                    }
                }
            }        /// <summary>根据设备路径创建设备</summary>
            /// <param name="path">设备路径</param>
            /// <returns>视频设备</returns>
            public static Device Create(string path)
            {
                IBindCtx bindCtx = null;
                IMoniker moniker = null;
                int n = 0;            if (CreateBindCtx(0, out bindCtx) == 0)
                {
                    int hr = MkParseDisplayName(bindCtx, path, ref n, out moniker);
                    hr.ThrowExceptionForHR();
                    Marshal.ReleaseComObject(bindCtx);
                    bindCtx = null;
                }
                if (moniker != null)
                    return new Device(moniker);
                else
                    return null;
            }        /// <summary>设备名称</summary>
            public string Name { get; private set; }        /// <summary>设备路径</summary>
            public string DevicePath { get; private set; }        public IMoniker Moniker { get; private set; }        private Device(IMoniker moniker)
            {
                Moniker = moniker;
                DevicePath = GetMonikerString(moniker);
                Name = GetName(moniker);
            }        internal IBaseFilter CreateFilter()
            {
                object filterObject = null;
                IBindCtx bindCtx = null;            if (CreateBindCtx(0, out bindCtx) == 0)
                {
                    Guid filterId = typeof(IBaseFilter).GUID;
                    Moniker.BindToObject(null, null, ref filterId, out filterObject);
                    Marshal.ReleaseComObject(bindCtx);
                }
                return filterObject as IBaseFilter;
            }        private string GetMonikerString(IMoniker moniker)
            {
                string str;
                moniker.GetDisplayName(null, null, out str);
                return str;
            }        private string GetName(IMoniker moniker)
            {
                Object bagObj = null;
                IPropertyBag bag = null;            try
                {
                    Guid bagId = typeof(IPropertyBag).GUID;
                    moniker.BindToStorage(null, null, ref bagId, out bagObj);
                    bag = (IPropertyBag)bagObj;                object val = "";
                    int hr = bag.Read("FriendlyName", out val, null);
                    if (hr != 0)
                        Marshal.ThrowExceptionForHR(hr);                string ret = (string)val;
                    if ((ret == null) || (ret.Length < 1))
                        throw new ApplicationException();                return ret;
                }
                catch (Exception)
                {
                    return "";
                }
                finally
                {
                    bag = null;
                    if (bagObj != null)
                    {
                        Marshal.ReleaseComObject(bagObj);
                        bagObj = null;
                    }
                }
            }        public override string ToString()
            {
                return Name;
            }        #region IDisposable 成员        public void Dispose()
            {
                Marshal.ReleaseComObject(Moniker);
                GC.SuppressFinalize(this);
            }        #endregion        ~Device() 
            {
                Dispose();
            }
        }