public static byte[] GetDeviceProperty(string deviceInstanceId, Native.PnpDevicePropertyAPINative.DEVPROPKEY prop) { Native.PnpDevicePropertyAPINative.SP_DEVINFO_DATA deviceInfoData = new Native.PnpDevicePropertyAPINative.SP_DEVINFO_DATA(); deviceInfoData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(deviceInfoData); var dataBuffer = new byte[4096]; ulong propertyType = 0; var requiredSize = 0; Guid hidGuid = new Guid(); Native.PnpDevicePropertyAPINative.HidD_GetHidGuid(ref hidGuid); IntPtr deviceInfoSet = Native.PnpDevicePropertyAPINative.SetupDiGetClassDevs(ref hidGuid, deviceInstanceId, 0, Native.PnpDevicePropertyAPINative.DIGCF_PRESENT | Native.PnpDevicePropertyAPINative.DIGCF_DEVICEINTERFACE); //IntPtr deviceInfoSet = Native.PnpDevicePropertyAPINative.SetupDiGetClassDevs(IntPtr.Zero, deviceInstanceId, 0, Native.PnpDevicePropertyAPINative.DIGCF_PRESENT | Native.PnpDevicePropertyAPINative.DIGCF_DEVICEINTERFACE | Native.PnpDevicePropertyAPINative.DIGCF_ALLCLASSES); /*bool tempval = */ try { Native.PnpDevicePropertyAPINative.SetupDiEnumDeviceInfo(deviceInfoSet, 0, ref deviceInfoData); if (Native.PnpDevicePropertyAPINative.SetupDiGetDeviceProperty(deviceInfoSet, ref deviceInfoData, ref prop, ref propertyType, dataBuffer, dataBuffer.Length, ref requiredSize, 0)) { return dataBuffer.Take(requiredSize).ToArray(); } } finally { if (deviceInfoSet.ToInt64() != Native.PnpDevicePropertyAPINative.INVALID_HANDLE_VALUE) { Native.PnpDevicePropertyAPINative.SetupDiDestroyDeviceInfoList(deviceInfoSet); } } return null; }
public static Dictionary<Native.PnpDevicePropertyAPINative.DEVPROPKEY, string> GetDeviceProperties(string deviceInstanceId) { //string result = string.Empty; Native.PnpDevicePropertyAPINative.SP_DEVINFO_DATA deviceInfoData = new Native.PnpDevicePropertyAPINative.SP_DEVINFO_DATA(); deviceInfoData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(deviceInfoData); var dataBuffer = new byte[4096]; ulong propertyType = 0; var requiredSize = 0; //Guid hidGuid = new Guid(); //Native.PnpDevicePropertyAPINative.HidD_GetHidGuid(ref hidGuid); //IntPtr deviceInfoSet = Native.PnpDevicePropertyAPINative.SetupDiGetClassDevs(ref hidGuid, deviceInstanceId, 0, Native.PnpDevicePropertyAPINative.DIGCF_PRESENT | Native.PnpDevicePropertyAPINative.DIGCF_DEVICEINTERFACE); //IntPtr deviceInfoSet = Native.PnpDevicePropertyAPINative.SetupDiGetClassDevs(IntPtr.Zero, deviceInstanceId, 0, Native.PnpDevicePropertyAPINative.DIGCF_PRESENT | Native.PnpDevicePropertyAPINative.DIGCF_DEVICEINTERFACE); IntPtr deviceInfoSet = Native.PnpDevicePropertyAPINative.SetupDiGetClassDevs(IntPtr.Zero, deviceInstanceId, 0, Native.PnpDevicePropertyAPINative.DIGCF_PRESENT | Native.PnpDevicePropertyAPINative.DIGCF_DEVICEINTERFACE | Native.PnpDevicePropertyAPINative.DIGCF_ALLCLASSES); //int memberIndex = 0; Dictionary<Native.PnpDevicePropertyAPINative.DEVPROPKEY, string> results = null; Native.PnpDevicePropertyAPINative.SetupDiEnumDeviceInfo(deviceInfoSet, 0, ref deviceInfoData); //while (Native.PnpDevicePropertyAPINative.SetupDiEnumDeviceInfo(deviceInfoSet, memberIndex, ref deviceInfoData)) { //if (Native.PnpDevicePropertyAPINative.SetupDiGetDevicePropertyKeys(deviceInfoSet, ref deviceInfoData, null, 0, out var cnt) == Win32Error.ERROR_INSUFFICIENT_BUFFER) if (!Native.PnpDevicePropertyAPINative.SetupDiGetDevicePropertyKeys(deviceInfoSet, ref deviceInfoData, null, 0, out var cnt) && Marshal.GetLastWin32Error() == Native.PnpDevicePropertyAPINative.ERROR_INSUFFICIENT_BUFFER) { var arr = new Native.PnpDevicePropertyAPINative.DEVPROPKEY[cnt]; if (Native.PnpDevicePropertyAPINative.SetupDiGetDevicePropertyKeys(deviceInfoSet, ref deviceInfoData, arr, (uint)arr.Length, out _)) { results = new Dictionary<Native.PnpDevicePropertyAPINative.DEVPROPKEY, string>(); foreach (var arr_ in arr.Distinct()) { var arrItem = arr_; if (Native.PnpDevicePropertyAPINative.SetupDiGetDeviceProperty(deviceInfoSet, ref deviceInfoData, ref arrItem, ref propertyType, dataBuffer, dataBuffer.Length, ref requiredSize, 0)) { try { string result = Encoding.Unicode.GetString(dataBuffer.Take(requiredSize).TakeWhile(dr => dr != '\0').ToArray()); if (result.Length > 4) { results.Add(arrItem, result); } else { results.Add(arrItem, BitConverter.ToString(dataBuffer.Take(requiredSize).ToArray()).Replace("-", string.Empty)); } } catch { results.Add(arrItem, BitConverter.ToString(dataBuffer.Take(requiredSize).ToArray()).Replace("-", string.Empty)); } } else { //results.Add(arrItem, "null"); } } } } //memberIndex++; } if (deviceInfoSet.ToInt64() != Native.PnpDevicePropertyAPINative.INVALID_HANDLE_VALUE) { Native.PnpDevicePropertyAPINative.SetupDiDestroyDeviceInfoList(deviceInfoSet); } return results; }