private static int GetInterfaceDetailBufferSize(DeviceInfoSet deviceInfo, Wrapper.SpDeviceInterfaceData deviceInterfaceData) { int bufferSize = 0; bool isSuccessful = Wrapper.SetupDiGetDeviceInterfaceDetail(deviceInfo.Handle.DangerousGetHandle(), ref deviceInterfaceData, default(IntPtr), 0, ref bufferSize, default(IntPtr)); var errorCode = Marshal.GetLastWin32Error(); if (!isSuccessful && errorCode != Wrapper.Win32ErrorInsufficientBuffer) { throw new Win32Exception(errorCode); } return(bufferSize); }
private static string GetDeviceFileInstancePath(DeviceInfoSet deviceInfo, Wrapper.SpDeviceInterfaceData deviceInterfaceData) { int bufferSize = GetInterfaceDetailBufferSize(deviceInfo, deviceInterfaceData); using (var interfaceDetailBuffer = new DeviceInterfaceDetailData(bufferSize)) { bool isSuccessful = Wrapper.SetupDiGetDeviceInterfaceDetail(deviceInfo.Handle.DangerousGetHandle(), ref deviceInterfaceData, interfaceDetailBuffer.Handle.DangerousGetHandle(), bufferSize, ref bufferSize, default(IntPtr)); var errorCode = Marshal.GetLastWin32Error(); if (!isSuccessful) { throw new Win32Exception(errorCode); } return(interfaceDetailBuffer.GetPathName()); } }
private static IEnumerable <Tuple <int, Wrapper.SpDeviceInterfaceData> > EnumerateDeviceInterfaces(DeviceInfoSet deviceInfoSet, Guid deviceGuid) { bool isEnumerationCompleted; var memberIndex = -1; do { var deviceInformationData = new Wrapper.SpDeviceInterfaceData(true); memberIndex++; bool deviceAvailable = Wrapper.SetupDiEnumDeviceInterfaces(deviceInfoSet.Handle.DangerousGetHandle(), IntPtr.Zero, ref deviceGuid, memberIndex, ref deviceInformationData); isEnumerationCompleted = Marshal.GetLastWin32Error().Equals(Wrapper.Win32ErrorNoMoreItems); if (!deviceAvailable) { continue; } yield return(new Tuple <int, Wrapper.SpDeviceInterfaceData>(memberIndex, deviceInformationData)); } while (!isEnumerationCompleted); }