static extern Boolean SetupDiGetDeviceInterfaceDetail( IntPtr hDevInfo, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA_W deviceInterfaceDetailData, UInt32 deviceInterfaceDetailDataSize, out UInt32 requiredSize, ref SP_DEVINFO_DATA deviceInfoData );
public IEnumerable<DeviceInfo> GetAllDevices() { SP_DEVICE_INTERFACE_DATA deviceInfoData = new SP_DEVICE_INTERFACE_DATA(); for (uint i = 0; ; i++) { deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData); if (!SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, IntPtr.Zero, // We don't care about specific PDOs ref _InterfaceGUID, i, ref deviceInfoData)) { int Error = Marshal.GetLastWin32Error(); if (Error == 259) //ERROR_NO_MORE_ITEMS yield break; } else { uint requiredLength = 0; SP_DEVINFO_DATA devinfoData = new SP_DEVINFO_DATA(); SetupDiGetDeviceInterfaceDetail( hardwareDeviceInfo, ref deviceInfoData, IntPtr.Zero, // probing so no output buffer yet 0, // probing so output buffer length of zero out requiredLength, IntPtr.Zero); // not interested in the specific dev-node devinfoData.cbSize = (UInt32)Marshal.SizeOf(devinfoData); SP_DEVICE_INTERFACE_DETAIL_DATA_W functionClassDeviceData = new SP_DEVICE_INTERFACE_DETAIL_DATA_W(); functionClassDeviceData.cbSize = (IntPtr.Size == 8) ? 8U : 6U; // // Retrieve the information from Plug and Play. // if (!SetupDiGetDeviceInterfaceDetail( hardwareDeviceInfo, ref deviceInfoData, ref functionClassDeviceData, requiredLength, out requiredLength, ref devinfoData)) { int err = Marshal.GetLastWin32Error(); continue; } int requiredSizeForID = 0; SetupDiGetDeviceInstanceId(hardwareDeviceInfo, ref devinfoData, IntPtr.Zero, 0, out requiredSizeForID); StringBuilder deviceID = new StringBuilder(requiredSizeForID); if (!SetupDiGetDeviceInstanceId(hardwareDeviceInfo, ref devinfoData, deviceID, requiredSizeForID, out requiredSizeForID)) continue; uint type, nameSize; SetupDiGetDeviceRegistryProperty(hardwareDeviceInfo, ref devinfoData, SPDRP.SPDRP_FRIENDLYNAME, out type, null, 0, out nameSize); StringBuilder friendlyName = new StringBuilder((int)nameSize); if (!SetupDiGetDeviceRegistryProperty(hardwareDeviceInfo, ref devinfoData, SPDRP.SPDRP_FRIENDLYNAME, out type, friendlyName, nameSize, out nameSize)) { friendlyName.Length = 0; friendlyName.Append("???"); } yield return new DeviceInfo(this, devinfoData, functionClassDeviceData.DevicePath.ToString(), deviceID.ToString(), friendlyName.ToString()); } } }
public IEnumerable <DeviceInfo> GetAllDevices() { SP_DEVICE_INTERFACE_DATA deviceInfoData = new SP_DEVICE_INTERFACE_DATA(); for (uint i = 0; ; i++) { deviceInfoData.cbSize = Marshal.SizeOf(deviceInfoData); if (!SetupDiEnumDeviceInterfaces(hardwareDeviceInfo, IntPtr.Zero, // We don't care about specific PDOs ref _InterfaceGUID, i, ref deviceInfoData)) { int Error = Marshal.GetLastWin32Error(); if (Error == 259) //ERROR_NO_MORE_ITEMS { yield break; } } else { uint requiredLength = 0; SP_DEVINFO_DATA devinfoData = new SP_DEVINFO_DATA(); SetupDiGetDeviceInterfaceDetail( hardwareDeviceInfo, ref deviceInfoData, IntPtr.Zero, // probing so no output buffer yet 0, // probing so output buffer length of zero out requiredLength, IntPtr.Zero); // not interested in the specific dev-node devinfoData.cbSize = (UInt32)Marshal.SizeOf(devinfoData); SP_DEVICE_INTERFACE_DETAIL_DATA_W functionClassDeviceData = new SP_DEVICE_INTERFACE_DETAIL_DATA_W(); functionClassDeviceData.cbSize = (IntPtr.Size == 8) ? 8U : 6U; // // Retrieve the information from Plug and Play. // if (!SetupDiGetDeviceInterfaceDetail( hardwareDeviceInfo, ref deviceInfoData, ref functionClassDeviceData, requiredLength, out requiredLength, ref devinfoData)) { int err = Marshal.GetLastWin32Error(); continue; } int requiredSizeForID = 0; SetupDiGetDeviceInstanceId(hardwareDeviceInfo, ref devinfoData, IntPtr.Zero, 0, out requiredSizeForID); StringBuilder deviceID = new StringBuilder(requiredSizeForID); if (!SetupDiGetDeviceInstanceId(hardwareDeviceInfo, ref devinfoData, deviceID, requiredSizeForID, out requiredSizeForID)) { continue; } uint type, nameSize; SetupDiGetDeviceRegistryProperty(hardwareDeviceInfo, ref devinfoData, SPDRP.SPDRP_FRIENDLYNAME, out type, null, 0, out nameSize); StringBuilder friendlyName = new StringBuilder((int)nameSize); if (!SetupDiGetDeviceRegistryProperty(hardwareDeviceInfo, ref devinfoData, SPDRP.SPDRP_FRIENDLYNAME, out type, friendlyName, nameSize, out nameSize)) { friendlyName.Length = 0; friendlyName.Append("???"); } yield return(new DeviceInfo(this, devinfoData, functionClassDeviceData.DevicePath.ToString(), deviceID.ToString(), friendlyName.ToString())); } } }