public UsbDevice(IntPtr enumeration, ref Hardware.SP_DEVINFO_DATA device) : base(enumeration, ref device) { ProductId = Find(PRODUCT_ID, 0); Revision = Find(REVISION, 0); VendorId = Find(VENDOR_ID, 0); InterfaceId = Find(INTERFACE_ID, 0xFFFF); }
protected Device(IntPtr enumeration, ref Hardware.SP_DEVINFO_DATA device) { HardwareIds = (string[])GetDeviceRegistryProperty(enumeration, ref device, RegistryProperty.HardwareId); Manufacturer = (string)GetDeviceRegistryProperty(enumeration, ref device, RegistryProperty.Manufacturer); DeviceDescription = (string)GetDeviceRegistryProperty(enumeration, ref device, RegistryProperty.DeviceDescription); FriendlyName = (string)GetDeviceRegistryProperty(enumeration, ref device, RegistryProperty.FriendlyName); LocationInformation = (string)GetDeviceRegistryProperty(enumeration, ref device, RegistryProperty.LocationInformation); PhysicalDeviceObjectName = (string)GetDeviceRegistryProperty(enumeration, ref device, RegistryProperty.PhysicalDeviceObjectName); Configuration = (ConfigFlags)GetDeviceRegistryProperty(enumeration, ref device, RegistryProperty.ConfigFlags); InstallState = (InstallStates)GetDeviceRegistryProperty(enumeration, ref device, RegistryProperty.InstallState); LargeIcon = LoadDeviceIcon(enumeration, ref device, 48); SmallIcon = LoadDeviceIcon(enumeration, ref device, 16); }
private Bitmap LoadDeviceIcon(IntPtr enumeration, ref Hardware.SP_DEVINFO_DATA device, uint size) { IntPtr hIcon; return (SetupDiLoadDeviceIcon(enumeration, ref device, size, size, 0, out hIcon)) ? Bitmap.FromHicon(hIcon) : null; }
private static extern bool SetupDiLoadDeviceIcon( IntPtr deviceInfoSet, ref Hardware.SP_DEVINFO_DATA deviceInfoData, uint cXIcon, uint cYIcon, uint flags, out IntPtr hIcon );
private static extern bool SetupDiGetDeviceRegistryProperty( IntPtr deviceInfoSet, ref Hardware.SP_DEVINFO_DATA deviceInfoData, Device.RegistryProperty property, out RegistryDataType propertyRegDataType, IntPtr propertyBuffer, uint propertyBufferSize, out UInt32 requiredSize );
private static object GetDeviceRegistryProperty(IntPtr h, ref Hardware.SP_DEVINFO_DATA devInfo, RegistryProperty property) { RegistryDataType dataType; uint bufferLength; SetupDiGetDeviceRegistryProperty(h, ref devInfo, property, out dataType, IntPtr.Zero, 0, out bufferLength); object result = null; IntPtr buffer = Marshal.AllocHGlobal((int)bufferLength); if (SetupDiGetDeviceRegistryProperty(h, ref devInfo, property, out dataType, buffer, bufferLength, out bufferLength)) { switch (dataType) { case RegistryDataType.REG_SZ: result = Marshal.PtrToStringAuto(buffer); break; case RegistryDataType.REG_MULTI_SZ: var bytes = PtrToByteArray(buffer, bufferLength); var text = Encoding.Unicode.GetString(bytes); result = text.TrimEnd('\0').Split('\0'); break; case RegistryDataType.REG_BINARY: result = PtrToByteArray(buffer, bufferLength); break; case RegistryDataType.REG_DWORD: result = Marshal.ReadInt32(buffer); break; default: throw new Exception(string.Format("Unsupported data type: {0}", dataType)); } } Marshal.FreeHGlobal(buffer); return result; }
public DeviceInterfaceChangedArgs(Hardware.DeviceEvent eventType, Guid classId, string deviceName) { EventType = eventType; ClassId = classId; DeviceName = deviceName; }