Esempio n. 1
0
        /// <summary>
        /// Returns an array of DsDevices of type devcat.
        /// </summary>
        /// <param name="filterCategory">Any one of FilterCategory</param>
        public static DsDevice[] GetDevicesOfCat(Guid filterCategory)
        {
            // Use arrayList to build the retun list since it is easily resizable
            DsDevice[]   devret;
            ArrayList    devs = new ArrayList();
            IEnumMoniker enumMon;

            ICreateDevEnum enumDev = (ICreateDevEnum) new CreateDevEnum();
            var            hr      = enumDev.CreateClassEnumerator(filterCategory, out enumMon, 0);

            DsError.ThrowExceptionForHR(hr);

            // CreateClassEnumerator returns null for enumMon if there are no entries
            if (hr != 1)
            {
                try
                {
                    try
                    {
                        IMoniker[] mon = new IMoniker[1];
                        while ((enumMon.Next(1, mon, IntPtr.Zero) == 0))
                        {
                            try
                            {
                                // The devs array now owns this object.  Don't
                                // release it if we are going to be successfully
                                // returning the devret array
                                devs.Add(new DsDevice(mon[0]));
                            }
                            catch
                            {
                                DsUtils.ReleaseComObject(mon[0]);
                                throw;
                            }
                        }
                    }
                    finally
                    {
                        DsUtils.ReleaseComObject(enumMon);
                    }

                    // Copy the ArrayList to the DsDevice[]
                    devret = new DsDevice[devs.Count];
                    devs.CopyTo(devret);
                }
                catch
                {
                    foreach (DsDevice d in devs)
                    {
                        d.Dispose();
                    }
                    throw;
                }
            }
            else
            {
                devret = new DsDevice[0];
            }

            return(devret);
        }