コード例 #1
0
        public UsbDeviceProperty GetProperty(UsbDeviceWinApi.DEVPROPKEY devPropKey)
        {
            for (Int32 i = 0; i < this.Properties.Length; i++)
            {
                if (this.Properties[i].HasSameKey(devPropKey))
                {
                    return(this.Properties[i]);
                }
            }

            return(null);
        }
コード例 #2
0
        public String GetDescription()
        {
            foreach (FieldInfo field in typeof(UsbDeviceWinApi.DevicePropertyKeys).GetFields(BindingFlags.Static | BindingFlags.Public))
            {
                UsbDeviceWinApi.DEVPROPKEY devPropKey = (UsbDeviceWinApi.DEVPROPKEY)field.GetValue(null);
                if (this.HasSameKey(devPropKey))
                {
                    return(field.Name);
                }
            }

            return(String.Format("{0:B}, {1}", this.Key.Fmtid, this.Key.Pid));
        }
コード例 #3
0
 internal UsbDeviceProperty(UsbDeviceWinApi.DEVPROPKEY key, Object value, UInt32 type)
 {
     this.Key = key;
     this.Value = value;
     this.Type = type;
 }
コード例 #4
0
        private UsbDeviceProperty[] GetProperties()
        {
            UInt32 propertyKeyCount;
            Boolean success = UsbDeviceWinApi.SetupDiGetDevicePropertyKeys(this.handle, ref this.devInfoData, IntPtr.Zero, 0, out propertyKeyCount, 0);

            if (success || (Marshal.GetLastWin32Error() != UsbDeviceWinApi.ERROR_INSUFFICIENT_BUFFER))
            {
                this.TraceError("SetupDiGetDevicePropertyKeys");
                return new UsbDeviceProperty[0];
            }

            if (0 == propertyKeyCount)
            {
                return new UsbDeviceProperty[0];
            }

            List<UsbDeviceProperty> properties = new List<UsbDeviceProperty>();

            UsbDeviceWinApi.DEVPROPKEY[] propertyKeyArray = new UsbDeviceWinApi.DEVPROPKEY[propertyKeyCount];
            GCHandle propertyKeyArrayPinned = GCHandle.Alloc(propertyKeyArray , GCHandleType.Pinned);

            IntPtr buffer = propertyKeyArrayPinned.AddrOfPinnedObject();

            success = UsbDeviceWinApi.SetupDiGetDevicePropertyKeys(this.handle, ref this.devInfoData, buffer, propertyKeyCount, out propertyKeyCount, 0);

            if (success)
            {
                for (UInt32 propertyKeyIndex = 0; propertyKeyIndex < propertyKeyCount; propertyKeyIndex++)
                {
                    UInt32 propertyType;
                    UInt32 requiredSize;

                    success = UsbDeviceWinApi.SetupDiGetDevicePropertyW(this.handle, ref this.devInfoData,
                        ref propertyKeyArray[propertyKeyIndex], out propertyType, IntPtr.Zero, 0, out requiredSize, 0);

                    if (success || (Marshal.GetLastWin32Error() != UsbDeviceWinApi.ERROR_INSUFFICIENT_BUFFER))
                    {
                        this.TraceError("SetupDiGetDeviceProperty");
                        success = false;
                    }
                    else
                    {
                        buffer = Marshal.AllocHGlobal((Int32)requiredSize);

                        success = UsbDeviceWinApi.SetupDiGetDevicePropertyW(this.handle, ref this.devInfoData,
                            ref propertyKeyArray[propertyKeyIndex], out propertyType, buffer, requiredSize, out requiredSize, 0);

                        if (success)
                        {
                            Object value = this.MarshalDeviceProperty(buffer, (Int32)requiredSize, propertyType);
                            properties.Add(new UsbDeviceProperty(propertyKeyArray[propertyKeyIndex], value, propertyType));
                        }
                        else
                        {
                            this.TraceError("SetupDiGetDevicePropertyW");
                        }

                        Marshal.FreeHGlobal(buffer);
                    }

                    if (!success) // don't combine with previous "if", covers 2 cases
                    {
                        properties.Add(new UsbDeviceProperty(propertyKeyArray[propertyKeyIndex], null, UsbDeviceWinApi.DevicePropertyTypes.DEVPROP_TYPE_EMPTY));
                    }
                }
            }
            else
            {
                this.TraceError("SetupDiGetDevicePropertyKeys");
            }

            propertyKeyArrayPinned.Free();

            return properties.ToArray();
        }
コード例 #5
0
        private UsbDeviceProperty[] GetProperties()
        {
            UInt32  propertyKeyCount;
            Boolean success = UsbDeviceWinApi.SetupDiGetDevicePropertyKeys(this.handle, ref this.devInfoData, IntPtr.Zero, 0, out propertyKeyCount, 0);

            if (success || (Marshal.GetLastWin32Error() != UsbDeviceWinApi.ERROR_INSUFFICIENT_BUFFER))
            {
                this.TraceError("SetupDiGetDevicePropertyKeys");
                return(new UsbDeviceProperty[0]);
            }

            if (0 == propertyKeyCount)
            {
                return(new UsbDeviceProperty[0]);
            }

            List <UsbDeviceProperty> properties = new List <UsbDeviceProperty>();

            UsbDeviceWinApi.DEVPROPKEY[] propertyKeyArray = new UsbDeviceWinApi.DEVPROPKEY[propertyKeyCount];
            GCHandle propertyKeyArrayPinned = GCHandle.Alloc(propertyKeyArray, GCHandleType.Pinned);

            IntPtr buffer = propertyKeyArrayPinned.AddrOfPinnedObject();

            success = UsbDeviceWinApi.SetupDiGetDevicePropertyKeys(this.handle, ref this.devInfoData, buffer, propertyKeyCount, out propertyKeyCount, 0);

            if (success)
            {
                for (UInt32 propertyKeyIndex = 0; propertyKeyIndex < propertyKeyCount; propertyKeyIndex++)
                {
                    UInt32 propertyType;
                    UInt32 requiredSize;

                    success = UsbDeviceWinApi.SetupDiGetDevicePropertyW(this.handle, ref this.devInfoData,
                                                                        ref propertyKeyArray[propertyKeyIndex], out propertyType, IntPtr.Zero, 0, out requiredSize, 0);

                    if (success || (Marshal.GetLastWin32Error() != UsbDeviceWinApi.ERROR_INSUFFICIENT_BUFFER))
                    {
                        this.TraceError("SetupDiGetDeviceProperty");
                        success = false;
                    }
                    else
                    {
                        buffer = Marshal.AllocHGlobal((Int32)requiredSize);

                        success = UsbDeviceWinApi.SetupDiGetDevicePropertyW(this.handle, ref this.devInfoData,
                                                                            ref propertyKeyArray[propertyKeyIndex], out propertyType, buffer, requiredSize, out requiredSize, 0);

                        if (success)
                        {
                            Object value = this.MarshalDeviceProperty(buffer, (Int32)requiredSize, propertyType);
                            properties.Add(new UsbDeviceProperty(propertyKeyArray[propertyKeyIndex], value, propertyType));
                        }
                        else
                        {
                            this.TraceError("SetupDiGetDevicePropertyW");
                        }

                        Marshal.FreeHGlobal(buffer);
                    }

                    if (!success) // don't combine with previous "if", covers 2 cases
                    {
                        properties.Add(new UsbDeviceProperty(propertyKeyArray[propertyKeyIndex], null, UsbDeviceWinApi.DevicePropertyTypes.DEVPROP_TYPE_EMPTY));
                    }
                }
            }
            else
            {
                this.TraceError("SetupDiGetDevicePropertyKeys");
            }

            propertyKeyArrayPinned.Free();

            return(properties.ToArray());
        }
コード例 #6
0
        public Object GetPropertyValue(UsbDeviceWinApi.DEVPROPKEY devPropKey)
        {
            UsbDeviceProperty usbDeviceProperty = this.GetProperty(devPropKey);

            return(null == usbDeviceProperty ? null : usbDeviceProperty.Value);
        }
コード例 #7
0
 internal UsbDeviceProperty(UsbDeviceWinApi.DEVPROPKEY key, Object value, UInt32 type)
 {
     this.Key   = key;
     this.Value = value;
     this.Type  = type;
 }
コード例 #8
0
 public Boolean HasSameKey(UsbDeviceWinApi.DEVPROPKEY devPropKey)
 {
     return((this.Key.Fmtid == devPropKey.Fmtid) && (this.Key.Pid == devPropKey.Pid));
 }