예제 #1
0
        private IBaseFilter GetVideoInputObject()
        {
            object         videoInputObject = null;
            IEnumMoniker   classEnum;
            ICreateDevEnum devEnum = (ICreateDevEnum) new CreateDevEnum();

            hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, 0);
            DsError.ThrowExceptionForHR(hr);
            Marshal.ReleaseComObject(devEnum);

            if (null != classEnum)
            {
                IMoniker[] moniker = new IMoniker[1];
                if (classEnum.Next(moniker.Length, moniker, IntPtr.Zero) == DEVICE_ID)
                {
                    Guid iid = (typeof(IBaseFilter)).GUID;
                    moniker[0].BindToObject(null, null, ref iid, out videoInputObject);
                }

                Marshal.ReleaseComObject(moniker[0]);
                Marshal.ReleaseComObject(classEnum);
            }

            return((IBaseFilter)videoInputObject);
        }
예제 #2
0
        private Device[] GetDevices(Guid category)
        {
            List <Device>  devices    = new List <Device>();
            ICreateDevEnum enumerator = (ICreateDevEnum) new CreateDevEnum();
            IEnumMoniker   iterator;

            enumerator.CreateClassEnumerator(category, out iterator, 0);
            IMoniker[] moniker = new IMoniker[1];
            while (iterator.Next(1, moniker, IntPtr.Zero) == 0)
            {
                String friendly_name = "";
                String description   = "";

                object obj;
                moniker[0].BindToStorage(null, null, typeof(IPropertyBag).GUID, out obj);
                IPropertyBag bag = (IPropertyBag)obj;
                obj = null;
                bag.Read("FriendlyName", out obj, null);
                if (obj != null)
                {
                    friendly_name = obj.ToString();
                }
                bag.Read("Description", out obj, null);
                if (obj != null)
                {
                    description = obj.ToString();
                }
                moniker[0].BindToObject(null, null, typeof(IBaseFilter).GUID, out obj);
                if (obj != null)
                {
                    devices.Add(new Device((IBaseFilter)obj, friendly_name, description));
                }
            }
            return(devices.ToArray());
        }
예제 #3
0
        private Dictionary <string, IMoniker> EnumDevices(Guid type)
        {
            Dictionary <string, IMoniker> results = new Dictionary <string, IMoniker>();

            ICreateDevEnum enumDevices = (ICreateDevEnum) new CreateDevEnum();
            IEnumMoniker   enumMoniker;

            enumDevices.CreateClassEnumerator(type, out enumMoniker, 0);

            IMoniker[] monikers = new IMoniker[1];
            IntPtr     ptr      = IntPtr.Zero;

            while (enumMoniker.Next(1, monikers, ptr) == 0)
            {
                object obj;
                monikers[0].BindToStorage(null, null, typeof(IPropertyBag).GUID, out obj);
                IPropertyBag props = (IPropertyBag)obj;

                object friendlyName = null;
                object description  = null;
                props.Read("FriendlyName", ref friendlyName, null);
                props.Read("Description", ref description, null);

                results.Add((string)friendlyName, monikers[0]);
            }

            return(results);
        }
예제 #4
0
        /// <summary>
        /// Enumerates all video compressors.
        /// </summary>
        public static CompressorMoniker[] EnumCompressors()
        {
            ArrayList r = new ArrayList();

            ICreateDevEnum devEnum = (ICreateDevEnum) new SystemDeviceEnum();
            IEnumMoniker   e;

            devEnum.CreateClassEnumerator(ref VideoCompressorCategory, out e, 0);
            e.Reset();
            while (true)
            {
                uint     count;
                IMoniker moniker = null;
                e.RemoteNext(1, out moniker, out count);
                if (moniker == null || count == 0)
                {
                    break;
                }

                r.Add(new CompressorMoniker(moniker));
                Marshal.ReleaseComObject(moniker);
            }
            Marshal.ReleaseComObject(e);
            Marshal.ReleaseComObject(devEnum);

            return((CompressorMoniker[])r.ToArray(typeof(CompressorMoniker)));
        }
예제 #5
0
        private IBaseFilter FindCaptureDevice()
        {
            Debug.WriteLine("Start the Sub FindCaptureDevice");
            int hr = 0;
            UCOMIEnumMoniker classEnum = null;

            UCOMIMoniker[] moniker = new UCOMIMoniker[1];
            object         source  = null;
            ICreateDevEnum devEnum = (ICreateDevEnum) new CreateDevEnum();

            hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, CDef.None);
            Debug.WriteLine("Create an enumerator for the video capture devices : " + DsError.GetErrorText(hr));
            DsError.ThrowExceptionForHR(hr);
            Marshal.ReleaseComObject(devEnum);
            if (classEnum == null)
            {
                throw new ApplicationException("No video capture device was detected.\\r\\n\\r\\n" + "This sample requires a video capture device, such as a USB WebCam,\\r\\n" + "to be installed and working properly.  The sample will now close.");
            }
            int celt = 0;

            if (classEnum.Next(moniker.Length, moniker, out celt) == 0)
            {
                Guid iid = typeof(IBaseFilter).GUID;
                moniker[0].BindToObject(null, null, ref iid, out source);
            }
            else
            {
                throw new ApplicationException("Unable to access video capture device!");
            }
            Marshal.ReleaseComObject(moniker[0]);
            Marshal.ReleaseComObject(classEnum);
            return((IBaseFilter)source);
        }
예제 #6
0
        public void DoTests()
        {
            int hr;

            int            x;
            CreateDevEnum  cde     = new CreateDevEnum();
            ICreateDevEnum devEnum = cde as ICreateDevEnum;
            IEnumMoniker   em;

            IMoniker[] monikers = new IMoniker[5];
            IntPtr     p        = Marshal.AllocCoTaskMem(4);

            try
            {
                hr = devEnum.CreateClassEnumerator(FilterCategory.ActiveMovieCategories, out em, CDef.None);
                DsError.ThrowExceptionForHR(hr);

                hr = em.Next(monikers.Length, monikers, p);
                DsError.ThrowExceptionForHR(hr);

                x = Marshal.ReadInt32(p);
            }
            finally
            {
                Marshal.FreeCoTaskMem(p);
            }

            Debug.Assert(x > 0, "CreateClassEnumerator");
        }
예제 #7
0
        /// <summary>
        /// Enumerates available filters and returns a list of <see cref="CodecInfo"/>.
        /// </summary>
        /// <param name="filterCategory">GUID of filter category (<see cref="DirectShowLib.FilterCategory"/> members)></param>
        /// <returns></returns>
        public static List <CodecInfo> GetFiltersForCategory(Guid filterCategory)
        {
            List <CodecInfo> codecInfos  = new List <CodecInfo>();
            ICreateDevEnum   devEnum     = null;
            IEnumMoniker     enumMoniker = null;

            try
            {
                devEnum = (ICreateDevEnum) new CreateDevEnum();
                int catResult = devEnum.CreateClassEnumerator(filterCategory, out enumMoniker, CDef.None);
                if (catResult == 0)
                {
                    IMoniker[] moniker = new IMoniker[1];
                    while (enumMoniker.Next(1, moniker, IntPtr.Zero) == 0)
                    {
                        string    filterName    = FilterGraphTools.GetFriendlyName(moniker[0]);
                        Guid      filterClassId = FilterGraphTools.GetCLSID(moniker[0]);
                        CodecInfo codecInfo     = new CodecInfo(filterName, filterClassId);
                        codecInfos.Add(codecInfo);

                        FilterGraphTools.TryRelease(ref moniker[0]);
                    }
                }
                codecInfos.Sort();
                return(codecInfos);
            }
            finally
            {
                FilterGraphTools.TryRelease(ref enumMoniker);
                FilterGraphTools.TryRelease(ref devEnum);
            }
        }
예제 #8
0
        public void TestAddSourceFilterForMoniker()
        {
            int            hr          = 0;
            ICreateDevEnum devEnum     = (ICreateDevEnum) new CreateDevEnum();
            IEnumMoniker   enumMoniker = null;

            IMoniker[] monikers = new IMoniker[1];

            // In this method, i try to add an Audio Renderer by browsing the
            // AudioRenderer Filters Category...
            hr = devEnum.CreateClassEnumerator(FilterCategory.AudioRendererCategory, out enumMoniker, 0);
            Marshal.ThrowExceptionForHR(hr);
            Marshal.ReleaseComObject(devEnum);

            hr = enumMoniker.Next(1, monikers, IntPtr.Zero);
            Marshal.ThrowExceptionForHR(hr);

            hr = this.filterGraph2.AddSourceFilterForMoniker(monikers[0], null, "Audio Renderer from Moniker", out this.audioRenderer);
            Marshal.ThrowExceptionForHR(hr);

            Debug.Assert(hr == 0, "IFilterGraph2.AddSourceFilterForMoniker");

            Marshal.ReleaseComObject(enumMoniker);
            Marshal.ReleaseComObject(monikers[0]);
        }
예제 #9
0
        // This version of FindCaptureDevice is provide for education only.
        // A second version using the DsDevice helper class is define later.
        public IBaseFilter FindCaptureDevice()
        {
            int hr = 0;

#if USING_NET11
            UCOMIEnumMoniker classEnum = null;
            UCOMIMoniker[]   moniker   = new UCOMIMoniker[1];
#else
            IEnumMoniker classEnum = null;
            IMoniker[]   moniker   = new IMoniker[1];
#endif
            object source = null;

            // Create the system device enumerator
            ICreateDevEnum devEnum = (ICreateDevEnum) new CreateDevEnum();

            // Create an enumerator for the video capture devices
            hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, 0);
            DsError.ThrowExceptionForHR(hr);

            // The device enumerator is no more needed
            Marshal.ReleaseComObject(devEnum);

            // If there are no enumerators for the requested type, then
            // CreateClassEnumerator will succeed, but classEnum will be NULL.
            if (classEnum == null)
            {
                throw new ApplicationException("No video capture device was detected.\r\n\r\n" +
                                               "This sample requires a video capture device, such as a USB WebCam,\r\n" +
                                               "to be installed and working properly.  The sample will now close.");
            }

            // Use the first video capture device on the device list.
            // Note that if the Next() call succeeds but there are no monikers,
            // it will return 1 (S_FALSE) (which is not a failure).  Therefore, we
            // check that the return code is 0 (S_OK).
#if USING_NET11
            int i;
            if (classEnum.Next(moniker.Length, moniker, IntPtr.Zero) == 0)
#else
            if (classEnum.Next(moniker.Length, moniker, IntPtr.Zero) == 0)
#endif
            {
                // Bind Moniker to a filter object
                Guid iid = typeof(IBaseFilter).GUID;
                moniker[0].BindToObject(null, null, ref iid, out source);
            }
            else
            {
                throw new ApplicationException("Unable to access video capture device!");
            }

            // Release COM objects
            Marshal.ReleaseComObject(moniker[0]);
            Marshal.ReleaseComObject(classEnum);

            // An exception is thrown if cast fail
            return((IBaseFilter)source);
        }
예제 #10
0
        /// <summary>
        ///  This method gets a System.Runtime.InteropServices.ComTypes.IMoniker object.
        ///
        ///  HACK: The only way to create a System.Runtime.InteropServices.ComTypes.IMoniker from a moniker
        ///  string is to use System.Runtime.InteropServices.ComTypes.IMoniker.ParseDisplayName(). So I
        ///  need ANY System.Runtime.InteropServices.ComTypes.IMoniker object so that I can call
        ///  ParseDisplayName(). Does anyone have a better solution?
        ///
        ///  This assumes there is at least one video compressor filter
        ///  installed on the system.
        /// </summary>
        protected IMoniker getAnyMoniker()
        {
            Guid           category = FilterCategory.VideoCompressorCategory;
            int            hr;
            object         comObj  = null;
            ICreateDevEnum enumDev = null;
            IEnumMoniker   enumMon = null;

            if (mon != null)
            {
                return(mon[0]);
            }

            mon = new IMoniker[1];

            try
            {
                // Get the system device enumerator
                Type srvType = Type.GetTypeFromCLSID(ClassId.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(category, out enumMon, 0);
                if (hr != 0)
                {
                    throw new NotSupportedException("No devices of the category");
                }

                // Get first filter
                IntPtr f = IntPtr.Zero;
                hr = enumMon.Next(1, mon, f);
                if ((hr != 0))
                {
                    mon[0] = null;
                }

                return(mon[0]);
            }
            finally
            {
                enumDev = null;
                if (enumMon != null)
                {
                    DirectShowUtil.ReleaseComObject(enumMon);
                }
                enumMon = null;
                if (comObj != null)
                {
                    DirectShowUtil.ReleaseComObject(comObj);
                }
                comObj = null;
            }
        }
        /// <summary>
        /// 获取 所有摄像头,返回 List<CameraDevice>
        /// </summary>
        /// <returns></returns>
        public List <CameraDevice> GetCameraDeviceList()
        {
            List <CameraDevice> cameras = new List <CameraDevice>();
            Object         bagObj       = null;
            object         comObj       = null;
            ICreateDevEnum enumDev      = null;
            IEnumMoniker   enumMon      = null;

            IMoniker[]   moniker = new IMoniker[100];
            IPropertyBag bag     = null;

            try
            {
                // Get the system device enumerator
                Type srvType = Type.GetTypeFromCLSID(SystemDeviceEnum);
                // create device enumerator
                comObj  = Activator.CreateInstance(srvType);
                enumDev = (ICreateDevEnum)comObj;
                // Create an enumerator to find filters of specified category
                enumDev.CreateClassEnumerator(VideoInputDevice, out enumMon, 0);
                Guid bagId = typeof(IPropertyBag).GUID;
                int  i     = 0;

                while (enumMon.Next(1, moniker, IntPtr.Zero) == 0)
                {
                    CameraDevice cam = new CameraDevice();
                    // get property bag of the moniker
                    moniker[0].BindToStorage(null, null, ref bagId, out bagObj);
                    bag = (IPropertyBag)bagObj;
                    // read FriendlyName
                    object val = "";
                    bag.Read("FriendlyName", ref val, IntPtr.Zero);

                    cam.Index = i++;
                    cam.Title = (string)val;
                    cameras.Add(cam);
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                bag = null;
                if (bagObj != null)
                {
                    Marshal.ReleaseComObject(bagObj);
                    bagObj = null;
                }
                enumDev = null;
                enumMon = null;
                moniker = null;
            }

            return(cameras);
        }
예제 #12
0
    protected void Initialize()
    {
        FrameReady            = false;
        frame                 = new Texture2D(GraphicsDevice, Width, Height, false, SurfaceFormat.Color);
        FrameBGR              = new byte[(Width * Height) * 3];
        FrameRGBA             = new byte[(Width * Height) * 4];
        FrameGrayscale        = new byte[(Width * Height)];
        FrameHalfGrayscale    = new byte[(Width / 2 * Height / 2)];
        FrameQuarterGrayscale = new byte[(Width / 4 * Height / 4)];
        GraphBuilder          = (IGraphBuilder) new FilterGraph();
        CaptureGraphBuilder   = (ICaptureGraphBuilder2) new CaptureGraphBuilder2();
        MediaControl          = (IMediaControl)GraphBuilder;
        CaptureGraphBuilder.SetFiltergraph(GraphBuilder);
        object         VideoInputObject = null;
        IBaseFilter    VideoInput       = null;
        IEnumMoniker   classEnum;
        ICreateDevEnum devEnum = (ICreateDevEnum) new CreateDevEnum();

        devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, 0);
        Marshal.ReleaseComObject(devEnum);
        if (classEnum != null)
        {
            IMoniker[] moniker = new IMoniker[1];
            if (classEnum.Next(moniker.Length, moniker, IntPtr.Zero) == DEVICE_ID)
            {
                Guid iid = typeof(IBaseFilter).GUID;
                moniker[0].BindToObject(null, null, ref iid, out VideoInputObject);
            }
            Marshal.ReleaseComObject(moniker[0]);
            Marshal.ReleaseComObject(classEnum);
            VideoInput = (IBaseFilter)VideoInputObject;
        }
        if (VideoInput != null)
        {
            isRunning     = true;
            SampleGrabber = new SampleGrabber() as ISampleGrabber;
            GraphBuilder.AddFilter((IBaseFilter)SampleGrabber, "Render");
            AMMediaType Type = new AMMediaType()
            {
                majorType = MediaType.Video, subType = MediaSubType.RGB24, formatType = FormatType.VideoInfo
            };
            SampleGrabber.SetMediaType(Type);
            GraphBuilder.AddFilter(VideoInput, "Camera");
            SampleGrabber.SetBufferSamples(false);
            SampleGrabber.SetOneShot(false);
            SampleGrabber.GetConnectedMediaType(new AMMediaType());
            SampleGrabber.SetCallback((ISampleGrabberCB)this, 1);
            CaptureGraphBuilder.RenderStream(PinCategory.Preview, MediaType.Video, VideoInput, null, SampleGrabber as IBaseFilter);
            UpdateThread = new Thread(UpdateBuffer);
            UpdateThread.Start();
            MediaControl.Run();
            Marshal.ReleaseComObject(VideoInput);
        }
    }
예제 #13
0
        protected void FillList()
        {
            IEnumMoniker classEnum = null;

            IMoniker[] moniker = new IMoniker[1];

            ICreateDevEnum devEnum = (ICreateDevEnum) new CreateDevEnum();

            var hr = devEnum.CreateClassEnumerator(deviceClass, out classEnum, 0);

            DsError.ThrowExceptionForHR(hr);

            Marshal.ReleaseComObject(devEnum);
            devEnum = null;

            if (classEnum == null)
            {
                return;
            }

            int i = 0;

            while (classEnum.Next(moniker.Length, moniker, IntPtr.Zero) == 0)
            {
                if (TryGetBaseFilter(moniker[0], out var baseFilter))
                {
                    if (TryGetPropertyBag(moniker[0], out var propertyBag))
                    {
                        string name, displayName;
                        propertyBag.Read("FriendlyName", out var friendlyNameObj, null);
                        var friendlyName = friendlyNameObj.ToString();
                        displayName = friendlyName;
                        name        = friendlyName;
                        if (TryCreateEntry(name, displayName, baseFilter, propertyBag, out var entry))
                        {
                            list.Add(entry);
                        }
                        else
                        {
                            Marshal.ReleaseComObject(propertyBag);
                            Marshal.ReleaseComObject(baseFilter);
                        }
                    }
                    else
                    {
                        Marshal.ReleaseComObject(baseFilter);
                    }
                }
                Marshal.ReleaseComObject(moniker[0]);
                i++;
            }

            Marshal.ReleaseComObject(classEnum);
        }
예제 #14
0
파일: Camera.cs 프로젝트: x1234xx/DcRat
        private static void EnumMonikers(Guid category, Func <IMoniker, IPropertyBag, bool> func)
        {
            IEnumMoniker   enumerator = null;
            ICreateDevEnum device     = null;

            try
            {
                device = (ICreateDevEnum)Activator.CreateInstance(Type.GetTypeFromCLSID(CLSID_SystemDeviceEnum));
                device.CreateClassEnumerator(ref category, ref enumerator, 0);
                if (enumerator == null)
                {
                    return;
                }
                var monikers = new IMoniker[1];
                var fetched  = IntPtr.Zero;

                while (enumerator.Next(monikers.Length, monikers, fetched) == 0)
                {
                    var    moniker = monikers[0];
                    object value   = null;
                    Guid   guid    = IID_IPropertyBag;
                    moniker.BindToStorage(null, null, ref guid, out value);
                    var prop = (IPropertyBag)value;
                    try
                    {
                        var rc = func(moniker, prop);
                        if (rc == true)
                        {
                            break;
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(prop);
                        if (moniker != null)
                        {
                            Marshal.ReleaseComObject(moniker);
                        }
                    }
                }
            }
            finally
            {
                if (enumerator != null)
                {
                    Marshal.ReleaseComObject(enumerator);
                }
                if (device != null)
                {
                    Marshal.ReleaseComObject(device);
                }
            }
        }
예제 #15
0
        // Token: 0x06000341 RID: 833 RVA: 0x00013A08 File Offset: 0x00011C08
        protected void getFilters(Guid category)
        {
            object           obj = null;
            UCOMIEnumMoniker ucomienumMoniker = null;

            UCOMIMoniker[] array = new UCOMIMoniker[1];
            try
            {
                Type typeFromCLSID = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum);
                obj = RuntimeHelpers.GetObjectValue(Activator.CreateInstance(typeFromCLSID));
                ICreateDevEnum createDevEnum = (ICreateDevEnum)obj;
                int            num           = createDevEnum.CreateClassEnumerator(ref category, out ucomienumMoniker, 0);
                if (num != 0)
                {
                }
                for (;;)
                {
                    int num2;
                    num = ucomienumMoniker.Next(1, array, out num2);
                    if (num != 0)
                    {
                        break;
                    }
                    if (array[0] == null)
                    {
                        break;
                    }
                    Filter value = new Filter(array[0]);
                    this.InnerList.Add(value);
                    Marshal.ReleaseComObject(array[0]);
                    array[0] = null;
                }
                this.InnerList.Sort();
            }
            finally
            {
                if (array[0] != null)
                {
                    Marshal.ReleaseComObject(array[0]);
                }
                array[0] = null;
                if (ucomienumMoniker != null)
                {
                    Marshal.ReleaseComObject(ucomienumMoniker);
                }
                ucomienumMoniker = null;
                if (obj != null)
                {
                    Marshal.ReleaseComObject(RuntimeHelpers.GetObjectValue(obj));
                }
                obj = null;
            }
        }
예제 #16
0
        private void CollectFilters(Guid category)
        {
            object         obj           = null;
            ICreateDevEnum createDevEnum = null;
            IEnumMoniker   enumMoniker   = null;

            IMoniker[] array = new IMoniker[1];
            try
            {
                Type typeFromCLSID = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum);
                if (typeFromCLSID == null)
                {
                    throw new ApplicationException("Failed creating device enumerator");
                }
                obj           = Activator.CreateInstance(typeFromCLSID);
                createDevEnum = (ICreateDevEnum)obj;
                if (createDevEnum.CreateClassEnumerator(ref category, out enumMoniker, 0) != 0)
                {
                    throw new ApplicationException("No devices of the category");
                }
                IntPtr zero = IntPtr.Zero;
                while (enumMoniker.Next(1, array, zero) == 0 && array[0] != null)
                {
                    FilterInfo value = new FilterInfo(array[0]);
                    base.InnerList.Add(value);
                    Marshal.ReleaseComObject(array[0]);
                    array[0] = null;
                }
                base.InnerList.Sort();
            }
            catch
            {
            }
            finally
            {
                createDevEnum = null;
                if (obj != null)
                {
                    Marshal.ReleaseComObject(obj);
                    obj = null;
                }
                if (enumMoniker != null)
                {
                    Marshal.ReleaseComObject(enumMoniker);
                    enumMoniker = null;
                }
                if (array[0] != null)
                {
                    Marshal.ReleaseComObject(array[0]);
                    array[0] = null;
                }
            }
        }
예제 #17
0
        /// <summary>
        ///  This method gets a UCOMIMoniker object.
        ///
        ///  HACK: The only way to create a UCOMIMoniker from a moniker
        ///  string is to use UCOMIMoniker.ParseDisplayName(). So I
        ///  need ANY UCOMIMoniker object so that I can call
        ///  ParseDisplayName(). Does anyone have a better solution?
        ///
        ///  This assumes there is at least one video compressor filter
        ///  installed on the system.
        /// </summary>
        protected UCOMIMoniker GetAnyMoniker()
        {
            Guid             category = Uuid.FilterCategory.VideoCompressorCategory;
            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(Uuid.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");
                }

                // Get first filter
                int f;
                hr = enumMon.Next(1, mon, out f);
                if ((hr != 0))
                {
                    mon[0] = null;
                }

                return(mon[0]);
            }
            finally
            {
                enumDev = null;
                if (enumMon != null)
                {
                    Marshal.ReleaseComObject(enumMon);
                }
                enumMon = null;
                if (comObj != null)
                {
                    Marshal.ReleaseComObject(comObj);
                }
                comObj = null;
            }
        }
예제 #18
0
        private void ListVideoDevices(ComboBox xcombobox)
        {
            Object         bagObj  = null;
            object         comObj  = null;
            ICreateDevEnum enumDev = null;
            IEnumMoniker   enumMon = null;

            IMoniker[]   moniker = new IMoniker[100];
            IPropertyBag bag     = null;

            try
            {
                // Get the system device enumerator
                Type srvType = Type.GetTypeFromCLSID(SystemDeviceEnum);
                // create device enumerator
                comObj  = Activator.CreateInstance(srvType);
                enumDev = (ICreateDevEnum)comObj;
                // Create an enumerator to find filters of specified category
                enumDev.CreateClassEnumerator(VideoInputDevice, out enumMon, 0);
                Guid bagId = typeof(IPropertyBag).GUID;
                while (enumMon.Next(1, moniker, IntPtr.Zero) == 0)
                {
                    // get property bag of the moniker
                    moniker[0].BindToStorage(null, null, ref bagId, out bagObj);
                    bag = (IPropertyBag)bagObj;
                    // read FriendlyName
                    object val = "";
                    bag.Read("FriendlyName", ref val, IntPtr.Zero);
                    //list in box
                    xcombobox.Items.Add((string)val);
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                bag = null;
                if (bagObj != null)
                {
                    Marshal.ReleaseComObject(bagObj);
                    bagObj = null;
                }
                enumDev = null;
                enumMon = null;
                moniker = null;
            }
            if (xcombobox.Items.Count > 0)
            {
                xcombobox.SelectedIndex = 0;
            }
        }
예제 #19
0
        protected void getFilters(Guid category)
        {
            object           o             = null;
            ICreateDevEnum   enum2         = null;
            UCOMIEnumMoniker ppEnumMoniker = null;

            UCOMIMoniker[] rgelt = new UCOMIMoniker[1];
            try
            {
                int  num;
                Type typeFromCLSID = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum);
                if (typeFromCLSID == null)
                {
                    throw new NotImplementedException("System Device Enumerator");
                }
                o     = Activator.CreateInstance(typeFromCLSID);
                enum2 = (ICreateDevEnum)o;
                if (enum2.CreateClassEnumerator(ref category, out ppEnumMoniker, 0) != 0)
                {
                    throw new NotSupportedException("No devices of the category");
                }
                while ((ppEnumMoniker.Next(1, rgelt, out num) == 0) && (rgelt[0] != null))
                {
                    Filter filter = new Filter(rgelt[0]);
                    base.InnerList.Add(filter);
                    Marshal.ReleaseComObject(rgelt[0]);
                    rgelt[0] = null;
                }
                base.InnerList.Sort();
            }
            finally
            {
                enum2 = null;
                if (rgelt[0] != null)
                {
                    Marshal.ReleaseComObject(rgelt[0]);
                }
                rgelt[0] = null;
                if (ppEnumMoniker != null)
                {
                    Marshal.ReleaseComObject(ppEnumMoniker);
                }
                ppEnumMoniker = null;
                if (o != null)
                {
                    Marshal.ReleaseComObject(o);
                }
                o = null;
            }
        }
예제 #20
0
        /// <summary>
        ///  This method gets a UCOMIMoniker object.
        ///
        ///  HACK: The only way to create a UCOMIMoniker from a moniker
        ///  string is to use UCOMIMoniker.ParseDisplayName(). So I
        ///  need ANY UCOMIMoniker object so that I can call
        ///  ParseDisplayName(). Does anyone have a better solution?
        ///
        ///  This assumes there is at least one video compressor filter
        ///  installed on the system.
        /// </summary>
        protected System.Runtime.InteropServices.ComTypes.IMoniker getAnyMoniker()
        {
            Guid   category = FilterCategory.VideoCompressorCategory;
            object comObj   = null;

            System.Runtime.InteropServices.ComTypes.IEnumMoniker enumMon = null;

            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);

                ICreateDevEnum enumDev = (ICreateDevEnum) new CreateDevEnum();

                // Create an enumerator to find filters in category
                int hr = enumDev.CreateClassEnumerator(category, out enumMon, 0);
                //int hr = enumDev.CreateClassEnumerator(ref category, out enumMon, 0);
                if (hr != 0)
                {
                    throw new NotSupportedException("No devices of the category");
                }

                // Get first filter
                IntPtr f   = IntPtr.Zero;
                var    mon = new System.Runtime.InteropServices.ComTypes.IMoniker[1];
                hr = enumMon.Next(1, mon, f);
                if ((hr != 0))
                {
                    mon[0] = null;
                }

                return(mon[0]);
            }
            finally
            {
                if (enumMon != null)
                {
                    Marshal.ReleaseComObject(enumMon);
                }
                enumMon = null;
                if (comObj != null)
                {
                    Marshal.ReleaseComObject(comObj);
                }
                comObj = null;
            }
        }
예제 #21
0
        /// <summary>
        /// Enumerate the devices and show them in ListView
        /// </summary>
        private void EnumerateDevices()
        {
            ICreateDevEnum deviceEnum  = null;
            IEnumMoniker   monikerEnum = null;

            IMoniker[] monikers = new IMoniker[1];

            int hr = 0;

            try
            {
                // create the device enumerator (COM) object
                Type t = Type.GetTypeFromCLSID(ComGuids.DevEnumGuid);
                deviceEnum = (ICreateDevEnum)Activator.CreateInstance(t);

                hr = deviceEnum.CreateClassEnumerator(ComGuids.VidCapGuid, out monikerEnum, CDef.None);
                if (hr != 0)
                {
                    UIMessage.Error("No devices of the category", TITLE);
                    return;
                }

                while (monikerEnum.Next(1, monikers, IntPtr.Zero) == 0)
                {
                    string displayName = this.GetFriendlyName(monikers[0]);

                    ListViewItem item = new ListViewItem(displayName);
                    item.Tag = new Device(displayName, monikers[0]);
                    this.lstDevices.Items.Add(item);
                }
            }
            catch (Exception exc)
            {
                UIMessage.Error(exc.Message, TITLE);
            }
            finally
            {
                if (deviceEnum != null)
                {
                    Marshal.ReleaseComObject(deviceEnum);
                }
                if (monikerEnum != null)
                {
                    Marshal.ReleaseComObject(monikerEnum);
                }
            }
        }
예제 #22
0
        //
        // get a list of video capture devices found in the system
        //
        private void FindDevice()
        {
            ICreateDevEnum de = null;

            try
            {
                // create the device enumerator (COM) object
                Type t = Type.GetTypeFromCLSID(CLSID_SystemDeviceEnum);
                de = (ICreateDevEnum)Activator.CreateInstance(t);

                IEnumMoniker em;

                // get an enumerator for the video caputre devices
                de.CreateClassEnumerator(ref CLSID_VideoInputDeviceCategory, out em, 0);

                IMoniker     mon;
                uint         result;
                object       o, name;
                IPropertyBag pBag;

                // get a reference to the 1st device
                em.RemoteNext(1, out mon, out result);

                while (result == 1)
                {
                    // get our device ready
                    mon.RemoteBindToObject(null, null, ref IID_IBaseFilter, out o);
                    this.SourceFilterList.Add(o);
                    // get device FriendlyName
                    mon.RemoteBindToStorage(null, null, ref IID_IPropertyBag, out o);
                    pBag = (IPropertyBag)o;
                    pBag.RemoteRead("FriendlyName", out name, null, 0, null);

                    this.cbxDevice.Items.Add((string)name);
                    // go to next device
                    em.RemoteNext(1, out mon, out result);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error when find device: " + ex.Message);
            }
            finally
            {
                Marshal.ReleaseComObject(de);
            }
        }
예제 #23
0
        protected UCOMIMoniker getAnyMoniker()
        {
            UCOMIMoniker     moniker;
            Guid             videoCompressorCategory = FilterCategory.VideoCompressorCategory;
            object           o             = null;
            ICreateDevEnum   enum2         = null;
            UCOMIEnumMoniker ppEnumMoniker = null;

            UCOMIMoniker[] rgelt = new UCOMIMoniker[1];
            try
            {
                int  num;
                Type typeFromCLSID = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum);
                if (typeFromCLSID == null)
                {
                    throw new NotImplementedException("System Device Enumerator");
                }
                o     = Activator.CreateInstance(typeFromCLSID);
                enum2 = (ICreateDevEnum)o;
                if (enum2.CreateClassEnumerator(ref videoCompressorCategory, out ppEnumMoniker, 0) != 0)
                {
                    throw new NotSupportedException("No devices of the category");
                }
                if (ppEnumMoniker.Next(1, rgelt, out num) != 0)
                {
                    rgelt[0] = null;
                }
                moniker = rgelt[0];
            }
            finally
            {
                enum2 = null;
                if (ppEnumMoniker != null)
                {
                    Marshal.ReleaseComObject(ppEnumMoniker);
                }
                ppEnumMoniker = null;
                if (o != null)
                {
                    Marshal.ReleaseComObject(o);
                }
                o = null;
            }
            return(moniker);
        }
예제 #24
0
        public static bool HasCamera()
        {
            bool           hascamera = false;
            ICreateDevEnum de        = null;

            try
            {
                // create the device enumerator (COM) object
                Type t = Type.GetTypeFromCLSID(CLSID_SystemDeviceEnum);
                de = (ICreateDevEnum)Activator.CreateInstance(t);

                IEnumMoniker em;

                // get an enumerator for the video caputre devices
                de.CreateClassEnumerator(ref CLSID_VideoInputDeviceCategory, out em, 0);

                IMoniker mon;
                uint     result = 0;

                // get a reference to the 1st device
                if (em != null)
                {
                    em.RemoteNext(1, out mon, out result);
                }

                if (result == 1)
                {
                    hascamera = true;
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                Marshal.ReleaseComObject(de);
            }

            return(hascamera);
        }
예제 #25
0
        // Token: 0x0600033E RID: 830 RVA: 0x0001391C File Offset: 0x00011B1C
        protected UCOMIMoniker getAnyMoniker()
        {
            Guid             videoCompressorCategory = FilterCategory.VideoCompressorCategory;
            object           obj = null;
            UCOMIEnumMoniker ucomienumMoniker = null;

            UCOMIMoniker[] array = new UCOMIMoniker[1];
            UCOMIMoniker   result;

            try
            {
                Type typeFromCLSID = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum);
                obj = RuntimeHelpers.GetObjectValue(Activator.CreateInstance(typeFromCLSID));
                ICreateDevEnum createDevEnum = (ICreateDevEnum)obj;
                int            num           = createDevEnum.CreateClassEnumerator(ref videoCompressorCategory, out ucomienumMoniker, 0);
                int            num2;
                num = ucomienumMoniker.Next(1, array, out num2);
                if (num != 0)
                {
                    array[0] = null;
                }
                result = array[0];
            }
            finally
            {
                if (ucomienumMoniker != null)
                {
                    Marshal.ReleaseComObject(ucomienumMoniker);
                }
                ucomienumMoniker = null;
                if (obj != null)
                {
                    Marshal.ReleaseComObject(RuntimeHelpers.GetObjectValue(obj));
                }
                obj = null;
            }
            return(result);
        }
예제 #26
0
        //カメラデバイスにアクセス
        private IBaseFilter FindCaptureDevice()
        {
            ArrayList    devs      = new ArrayList();
            IEnumMoniker classEnum = null;

            IMoniker[] moniker = { null };

            object         source  = null;
            ICreateDevEnum devEnum = (ICreateDevEnum)(new CreateDevEnum());

            int hr = devEnum.CreateClassEnumerator(FilterCategory.VideoInputDevice, out classEnum, CDef.None);

            DsError.ThrowExceptionForHR(hr);

            if (classEnum == null)
            {
                throw new ApplicationException("No video capture device was detected.\\r\\n\\r\\n" + "This sample requires a video capture device, such as a USB WebCam,\\r\\nto be installed and working properly.  The sample will now close.");
            }

            IntPtr none = IntPtr.Zero;

            while (classEnum.Next(1, moniker, none) == 0)
            {
                Guid iid = typeof(IBaseFilter).GUID;
                moniker[0].BindToObject(null, null, ref iid, out source);
                Marshal.ReleaseComObject(moniker[0]);
                devs.Add(source);
            }

            Debug.WriteLine(devs.Count + " ==== cameras found");

            Marshal.ReleaseComObject(devEnum);
            Marshal.ReleaseComObject(classEnum);

            //we can now select cameras
            return((IBaseFilter)devs[0]);
        }
예제 #27
0
        private Dictionary <string, IMoniker> EnumDevices(Guid type)
        {
            Dictionary <string, IMoniker> results = new Dictionary <string, IMoniker>();

            ICreateDevEnum enumDevices = (ICreateDevEnum) new CreateDevEnum();
            IEnumMoniker   enumMoniker;

            enumDevices.CreateClassEnumerator(type, out enumMoniker, 0);

            IMoniker[] monikers = new IMoniker[1];
            IntPtr     ptr      = IntPtr.Zero;

            try
            {
                while (enumMoniker.Next(1, monikers, ptr) == 0)
                {
                    object obj;
                    monikers[0].BindToStorage(null, null, typeof(IPropertyBag).GUID, out obj);
                    IPropertyBag props = (IPropertyBag)obj;

                    object friendlyName = null;
                    object description  = null;
                    props.Read("FriendlyName", ref friendlyName, null);
                    props.Read("Description", ref description, null);

                    results.Add((string)friendlyName, monikers[0]);
                }
            }
            catch (Exception)
            {
                System.Windows.Forms.MessageBox.Show("Der kunne ikke findes et kamera.", "Ikke noget kamera", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
            }


            return(results);
        }
        public static bool GetDevicesOfCat(Guid cat, out ArrayList devs)
        {
            devs = null;
            int              hr;
            object           comObj  = null;
            ICreateDevEnum   enumDev = null;
            UCOMIEnumMoniker enumMon = null;

            UCOMIMoniker[] mon = new UCOMIMoniker[1];
            try
            {
                Type srvType = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum);
                if (srvType == null)
                {
                    throw new NotImplementedException("System Device Enumerator");
                }

                comObj  = Activator.CreateInstance(srvType);
                enumDev = (ICreateDevEnum)comObj;
                hr      = enumDev.CreateClassEnumerator(ref cat, out enumMon, 0);
                if (hr != 0)
                {
                    throw new NotSupportedException("No devices of the category");
                }

                int f, count = 0;
                do
                {
                    hr = enumMon.Next(1, mon, out f);
                    if ((hr != 0) || (mon[0] == null))
                    {
                        break;
                    }
                    DsDevice dev = new DsDevice();
                    dev.Name = GetFriendlyName(mon[0]);
                    if (devs == null)
                    {
                        devs = new ArrayList();
                    }
                    dev.Mon            = mon[0]; mon[0] = null;
                    devs.Add(dev); dev = null;
                    count++;
                }while (true);

                return(count > 0);
            }
            catch (Exception)
            {
                if (devs != null)
                {
                    foreach (DsDevice d in devs)
                    {
                        d.Dispose();
                    }
                    devs = null;
                }
                return(false);
            }
            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;
            }
        }
예제 #29
0
        // Collect filters of specified category
        private void CollectFilters(Guid category)
        {
            object         comObj  = null;
            ICreateDevEnum enumDev = null;
            IEnumMoniker   enumMon = null;
            var            devMon  = new IMoniker[1];
            int            hr;

            try
            {
                // Get the system device enumerator
                var srvType = Type.GetTypeFromCLSID(Clsid.SystemDeviceEnum);
                if (srvType == null)
                {
                    throw new ApplicationException("Failed creating device enumerator");
                }

                // create device enumerator
                comObj  = Activator.CreateInstance(srvType);
                enumDev = (ICreateDevEnum)comObj;

                // Create an enumerator to find filters of specified category
                hr = enumDev.CreateClassEnumerator(ref category, out enumMon, 0);
                if (hr != 0)
                {
                    throw new ApplicationException("No devices of the category");
                }

                // Collect all filters
                var n = IntPtr.Zero;
                while (true)
                {
                    // Get next filter
                    hr = enumMon.Next(1, devMon, n);
                    if ((hr != 0) || (devMon[0] == null))
                    {
                        break;
                    }

                    // Add the filter
                    var filter = new FilterInfo(devMon[0]);
                    InnerList.Add(filter);

                    // Release COM object
                    Marshal.ReleaseComObject(devMon[0]);
                    devMon[0] = null;
                }

                // Sort the collection
                InnerList.Sort();
            }
            catch
            {
            }
            finally
            {
                // release all COM objects
                enumDev = null;
                if (comObj != null)
                {
                    Marshal.ReleaseComObject(comObj);
                    comObj = null;
                }
                if (enumMon != null)
                {
                    Marshal.ReleaseComObject(enumMon);
                    enumMon = null;
                }
                if (devMon[0] != null)
                {
                    Marshal.ReleaseComObject(devMon[0]);
                    devMon[0] = null;
                }
            }
        }
예제 #30
0
        public static IBaseFilter CreateCaptureDevice(Guid filterCategory, string captureDeviceName)
        {
            // FilterCategory.VideoInputDevice, FilterCategory.AudioInputDevice

            ICreateDevEnum devEnum = null;
            IEnumMoniker   enumCat = null;

            IMoniker[] moniker = new IMoniker[1] {
                null
            };
            IPropertyBag propBag = null;

            int hr = 0;

            try
            {
                devEnum = new CreateDevEnum() as ICreateDevEnum;
                if (devEnum == null)
                {
                    throw new COMException("Cannot create CLSID_SystemDeviceEnum");
                }

                // Obtain enumerator for the capture category.
                hr = devEnum.CreateClassEnumerator(filterCategory, out enumCat, 0);
                DsError.ThrowExceptionForHR(hr);

                if (enumCat == null)
                {
                    return(null);
                }

                // Enumerate the monikers.
                IntPtr fetchedCount = new IntPtr(0);
                while (0 == enumCat.Next(1, moniker, fetchedCount))
                {
                    Guid   bagId = typeof(IPropertyBag).GUID;
                    object bagObj;
                    moniker[0].BindToStorage(null, null, ref bagId, out bagObj);

                    propBag = bagObj as IPropertyBag;

                    if (propBag != null)
                    {
                        object val;
                        string friendlyName = null;
                        string displayName  = null;

                        hr = propBag.Read("FriendlyName", out val, null);
                        if (hr == 0)
                        {
                            friendlyName = val as string;
                        }

                        Util.ReleaseComObject(ref propBag);

                        moniker[0].GetDisplayName(null, null, out displayName);

                        // create an instance of the filter
                        Guid   baseFilterId = typeof(IBaseFilter).GUID;
                        object filter;
                        try
                        {
                            moniker[0].BindToObject(null, null, ref baseFilterId, out filter);
                            if (filter != null)
                            {
                                if ((captureDeviceName == null) || (captureDeviceName == friendlyName))
                                {
                                    return(filter as IBaseFilter);
                                }

                                Util.ReleaseComObject(ref filter);
                            }
                        }
                        catch
                        {
                            System.Diagnostics.Trace.WriteLine("Cannot use input device " + friendlyName);
                        }
                    } // if IPropertyBag

                    Util.ReleaseComObject(ref moniker[0]);
                } // while enum devices
            }
            catch (COMException ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.ToString());
            }
            finally
            {
                Util.ReleaseComObject(ref propBag);
                Util.ReleaseComObject(ref moniker[0]);
                Util.ReleaseComObject(ref enumCat);
                Util.ReleaseComObject(ref devEnum);
            }

            return(null);
        } // EnumInputDev