public static extern bool SetupDiGetDeviceProperty( IntPtr deviceInfoSet, [In] ref SP_DEVINFO_DATA deviceInfoData, [In] ref DEVPROPKEY propertyKey, out DEVPROPTYPE propertyType, byte[] propertyBuffer, uint propertyBufferSize, ref uint requiredSize, uint flags);
internal static extern bool SetupDiGetDeviceProperty( SafeDeviceInfoSetHandle DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref DEVPROPKEY PropertyKey, out DEVPROPTYPE PropertyType, byte[] PropertyBuffer, uint PropertyBufferSize, ref uint RequiredSize, uint Flags);
public void GetStringProperty(DEVPROPKEY key) { DEVPROPTYPE dpt = 0; uint type = 0; uint size = 0; bool Success = true; uint i = 0; int BUFFER_SIZE = 2048; IntPtr _hDevInfo = IntPtr.Zero; IntPtr buffer = Marshal.AllocHGlobal(BUFFER_SIZE); devices.Clear(); try { _hDevInfo = SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, DiGetClassFlags.DIGCF_PRESENT | DiGetClassFlags.DIGCF_ALLCLASSES); if (_hDevInfo == INVALID_HANDLE_VALUE) { throw new Win32Exception(Marshal.GetLastWin32Error()); } while (Success) { Device device = new Device(); SP_DEVINFO_DATA data = new SP_DEVINFO_DATA(); data.cbSize = (uint)Marshal.SizeOf(data); Success = SetupDiEnumDeviceInfo(_hDevInfo, i, ref data); if (SetupDiGetDeviceProperty(_hDevInfo, ref data, ref key, ref dpt, buffer, (uint)BUFFER_SIZE, out size, 0)) { device.Parent = Marshal.PtrToStringAuto(buffer).Trim(); } StringBuilder sb = new StringBuilder(BUFFER_SIZE); if (SetupDiGetDeviceInstanceId(_hDevInfo, ref data, sb, (uint)BUFFER_SIZE, out size)) { device.DeviceInstancePath = sb.ToString(); } DEVPROPKEY pdokey = DEVPROPKEY.DEVPKEY_Device_PDOName; if (SetupDiGetDeviceProperty(_hDevInfo, ref data, ref pdokey, ref dpt, buffer, (uint)BUFFER_SIZE, out size, 0)) { device.Pdo = Marshal.PtrToStringAuto(buffer).Trim(); } if (SetupDiGetDeviceRegistryProperty(_hDevInfo, ref data, (int)SPDRP.SPDRP_FRIENDLYNAME, ref type, buffer, (uint)BUFFER_SIZE, ref size)) { var s = Marshal.PtrToStringAuto(buffer); device.FriendlyName = s.Trim(); } if (SetupDiGetDeviceRegistryProperty(_hDevInfo, ref data, (int)SPDRP.SPDRP_LOCATION_INFORMATION, ref type, buffer, (uint)BUFFER_SIZE, ref size)) { var s = Marshal.PtrToStringAuto(buffer); device.LocationInformation = s.Trim(); } if (SetupDiGetDeviceRegistryProperty(_hDevInfo, ref data, (int)SPDRP.SPDRP_LOCATION_PATHS, ref type, buffer, (uint)BUFFER_SIZE, ref size)) { var s = Marshal.PtrToStringAuto(buffer); device.LocationPaths = s.Trim(); } if (SetupDiGetDeviceRegistryProperty(_hDevInfo, ref data, (int)SPDRP.SPDRP_HARDWAREID, ref type, buffer, (uint)BUFFER_SIZE, ref size)) { var s = Marshal.PtrToStringAuto(buffer); device.HardwareId = s.Trim(); } devices.Add(device); i++; } } catch (Exception ex) { Console.WriteLine(ex); } finally { SetupDiDestroyDeviceInfoList(_hDevInfo); Marshal.FreeHGlobal(buffer); } }
private static extern bool SetupDiGetDeviceProperty(IntPtr DeviceInfoSet, ref SP_DEVINFO_DATA DeviceInfoData, ref DEVPROPKEY propertyKey, ref DEVPROPTYPE propertyType, IntPtr propertyBuffer, uint propertyBufferSize, out uint requiredSize, uint flags);
private static NtResult <SafeHGlobalBuffer> GetDeviceInterfaceProperty(string device_instance, DEVPROPKEY key, out DEVPROPTYPE type, bool throw_on_error) { int length = 0; var result = DeviceNativeMethods.CM_Get_Device_Interface_PropertyW(device_instance, key, out type, SafeHGlobalBuffer.Null, ref length, 0); if (result != CrError.BUFFER_SMALL) { return(result.ToNtStatus().CreateResultFromError <SafeHGlobalBuffer>(throw_on_error)); } using (var buffer = new SafeHGlobalBuffer(length)) { return(DeviceNativeMethods.CM_Get_Device_Interface_PropertyW(device_instance, key, out type, buffer, ref length, 0).ToNtStatus().CreateResult(throw_on_error, () => buffer.Detach())); } }
private static NtResult <SafeHGlobalBuffer> GetClassProperty(Guid class_guid, CmClassType flags, DEVPROPKEY key, out DEVPROPTYPE type, bool throw_on_error) { int length = 0; var result = DeviceNativeMethods.CM_Get_Class_PropertyW(class_guid, key, out type, SafeHGlobalBuffer.Null, ref length, flags); if (result != CrError.BUFFER_SMALL) { return(result.ToNtStatus().CreateResultFromError <SafeHGlobalBuffer>(throw_on_error)); } using (var buffer = new SafeHGlobalBuffer(length)) { return(DeviceNativeMethods.CM_Get_Class_PropertyW(class_guid, key, out type, buffer, ref length, flags).ToNtStatus().CreateResult(throw_on_error, () => buffer.Detach())); } }
public static IEnumerable <Device> GetDevices(string instanceId = "", DeviceClass?deviceClass = null) { SafeDeviceInfoSetHandle diSetHandle = null; try { GCHandle guid = new GCHandle(); if (deviceClass.HasValue) { var classGuid = Device.DeviceClassGuids[(int)deviceClass]; guid = GCHandle.Alloc(classGuid, GCHandleType.Pinned); diSetHandle = NativeMethods.SetupDiGetClassDevs(guid.AddrOfPinnedObject(), null, IntPtr.Zero, SetupDiGetClassDevsFlags.Null); } else { diSetHandle = NativeMethods.SetupDiGetClassDevs(IntPtr.Zero, null, IntPtr.Zero, SetupDiGetClassDevsFlags.AllClasses); } if (diSetHandle.IsInvalid) { throw new Win32Exception(Marshal.GetLastWin32Error(), "Error calling SetupDiGetClassDevs"); } //Get the device information data for each matching device. var diData = GetDeviceInfoData(diSetHandle); if (!string.IsNullOrEmpty(instanceId)) { // Find the index of of the instance int index = GetIndexOfInstance(diSetHandle, diData, instanceId); if (index == -1) { throw new IndexOutOfRangeException(string.Format("The device '{0}' could not be found", instanceId)); } diData = new SP_DEVINFO_DATA[] { diData[index] }; } for (int i = 0; i < diData.Length; i++) { uint propertyCount = 0; if (!NativeMethods.SetupDiGetDevicePropertyKeys(diSetHandle, ref diData[i], IntPtr.Zero, 0, ref propertyCount, 0)) { var error = Marshal.GetLastWin32Error(); if (error != 122) { throw new Win32Exception(error, "Error calling SetupDiGetDevicePropertyKeys"); } } var device = new Device(diData[i].ClassGuid); DEVPROPKEY[] dev = new DEVPROPKEY[propertyCount]; GCHandle devPropHandle = GCHandle.Alloc(dev, GCHandleType.Pinned); if (!NativeMethods.SetupDiGetDevicePropertyKeys(diSetHandle, ref diData[i], devPropHandle.AddrOfPinnedObject(), (uint)dev.Length, ref propertyCount, 0)) { var error = Marshal.GetLastWin32Error(); throw new Win32Exception(error, "Error calling SetupDiGetDevicePropertyKeys"); } int devicePropertyIterator = 0; while (devicePropertyIterator < propertyCount) { byte[] propBuffer = new byte[1]; uint requiredSize = 0; DEVPROPTYPE devicePropertyType; if (!NativeMethods.SetupDiGetDeviceProperty( diSetHandle, ref diData[i], ref dev[devicePropertyIterator], out devicePropertyType, propBuffer, (uint)propBuffer.Length, ref requiredSize, 0)) { var error = Marshal.GetLastWin32Error(); if (error != 122) { throw new Win32Exception(error, "Error calling SetupDiGetDeviceProperty"); } } Array.Resize(ref propBuffer, (int)requiredSize); if (NativeMethods.SetupDiGetDeviceProperty(diSetHandle, ref diData[i], ref dev[devicePropertyIterator], out devicePropertyType, propBuffer, (uint)propBuffer.Length, ref requiredSize, 0)) { DEVPROPTYPE devPropType = devicePropertyType; switch (devPropType) { case DEVPROPTYPE.DEVPROP_TYPE_UINT16: device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToUInt16(propBuffer, 0)); break; case DEVPROPTYPE.DEVPROP_TYPE_INT32: device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToInt32(propBuffer, 0)); break; case DEVPROPTYPE.DEVPROP_TYPE_UINT32: device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToUInt32(propBuffer, 0)); break; case DEVPROPTYPE.DEVPROP_TYPE_INT64: device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToInt64(propBuffer, 0)); break; case DEVPROPTYPE.DEVPROP_TYPE_FLOAT: case DEVPROPTYPE.DEVPROP_TYPE_DOUBLE: case DEVPROPTYPE.DEVPROP_TYPE_DECIMAL: case DEVPROPTYPE.DEVPROP_TYPE_CURRENCY: case DEVPROPTYPE.DEVPROP_TYPE_DATE: throw new Exception(string.Format("No property processing available for device property type '{0}'", devPropType.ToString())); case DEVPROPTYPE.DEVPROP_TYPE_UINT64: device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToUInt64(propBuffer, 0)); break; case DEVPROPTYPE.DEVPROP_TYPE_GUID: { byte[] guidBuffer = new byte[16]; Array.ConstrainedCopy(propBuffer, 0, guidBuffer, 0, 16); device.Properties.Add(dev[devicePropertyIterator], new Guid(guidBuffer)); break; } case DEVPROPTYPE.DEVPROP_TYPE_FILETIME: device.Properties.Add(dev[devicePropertyIterator], DateTime.FromFileTime(BitConverter.ToInt64(propBuffer, 0))); break; case DEVPROPTYPE.DEVPROP_TYPE_BOOLEAN: device.Properties.Add(dev[devicePropertyIterator], BitConverter.ToBoolean(propBuffer, 0)); break; case DEVPROPTYPE.DEVPROP_TYPE_STRING: var value = Encoding.Unicode.GetString(propBuffer, 0, (int)requiredSize); if (value[value.Length - 1] == (new char[1])[0]) { value = value.Substring(0, value.Length - 1); } device.Properties.Add(dev[devicePropertyIterator], value); break; default: { Dictionary <DEVPROPKEY, object> properties = device.Properties; DEVPROPKEY prop = dev[devicePropertyIterator]; string propValue = Encoding.Unicode.GetString(propBuffer, 0, (int)requiredSize); char[] separator = new char[1]; properties.Add(prop, propValue.Split(separator)); break; } } devicePropertyIterator++; } } devPropHandle.Free(); yield return(device); } diSetHandle.Close(); } finally { if (diSetHandle != null) { if (diSetHandle.IsClosed == false) { diSetHandle.Close(); } diSetHandle.Dispose(); } } }