Example #1
0
        public IDictionary <PropertyKey, object> SetProperties(DataFieldInfo[] data)
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentNullException("data", "Data field array must not be null or empty.");
            }

            IPortableDeviceValues pdv = new PortableDeviceValues( );

            for (int i = 0; i < data.Length; i++)
            {
                PropertyKey propKey = data[i].Key;
                object      value   = data[i].Value;
                if (value == null)
                {
                    throw new ArgumentNullException("data", String.Format("Data contains a null value at index {0}", i));
                }

                PropVariant pv = new PropVariant();
                try
                {
                    // PropVariant.FromObject will throw an ArgumentException if the value can
                    // not be converted to an appropriate PropVariant.
                    pv = PropVariant.FromObject(value);
                    pdv.SetValue(ref propKey, ref pv);
                }
                catch (ArgumentException)
                {
                    if (value is Guid)
                    {
                        Guid guid = (Guid)value;
                        pdv.SetGuidValue(ref propKey, ref guid);
                    }
                    else if (value is byte[])
                    {
                        byte[] buffer = (byte[])value;
                        pdv.SetBufferValue(ref propKey, buffer, (uint)buffer.Length);
                    }
                    else
                    {
                        pdv.SetIUnknownValue(ref propKey, value);
                    }
                }
                finally
                {
                    pv.Clear();
                }
            }

            Dictionary <PropertyKey, object> results = new Dictionary <PropertyKey, object>( );
            IPortableDeviceValues            pdv2    = null;
            HRESULT hr = nativeISensor.SetProperties(pdv, out pdv2);

            if (hr == HRESULT.S_OK)
            {
                try
                {
                    uint count = 0;
                    pdv2.GetCount(ref count);

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

            return(results);
        }
Example #2
0
        /// <summary>
        /// Sets the values of multiple properties.
        /// </summary>
        /// <param name="data">An array that contains the property keys and values.</param>
        /// <returns>A dictionary of the new values for the properties. Actual values may not match the requested values.</returns>
        public IDictionary <PropertyKey, object> SetProperties(DataFieldInfo[] data)
        {
            if (data == null || data.Length == 0)
            {
                throw new ArgumentException(LocalizedMessages.SensorEmptyData, "data");
            }

            IPortableDeviceValues pdv = new PortableDeviceValues();

            for (int i = 0; i < data.Length; i++)
            {
                PropertyKey propKey = data[i].Key;
                object      value   = data[i].Value;
                if (value == null)
                {
                    throw new ArgumentException(
                              string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                            LocalizedMessages.SensorNullValueAtIndex, i),
                              "data");
                }

                try
                {
                    // new PropVariant will throw an ArgumentException if the value can
                    // not be converted to an appropriate PropVariant.
                    using (PropVariant pv = PropVariant.FromObject(value))
                    {
                        pdv.SetValue(ref propKey, pv);
                    }
                }
                catch (ArgumentException)
                {
                    byte[] buffer;
                    if (value is Guid)
                    {
                        Guid guid = (Guid)value;
                        pdv.SetGuidValue(ref propKey, ref guid);
                    }
                    else if ((buffer = value as byte[]) != null)
                    {
                        pdv.SetBufferValue(ref propKey, buffer, (uint)buffer.Length);
                    }
                    else
                    {
                        pdv.SetIUnknownValue(ref propKey, value);
                    }
                }
            }

            Dictionary <PropertyKey, object> results = new Dictionary <PropertyKey, object>();
            IPortableDeviceValues            pdv2    = null;
            HResult hr = nativeISensor.SetProperties(pdv, out pdv2);

            if (hr == HResult.Ok)
            {
                try
                {
                    uint count = 0;
                    pdv2.GetCount(ref count);

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

            return(results);
        }