public static extern bool SetupDiGetDevicePropertyW( IntPtr deviceInfoSet, [In] ref SP_DEVINFO_DATA DeviceInfoData, [In] ref DisableHardware.DEVPROPKEY propertyKey, [Out] out UInt32 propertyType, IntPtr propertyBuffer, UInt32 propertyBufferSize, out UInt32 requiredSize, UInt32 flags);
public static string GetStringPropertyForDevice(IntPtr info, Native.SP_DEVINFO_DATA devdata, DisableHardware.DEVPROPKEY key) { uint proptype, outsize; IntPtr buffer = IntPtr.Zero; try { uint buflen = 512; buffer = Marshal.AllocHGlobal((int)buflen); Native.SetupDiGetDevicePropertyW( info, ref devdata, ref key, out proptype, buffer, buflen, out outsize, 0); byte[] lbuffer = new byte[outsize]; Marshal.Copy(buffer, lbuffer, 0, (int)outsize); return(Encoding.Unicode.GetString(lbuffer)); } finally { if (buffer != IntPtr.Zero) { Marshal.FreeHGlobal(buffer); } } }
public HWinfoModel[] GetAll() { List <HWinfoModel> HWList = new List <HWinfoModel>(); try { Guid myGUID = System.Guid.Empty; IntPtr hDevInfo = Native.SetupDiGetClassDevs(ref myGUID, 0, IntPtr.Zero, Native.DIGCF_ALLCLASSES | Native.DIGCF_PRESENT); if (hDevInfo.ToInt32() == Native.INVALID_HANDLE_VALUE) { throw new Exception("Invalid Handle"); } Native.SP_DEVINFO_DATA DeviceInfoData; DeviceInfoData = new Native.SP_DEVINFO_DATA(); DeviceInfoData.cbSize = 28; //is devices exist for class DeviceInfoData.devInst = 0; DeviceInfoData.classGuid = System.Guid.Empty; DeviceInfoData.reserved = 0; UInt32 i; StringBuilder DeviceName = new StringBuilder(""); DeviceName.Capacity = Native.MAX_DEV_LEN; for (i = 0; Native.SetupDiEnumDeviceInfo(hDevInfo, i, DeviceInfoData); i++) { Debug.WriteLine(i + "- " + DeviceInfoData.classGuid); //Declare vars int skip = 0; while (!Native.SetupDiGetDeviceRegistryProperty(hDevInfo, DeviceInfoData, Native.SPDRP_DEVICEDESC, 0, DeviceName, Native.MAX_DEV_LEN, IntPtr.Zero)) { // Debug.WriteLine("Skip"); skip++; if (skip > 1024) { break; } } if (skip < 1024) { if (DeviceInfoData.classGuid == Guid.Parse("{a45c254e-df1c-4efd-8020-67d146a850e0}")) { Debug.WriteLine("ooooh!"); } string devicepath = ""; var DEVPKEY_Device_HardwareIds = new DisableHardware.DEVPROPKEY(); DEVPKEY_Device_HardwareIds.fmtid = new Guid((uint)0x540b947e, (ushort)0x8b40, (ushort)0x45bc, 0xa8, 0xa2, 0x6a, 0x0b, 0x89, 0x4c, 0xbd, 0xa2); DEVPKEY_Device_HardwareIds.pid = 4; try { devicepath = GetStringPropertyForDevice(hDevInfo, DeviceInfoData, DEVPKEY_Device_HardwareIds); if (devicepath != "") { Debug.WriteLine(devicepath); } } catch { } HWList.Add(new HWinfoModel { Name = DeviceName.ToString(), Pointer = hDevInfo, ClassGuid = DeviceInfoData.classGuid }); } } Native.SetupDiDestroyDeviceInfoList(hDevInfo); } catch (Exception ex) { throw new Exception("Failed to enumerate device tree!", ex); } return(HWList.ToArray()); }