public static extern Boolean SetupDiGetDeviceInterfaceDetail( IntPtr hDevInfo, ref SP_DEVICE_INTERFACE_DATA deviceInterfaceData, ref SP_DEVICE_INTERFACE_DETAIL_DATA deviceInterfaceDetailData, UInt32 deviceInterfaceDetailDataSize, out UInt32 requiredSize, ref SP_DEVINFO_DATA deviceInfoData );
public static extern bool SetupDiGetDeviceRegistryProperty( IntPtr deviceInfoSet, ref SP_DEVINFO_DATA deviceInfoData, SPDRP property, out UInt32 propertyRegDataType, IntPtr propertyBuffer, uint propertyBufferSize, out UInt32 requiredSize );
private string FindDevicePath() { var hidGuid = HidGuid; var deviceInfoList = SetupApi.SetupDiGetClassDevs(ref hidGuid, null, IntPtr.Zero, DIGCF.DeviceInterface | DIGCF.Present); if (IntPtr.Zero == deviceInfoList) return null; try { for (uint i = 0; ; i++) { var deviceInterfaceData = new SP_DEVICE_INTERFACE_DATA(); deviceInterfaceData.cbSize = (uint) Marshal.SizeOf(deviceInterfaceData); if (!SetupApi.SetupDiEnumDeviceInterfaces(deviceInfoList, IntPtr.Zero, ref hidGuid, i, ref deviceInterfaceData)) break; // end of the list var deviceInfoData = new SP_DEVINFO_DATA(); deviceInfoData.cbSize = (uint)Marshal.SizeOf(deviceInfoData); if (!SetupApi.SetupDiEnumDeviceInfo(deviceInfoList, i, ref deviceInfoData)) continue; const uint bufferSize = 1048; var propertyBuffer = Marshal.AllocHGlobal((int)bufferSize); try { uint requiredSize; uint propRegDataType; if (!SetupApi.SetupDiGetDeviceRegistryProperty( deviceInfoList, ref deviceInfoData, SPDRP.HardwareId, out propRegDataType, propertyBuffer, bufferSize, out requiredSize) ) { continue; } var deviceId = Marshal.PtrToStringAuto(propertyBuffer, (int) requiredSize); if (String.IsNullOrEmpty(deviceId) || !deviceId.ToUpperInvariant().Contains(_deviceIdUpper) ) { continue; } var deviceInterfaceDetailData = new SP_DEVICE_INTERFACE_DETAIL_DATA { cbSize = IntPtr.Size == 8 ? 8 : (uint)(4 + Marshal.SystemDefaultCharSize) }; var interfaceDetailOk = SetupApi.SetupDiGetDeviceInterfaceDetail( deviceInfoList, ref deviceInterfaceData, ref deviceInterfaceDetailData, SP_DEVICE_INTERFACE_DETAIL_DATA.BUFFER_SIZE, out requiredSize, ref deviceInfoData); if (interfaceDetailOk) return deviceInterfaceDetailData.devicePath; } finally { Marshal.FreeHGlobal(propertyBuffer); } } } finally { SetupApi.SetupDiDestroyDeviceInfoList(deviceInfoList); } return null; }
public static extern bool SetupDiEnumDeviceInfo( IntPtr deviceInfoSet, uint memberIndex, ref SP_DEVINFO_DATA deviceInfoData );