/// <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);
            }
        }
Example #2
0
        /// <summary>
        /// Retrives the values of multiple properties by their indices.
        /// Assues that the GUID component of property keys is the sensor's type GUID.
        /// </summary>
        /// <param name="propIndices">Indices of properties to retrieve.</param>
        /// <returns>An array containing the property values.</returns>
        /// <remarks>
        /// If the values of some properties could not be retrieved, then the returned array will contain null values in the corresponding positions.
        /// </remarks>
        public object[] GetProperties(params int[] propIndices)
        {
            if (propIndices == null || propIndices.Length == 0)
            {
                throw new ArgumentNullException("propIndices", "Property keys array must not be null or empty.");
            }

            IPortableDeviceKeyCollection keyCollection = new PortableDeviceKeyCollection();

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

                for (int i = 0; i < propIndices.Length; i++)
                {
                    PropertyKey propKey = PropertyKey.Create(this.TypeID, propIndices[i]);
                    keyCollection.Add(ref propKey);
                    propKeyToIdx.Add(propKey, i);
                }

                object[] data = new object[propIndices.Length];
                _iSensor.GetProperties(keyCollection, out valuesCollection);

                if (valuesCollection == null)
                {
                    return(data);
                }

                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, out propVal);

                    try
                    {
                        int idx = propKeyToIdx[propKey];
                        data[idx] = propVal.Value;
                    }
                    finally
                    {
                        propVal.Clear();
                    }
                }

                return(data);
            }
            finally
            {
                Marshal.ReleaseComObject(keyCollection);
            }
        }
Example #3
0
        /// <summary>
        /// Retrives the values of multiple properties.
        /// </summary>
        /// <param name="propKeys">Properties to retrieve.</param>
        /// <returns>A dictionary containing 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;

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

                _iSensor.GetProperties(keyCollection, out valuesCollection);

                Dictionary <PropertyKey, object> data = new Dictionary <PropertyKey, object>();

                if (valuesCollection == null)
                {
                    return(data);
                }

                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, out propVal);

                    try
                    {
                        data.Add(propKey, propVal.Value);
                    }
                    finally
                    {
                        propVal.Clear();
                    }
                }

                return(data);
            }
            finally
            {
                Marshal.ReleaseComObject(keyCollection);
            }
        }
        /// <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;
            }
        }
 public virtual extern void SetIPortableDeviceKeyCollectionValue([In] ref _tagpropertykey key, [In, MarshalAs(UnmanagedType.Interface)] PortableDeviceKeyCollection pValue);