private static string GetPropertyString(DeviceInfoSet deviceInfoSet, Wrapper.SpDevInfoData deviceInfoData, Wrapper.DevPropKey key) { var propertySize = GetPropertyBufferSize(deviceInfoSet, deviceInfoData, key); var devPropKey = key; using (var buffer = new DeviceIdBuffer(propertySize)) { bool isSuccessful = Wrapper.SetupDiGetDeviceProperty(deviceInfoSet.Handle.DangerousGetHandle(), ref deviceInfoData, ref devPropKey, out int _, buffer.Handle.DangerousGetHandle(), propertySize, out int _); int errorCode = Marshal.GetLastWin32Error(); if (!isSuccessful) { throw new Win32Exception(errorCode); } return(Marshal.PtrToStringAuto(buffer.Handle.DangerousGetHandle())); } }
private static IEnumerable <string> GetPropertyArray(DeviceInfoSet deviceInfoSet, Wrapper.SpDevInfoData deviceInfoData, Wrapper.DevPropKey key) { var result = new string[0]; try { var propertySize = GetPropertyBufferSize(deviceInfoSet, deviceInfoData, key); var devPropKey = key; using (var buffer = new DeviceIdBuffer(propertySize)) { bool isSuccessful = Wrapper.SetupDiGetDeviceProperty(deviceInfoSet.Handle.DangerousGetHandle(), ref deviceInfoData, ref devPropKey, out int _, buffer.Handle.DangerousGetHandle(), propertySize, out int _); int errorCode = Marshal.GetLastWin32Error(); if (!isSuccessful) { throw new Win32Exception(errorCode); } var data = new byte[propertySize]; Marshal.Copy(buffer.Handle.DangerousGetHandle(), data, 0, propertySize); result = System.Text.Encoding.Unicode.GetString(data).TrimEnd('\0').Split('\0'); } } catch (Exception e) { Debug.Print(e.Message); } return(result); }