示例#1
0
        /// <summary>
        /// Retrieves the values of multiple properties by their index.
        /// Assumes that the GUID component of the property keys is the sensor's type GUID.
        /// </summary>
        /// <param name="propIndexes">The indexes of the properties to retrieve.</param>
        /// <returns>An array that contains the property values.</returns>
        /// <remarks>
        /// The returned array will contain null values for some properties if the values could not be retrieved.
        /// </remarks>
        public object[] GetProperties(params int[] propIndexes)
        {
            if (propIndexes == null || propIndexes.Length == 0)
            {
                throw new ArgumentNullException("propIndexes");
            }

            IPortableDeviceKeyCollection keyCollection = new PortableDeviceKeyCollection();

            try
            {
                IPortableDeviceValues         valuesCollection;
                Dictionary <PropertyKey, int> propKeyToIdx = new Dictionary <PropertyKey, int>();

                for (int i = 0; i < propIndexes.Length; i++)
                {
                    PropertyKey propKey = new PropertyKey(TypeId.Value, propIndexes[i]);
                    keyCollection.Add(ref propKey);
                    propKeyToIdx.Add(propKey, i);
                }

                object[] data = new object[propIndexes.Length];
                HResult  hr   = nativeISensor.GetProperties(keyCollection, out valuesCollection);
                if (hr == HResult.Ok)
                {
                    try
                    {
                        if (valuesCollection == null)
                        {
                            return(data);
                        }

                        uint count = 0;
                        valuesCollection.GetCount(ref count);

                        for (uint i = 0; i < count; i++)
                        {
                            PropertyKey propKey = new PropertyKey();
                            using (PropVariant propVal = new PropVariant())
                            {
                                valuesCollection.GetAt(i, ref propKey, propVal);

                                int idx = propKeyToIdx[propKey];
                                data[idx] = propVal.Value;
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(valuesCollection);
                        valuesCollection = null;
                    }
                }
                return(data);
            }
            finally
            {
                Marshal.ReleaseComObject(keyCollection);
            }
        }
示例#2
0
        /// <summary>
        /// Retrieves the values of multiple properties by property key.
        /// </summary>
        /// <param name="propKeys">An array of properties to retrieve.</param>
        /// <returns>A dictionary that contains the property keys and values.</returns>
        public IDictionary <PropertyKey, object> GetProperties(PropertyKey[] propKeys)
        {
            if (propKeys == null || propKeys.Length == 0)
            {
                throw new ArgumentNullException("propKeys", "Property keys array must not be null or empty.");
            }

            IPortableDeviceKeyCollection keyCollection = new PortableDeviceKeyCollection( );

            try
            {
                IPortableDeviceValues valuesCollection = null;

                for (int i = 0; i < propKeys.Length; i++)
                {
                    PropertyKey propKey = propKeys[i];
                    keyCollection.Add(ref propKey);
                }

                Dictionary <PropertyKey, object> data = new Dictionary <PropertyKey, object>( );
                HRESULT hr = nativeISensor.GetProperties(keyCollection, out valuesCollection);
                if (valuesCollection != null)
                {
                    try
                    {
                        uint count = 0;
                        valuesCollection.GetCount(ref count);

                        for (uint i = 0; i < count; i++)
                        {
                            PropertyKey propKey = new PropertyKey();
                            PropVariant propVal = new PropVariant();
                            valuesCollection.GetAt(i, ref propKey, ref propVal);

                            try
                            {
                                data.Add(propKey, propVal.Value);
                            }
                            finally
                            {
                                propVal.Clear( );
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(valuesCollection);
                        valuesCollection = null;
                    }
                }

                return(data);
            }
            finally
            {
                Marshal.ReleaseComObject(keyCollection);
                keyCollection = null;
            }
        }
示例#3
0
        /// <summary>
        /// Retrieves the values of multiple properties by property key.
        /// </summary>
        /// <param name="propKeys">An array of properties to retrieve.</param>
        /// <returns>A dictionary that contains the property keys and values.</returns>
        public IDictionary <PropertyKey, object> GetProperties(PropertyKey[] propKeys)
        {
            if (propKeys == null || propKeys.Length == 0)
            {
                throw new ArgumentException(LocalizedMessages.SensorEmptyProperties, "propKeys");
            }

            IPortableDeviceKeyCollection keyCollection = new PortableDeviceKeyCollection();

            try
            {
                IPortableDeviceValues valuesCollection;

                for (int i = 0; i < propKeys.Length; i++)
                {
                    PropertyKey propKey = propKeys[i];
                    keyCollection.Add(ref propKey);
                }

                Dictionary <PropertyKey, object> data = new Dictionary <PropertyKey, object>();
                HResult hr = nativeISensor.GetProperties(keyCollection, out valuesCollection);
                if (CoreErrorHelper.Succeeded(hr) && valuesCollection != null)
                {
                    try
                    {
                        uint count = 0;
                        valuesCollection.GetCount(ref count);

                        for (uint i = 0; i < count; i++)
                        {
                            PropertyKey propKey = new PropertyKey();
                            using (PropVariant propVal = new PropVariant())
                            {
                                valuesCollection.GetAt(i, ref propKey, propVal);
                                data.Add(propKey, propVal.Value);
                            }
                        }
                    }
                    finally
                    {
                        Marshal.ReleaseComObject(valuesCollection);
                        valuesCollection = null;
                    }
                }

                return(data);
            }
            finally
            {
                Marshal.ReleaseComObject(keyCollection);
                keyCollection = null;
            }
        }