예제 #1
0
        public void Refresh()
        {
            if (this.Count > 0)
            {
                this.Clear();
            }

            IPortableDeviceProperties properties = null;

            this.content.Properties(out properties);

            IPortableDeviceValues values = null;

            properties.GetValues(this.objectID, null, out values);

            var count = 0U;

            values.GetCount(ref count);

            var key     = new _tagpropertykey();
            var propVar = new tag_inner_PROPVARIANT();

            for (var i = 0U; i < count; i++)
            {
                values.GetAt(i, ref key, ref propVar);
                this.Add(WpdProperty.Create(key, propVar));
            }
        }
예제 #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
        public static IEnumerable <KeyValuePair <string, string> > ToKeyValuePair(this IPortableDeviceValues values)
        {
            uint num = 0;

            values?.GetCount(ref num);
            for (uint i = 0; i < num; i++)
            {
                PropertyKey key = new PropertyKey();
                using (PropVariantFacade val = new PropVariantFacade())
                {
                    values.GetAt(i, ref key, ref val.Value);


                    string    fieldName = string.Empty;
                    FieldInfo propField = ComTrace.FindPropertyKeyField(key);
                    if (propField != null)
                    {
                        fieldName = propField.Name;
                    }
                    else
                    {
                        FieldInfo guidField = ComTrace.FindGuidField(key.fmtid);
                        if (guidField != null)
                        {
                            fieldName = $"{guidField.Name}, {key.pid}";
                        }
                        else
                        {
                            fieldName = $"{key.fmtid}, {key.pid}";
                        }
                    }
                    string fieldValue = string.Empty;
                    switch (val.VariantType)
                    {
                    case PropVariantType.VT_CLSID:
                        fieldValue = ComTrace.FindGuidField(val.ToGuid())?.Name ?? val.ToString();
                        break;

                    default:
                        fieldValue = val.ToDebugString();
                        break;
                    }

                    yield return(new KeyValuePair <string, string>(fieldName, fieldValue));
                }
            }
        }
예제 #4
0
        /// <summary>
        /// インスタンスを初期化します。
        /// </summary>
        /// <param name="values">イベント データのコレクション。</param>
        internal WpdEventArgs(IPortableDeviceValues values)
        {
            uint count = 0;

            values.GetCount(ref count);
            if (count < 1)
            {
                this.Values = new List <WpdPropertyValue>(0);
                return;
            }

            this.Values = new List <WpdPropertyValue>(( int )count);
            var key  = new _tagpropertykey();
            var info = new tag_inner_PROPVARIANT();

            for (uint i = 0; i < count; ++i)
            {
                values.GetAt(i, ref key, ref info);

                var value = new WpdPropertyValue(key, info, values);
                if (value.Key.Equals(WpdProperties.WPD_EVENT_PARAMETER_PNP_DEVICE_ID))
                {
                    this.DeviceId = value.ValueString;
                }
                else if (value.Key.Equals(WpdProperties.WPD_OBJECT_ID))
                {
                    this.ObjectId = value.ValueString;
                }
                else if (value.Key.Equals(WpdProperties.WPD_OBJECT_PARENT_ID))
                {
                    this.ParentObjectId = value.ValueString;
                }
                else if (value.Key.Equals(WpdProperties.WPD_EVENT_PARAMETER_EVENT_ID))
                {
                    if (value.ValueGuid != null)
                    {
                        this.Type = TypeConvertUtility.GuidToEventType(value.ValueGuid.Value);
                    }
                }
                else
                {
                    this.Values.Add(value);
                }
            }
        }
예제 #5
0
        private static void InternalWriteObject(IPortableDeviceValues values)
        {
            string func = new StackTrace().GetFrame(2).GetMethod().Name;

            Trace.WriteLine($"############################### {func}");
            uint num = 0;

            values.GetCount(ref num);
            for (uint i = 0; i < num; i++)
            {
                PropertyKey       key = new PropertyKey();
                PropVariantFacade val = new PropVariantFacade();
                values.GetAt(i, ref key, ref val.Value);

                string    fieldName = string.Empty;
                FieldInfo propField = FindPropertyKeyField(key);
                if (propField != null)
                {
                    fieldName = propField.Name;
                }
                else
                {
                    FieldInfo guidField = FindGuidField(key.fmtid);
                    if (guidField != null)
                    {
                        fieldName = $"{guidField.Name}, {key.pid}";
                    }
                    else
                    {
                        fieldName = $"{key.fmtid}, {key.pid}";
                    }
                }

                switch (val.VariantType)
                {
                case PropVariantType.VT_CLSID:
                    Trace.WriteLine($"##### {fieldName} = {FindGuidField(val.ToGuid())?.Name ?? val.ToString()}");
                    break;

                default:
                    Trace.WriteLine($"##### {fieldName} = {val.ToDebugString()}");
                    break;
                }
            }
        }
예제 #6
0
        public static bool HasKeyValue(this IPortableDeviceValues values, PropertyKey findKey)
        {
            uint num = 0;

            values?.GetCount(ref num);
            for (uint i = 0; i < num; i++)
            {
                PropertyKey key = new PropertyKey();
                using (PropVariantFacade val = new PropVariantFacade())
                {
                    values.GetAt(i, ref key, ref val.Value);
                    if (key == findKey)
                    {
                        return(val.VariantType != PropVariantType.VT_ERROR);
                    }
                }
            }

            return(false);
        }
예제 #7
0
        /// <summary>
        /// インスタンスを初期化します。
        /// </summary>
        /// <param name="values">イベント データのコレクション。</param>
        internal WpdEventArgs( IPortableDeviceValues values )
        {
            uint count = 0;
            values.GetCount( ref count );
            if( count < 1 )
            {
                this.Values = new List< WpdPropertyValue >( 0 );
                return;
            }

            this.Values = new List< WpdPropertyValue >( ( int )count );
            var key     = new _tagpropertykey();
            var info    = new tag_inner_PROPVARIANT();

            for( uint i = 0; i < count; ++i )
            {
                values.GetAt( i, ref key, ref info );

                var value = new WpdPropertyValue( key, info, values );
                if( value.Key.Equals( WpdProperties.WPD_EVENT_PARAMETER_PNP_DEVICE_ID ) )
                {
                    this.DeviceId = value.ValueString;
                }
                else if( value.Key.Equals( WpdProperties.WPD_OBJECT_ID ) )
                {
                    this.ObjectId = value.ValueString;
                }
                else if( value.Key.Equals( WpdProperties.WPD_OBJECT_PARENT_ID ) )
                {
                    this.ParentObjectId = value.ValueString;
                }
                else if( value.Key.Equals( WpdProperties.WPD_EVENT_PARAMETER_EVENT_ID ) )
                {
                    if( value.ValueGuid != null ) { this.Type = TypeConvertUtility.GuidToEventType( value.ValueGuid.Value ); }
                }
                else
                {
                    this.Values.Add( value );
                }
            }
        }
예제 #8
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);
        }
예제 #9
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);
        }
예제 #10
0
파일: Item.cs 프로젝트: chcg/MediaDevices
        private void GetProperties()
        {
            IPortableDeviceValues values = null;

            try
            {
                // get all predefined values
                this.device.deviceProperties.GetValues(this.Id, keyCollection, out values);
            }
            catch (Exception ex)
            {
                Trace.TraceError($"{ex.Message} for {this.Id}");
                return;
            }

            // read all properties
            // use a loop to prevent exceptions during calling GetValue for non existing values
            uint num = 0;

            values.GetCount(ref num);
            for (uint i = 0; i < num; i++)
            {
                PropertyKey key = new PropertyKey();
                using (PropVariantFacade val = new PropVariantFacade())
                {
                    values.GetAt(i, ref key, ref val.Value);

                    if (key.fmtid == WPD.OBJECT_PROPERTIES_V1)
                    {
                        switch ((ObjectProperties)key.pid)
                        {
                        case ObjectProperties.ContentType:
                            this.ContentType = val;
                            break;

                        case ObjectProperties.Name:
                            this.name = val;
                            break;

                        case ObjectProperties.OriginalFileName:
                            this.OriginalFileName = val;
                            break;

                        case ObjectProperties.HintLocationDisplayName:
                            this.HintLocationName = val;
                            break;

                        case ObjectProperties.ContainerFunctionalObjectId:
                            this.ParentContainerId = val;
                            break;

                        case ObjectProperties.Size:
                            this.Size = val;
                            break;

                        case ObjectProperties.DateCreated:
                            this.DateCreated = val;
                            break;

                        case ObjectProperties.DateModified:
                            this.DateModified = val;
                            break;

                        case ObjectProperties.DateAuthored:
                            this.DateAuthored = val;
                            break;

                        case ObjectProperties.CanDelete:
                            this.CanDelete = val;
                            break;

                        case ObjectProperties.IsSystem:
                            this.IsSystem = val.ToBool();
                            break;

                        case ObjectProperties.IsHidden:
                            this.IsHidden = val;
                            break;

                        case ObjectProperties.IsDrmProtected:
                            this.IsDRMProtected = val;
                            break;

                        case ObjectProperties.ParentId:
                            this.ParentId = val;
                            break;

                        case ObjectProperties.PersistentUniqueId:
                            this.PersistentUniqueId = val;
                            break;
                        }
                    }
                }
            }
        }
예제 #11
0
        /// <summary>
        /// Sets the values of multiple properties.
        /// </summary>
        /// <param name="data">The keys and values of properties to set.</param>
        /// <returns>The new values of the properties. Actual values may not match requested values.</returns>
        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));
                }

                if (value is string)
                {
                    pdv.SetStringValue(ref propKey, (string)value);
                }
                else if (value is uint)
                {
                    pdv.SetUnsignedIntegerValue(ref propKey, (uint)value);
                }
                else if (value is int)
                {
                    pdv.SetSignedIntegerValue(ref propKey, (int)value);
                }
                else if (value is ulong)
                {
                    pdv.SetUnsignedLargeIntegerValue(ref propKey, (ulong)value);
                }
                else if (value is long)
                {
                    pdv.SetSignedLargeIntegerValue(ref propKey, (long)value);
                }
                else if (value is float || value is double)
                {
                    pdv.SetFloatValue(ref propKey, (float)value);
                }
                else if (value is bool)
                {
                    pdv.SetBoolValue(ref propKey, ((bool)value) ? 1 : 0);
                }
                else 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);
                }
            }

            IPortableDeviceValues pdv2 = null;

            _iSensor.SetProperties(pdv, out pdv2);

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

            if (pdv2 == null)
            {
                return(results);
            }

            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, out propVal);
                    results.Add(propKey, propVal.Value);
                }
                finally
                {
                    propVal.Clear();
                }
            }

            return(results);
        }