private static Tuple <bool, Wrapper.SpDevInfoData> GetDeviceInfoData(DeviceInfoSet deviceInfoSet, int memberIndex) { var deviceInfoData = new Wrapper.SpDevInfoData(true); bool infoAvailable = Wrapper.SetupDiEnumDeviceInfo(deviceInfoSet.Handle.DangerousGetHandle(), memberIndex, ref deviceInfoData); return(new Tuple <bool, Wrapper.SpDevInfoData>(infoAvailable, deviceInfoData)); }
private static int GetPropertyBufferSize(DeviceInfoSet deviceInfoSet, Wrapper.SpDevInfoData deviceInfoData, Wrapper.DevPropKey key) { var parentInstanceIdProperty = key; bool isSuccessful = Wrapper.SetupDiGetDeviceProperty(deviceInfoSet.Handle.DangerousGetHandle(), ref deviceInfoData, ref parentInstanceIdProperty, out int _, IntPtr.Zero, 0, out int size); int errorCode = Marshal.GetLastWin32Error(); if (!isSuccessful && errorCode != Wrapper.Win32ErrorInsufficientBuffer) { throw new Win32Exception(errorCode); } return(size); }
private static IEnumerable <Tuple <int, Wrapper.SpDevInfoData> > EnumerateDeviceInfoData(DeviceInfoSet deviceInfoSet) { bool isEnumerationCompleted; var memberIndex = -1; do { var deviceInfoData = new Wrapper.SpDevInfoData(true); memberIndex++; bool infoAvailable = Wrapper.SetupDiEnumDeviceInfo(deviceInfoSet.Handle.DangerousGetHandle(), memberIndex, ref deviceInfoData); isEnumerationCompleted = Marshal.GetLastWin32Error().Equals(Wrapper.Win32ErrorNoMoreItems); if (!infoAvailable) { continue; } yield return(new Tuple <int, Wrapper.SpDevInfoData>(memberIndex, deviceInfoData)); } while (!isEnumerationCompleted); }
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); }