public static IEnumerable <string> GetAllDevicePaths(Guid guid) { var ret = new List <string>(); var deviceInfoSet = NativeInterface.SetupDiGetClassDevs(ref guid, null, 0, NativeInterface.DIGCF_PRESENT | NativeInterface.DIGCF_DEVICEINTERFACE); if (deviceInfoSet.ToInt64() != NativeInterface.INVALID_HANDLE_VALUE) { var deviceInfoData = _CreateDeviceInfoData(); var deviceIndex = 0; while (NativeInterface.SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, ref deviceInfoData)) { deviceIndex += 1; var deviceInterfaceData = new NativeInterface.SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); var deviceInterfaceIndex = 0; while (NativeInterface.SetupDiEnumDeviceInterfaces(deviceInfoSet, ref deviceInfoData, ref guid, deviceInterfaceIndex, ref deviceInterfaceData)) { ret.Add(_GetDevicePath(deviceInfoSet, deviceInterfaceData)); deviceInterfaceIndex++; } } NativeInterface.SetupDiDestroyDeviceInfoList(deviceInfoSet); } return(ret); }
private static string _GetDevicePath(IntPtr deviceInfoSet, NativeInterface.SP_DEVICE_INTERFACE_DATA deviceInterfaceData) { var bufferSize = 0; var interfaceDetail = new NativeInterface.SP_DEVICE_INTERFACE_DETAIL_DATA { Size = IntPtr.Size == 4 ? 4 + Marshal.SystemDefaultCharSize : 8 }; NativeInterface.SetupDiGetDeviceInterfaceDetailBuffer(deviceInfoSet, ref deviceInterfaceData, IntPtr.Zero, 0, ref bufferSize, IntPtr.Zero); return(NativeInterface.SetupDiGetDeviceInterfaceDetail(deviceInfoSet, ref deviceInterfaceData, ref interfaceDetail, bufferSize, ref bufferSize, IntPtr.Zero) ? interfaceDetail.DevicePath : null); }
public static IEnumerable<string> GetAllDevicePaths(Guid guid) { var ret = new List<string>(); var deviceInfoSet = NativeInterface.SetupDiGetClassDevs(ref guid, null, 0, NativeInterface.DIGCF_PRESENT | NativeInterface.DIGCF_DEVICEINTERFACE); if (deviceInfoSet.ToInt64() != NativeInterface.INVALID_HANDLE_VALUE) { var deviceInfoData = _CreateDeviceInfoData(); var deviceIndex = 0; while (NativeInterface.SetupDiEnumDeviceInfo(deviceInfoSet, deviceIndex, ref deviceInfoData)) { deviceIndex += 1; var deviceInterfaceData = new NativeInterface.SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = Marshal.SizeOf(deviceInterfaceData); var deviceInterfaceIndex = 0; while (NativeInterface.SetupDiEnumDeviceInterfaces(deviceInfoSet, ref deviceInfoData, ref guid, deviceInterfaceIndex, ref deviceInterfaceData)) { ret.Add(_GetDevicePath(deviceInfoSet, deviceInterfaceData)); deviceInterfaceIndex++; } } NativeInterface.SetupDiDestroyDeviceInfoList(deviceInfoSet); } return ret; }