public static extern bool SetupDiGetDeviceRegistryProperty( IntPtr deviceInfoSet, ref SpDevInfoData deviceInfoData, uint property, out UInt32 propertyRegDataType, StringBuilder propertyBuffer, uint propertyBufferSize, out UInt32 requiredSize);
public static extern bool SetupDiGetDeviceRegistryProperty( IntPtr deviceInfoSet, ref SpDevInfoData deviceInfoData, uint property, out UInt32 propertyRegDataType, StringBuilder propertyBuffer, uint propertyBufferSize, out UInt32 requiredSize);
public static FileStream Open(string deviceId) { deviceId = deviceId.ToLower(); FileStream fileStream = (FileStream) null; Guid lpHidGuid; UsbAPI.HidD_GetHidGuid(out lpHidGuid); IntPtr classDevs = UsbAPI.SetupDiGetClassDevs(ref lpHidGuid, IntPtr.Zero, IntPtr.Zero, 18); SpDevInfoData deviceInfoData = new SpDevInfoData(); deviceInfoData.cbSize = (uint) Marshal.SizeOf((object) deviceInfoData); for (uint memberIndex = 0U; UsbAPI.SetupDiEnumDeviceInfo(classDevs, memberIndex, ref deviceInfoData); ++memberIndex) { uint requiredSize1 = 4096U; StringBuilder propertyBuffer = new StringBuilder((int) requiredSize1); uint propertyRegDataType; UsbAPI.SetupDiGetDeviceRegistryProperty(classDevs, ref deviceInfoData, 1U, out propertyRegDataType, propertyBuffer, requiredSize1, out requiredSize1); if (propertyBuffer.ToString().ToLower().Contains(deviceId)) { SpDeviceInterfaceData deviceInterfaceData = new SpDeviceInterfaceData(); deviceInterfaceData.cbSize = Marshal.SizeOf((object) deviceInterfaceData); UsbAPI.SetupDiEnumDeviceInterfaces(classDevs, IntPtr.Zero, ref lpHidGuid, memberIndex, ref deviceInterfaceData); int requiredSize2; UsbAPI.SetupDiGetDeviceInterfaceDetail(classDevs, ref deviceInterfaceData, IntPtr.Zero, 0, out requiredSize2, IntPtr.Zero); SpDeviceInterfaceDetailData deviceInterfaceDetailData = new SpDeviceInterfaceDetailData(); deviceInterfaceDetailData.cbSize = IntPtr.Size == 8 ? 8 : 4 + Marshal.SystemDefaultCharSize; UsbAPI.SetupDiGetDeviceInterfaceDetail(classDevs, ref deviceInterfaceData, ref deviceInterfaceDetailData, requiredSize2, IntPtr.Zero, IntPtr.Zero); SafeFileHandle handle = new SafeFileHandle(UsbAPI.CreateFile(deviceInterfaceDetailData.DevicePath, 3221225472U, 3U, IntPtr.Zero, 3U, 0U, IntPtr.Zero), true); if (!handle.IsInvalid) { fileStream = new FileStream(handle, FileAccess.ReadWrite); break; } break; } } UsbAPI.SetupDiDestroyDeviceInfoList(classDevs); if (fileStream == null) throw new ApplicationException("Device not found"); return fileStream; }
public static FileStream Open(string deviceId) { deviceId = deviceId.ToLower(); FileStream file = null; Guid guid; UsbAPI.HidD_GetHidGuid(out guid); var hDevInfo = UsbAPI.SetupDiGetClassDevs( ref guid, IntPtr.Zero, IntPtr.Zero, UsbAPI.DigcfInterfaceDevice | UsbAPI.DigcfPresent); var deviceInfoData = new SpDevInfoData(); deviceInfoData.cbSize = (uint) Marshal.SizeOf(deviceInfoData); for (var i = 0u; UsbAPI.SetupDiEnumDeviceInfo(hDevInfo, i, ref deviceInfoData); i++) { uint bufferSize = 1024 * 4; var sb = new StringBuilder((int) bufferSize); uint propertyInfoRegDataType; UsbAPI.SetupDiGetDeviceRegistryProperty( hDevInfo, ref deviceInfoData, UsbAPI.SpdrpHardwareId, out propertyInfoRegDataType, sb, bufferSize, out bufferSize); if (sb.ToString().ToLower().Contains(deviceId)) { var interfaceData = new SpDeviceInterfaceData(); interfaceData.cbSize = Marshal.SizeOf(interfaceData); UsbAPI.SetupDiEnumDeviceInterfaces( hDevInfo, IntPtr.Zero, ref guid, i, ref interfaceData); int requiredSize; UsbAPI.SetupDiGetDeviceInterfaceDetail( hDevInfo, ref interfaceData, IntPtr.Zero, 0, out requiredSize, IntPtr.Zero); var interfaceDetail = new SpDeviceInterfaceDetailData(); interfaceDetail.cbSize = IntPtr.Size == 8 ? 8 : 4 + Marshal.SystemDefaultCharSize; UsbAPI.SetupDiGetDeviceInterfaceDetail( hDevInfo, ref interfaceData, ref interfaceDetail, requiredSize, IntPtr.Zero, IntPtr.Zero); var unsafeHandle = UsbAPI.CreateFile( interfaceDetail.DevicePath, UsbAPI.GenericRead | UsbAPI.GenericWrite, UsbAPI.FileShareRead | UsbAPI.FileShareWrite, IntPtr.Zero, UsbAPI.OpenExisting, 0, IntPtr.Zero); var hidHandle = new SafeFileHandle(unsafeHandle, true); if (!hidHandle.IsInvalid) { file = new FileStream(hidHandle, FileAccess.ReadWrite); } break; } } UsbAPI.SetupDiDestroyDeviceInfoList(hDevInfo); if (file == null) { throw new ApplicationException("Device not found"); } return file; }
public static extern bool SetupDiEnumDeviceInfo(IntPtr deviceInfoSet, uint memberIndex, ref SpDevInfoData deviceInfoData);
public static FileStream Open(string deviceId) { deviceId = deviceId.ToLower(); FileStream file = null; Guid guid; UsbAPI.HidD_GetHidGuid(out guid); var hDevInfo = UsbAPI.SetupDiGetClassDevs( ref guid, IntPtr.Zero, IntPtr.Zero, UsbAPI.DigcfInterfaceDevice | UsbAPI.DigcfPresent); var deviceInfoData = new SpDevInfoData(); deviceInfoData.cbSize = (uint)Marshal.SizeOf(deviceInfoData); for (var i = 0u; UsbAPI.SetupDiEnumDeviceInfo(hDevInfo, i, ref deviceInfoData); i++) { uint bufferSize = 1024 * 4; var sb = new StringBuilder((int)bufferSize); uint propertyInfoRegDataType; UsbAPI.SetupDiGetDeviceRegistryProperty( hDevInfo, ref deviceInfoData, UsbAPI.SpdrpHardwareId, out propertyInfoRegDataType, sb, bufferSize, out bufferSize); if (sb.ToString().ToLower().Contains(deviceId)) { var interfaceData = new SpDeviceInterfaceData(); interfaceData.cbSize = Marshal.SizeOf(interfaceData); UsbAPI.SetupDiEnumDeviceInterfaces( hDevInfo, IntPtr.Zero, ref guid, i, ref interfaceData); int requiredSize; UsbAPI.SetupDiGetDeviceInterfaceDetail( hDevInfo, ref interfaceData, IntPtr.Zero, 0, out requiredSize, IntPtr.Zero); var interfaceDetail = new SpDeviceInterfaceDetailData(); interfaceDetail.cbSize = IntPtr.Size == 8 ? 8 : 4 + Marshal.SystemDefaultCharSize; UsbAPI.SetupDiGetDeviceInterfaceDetail( hDevInfo, ref interfaceData, ref interfaceDetail, requiredSize, IntPtr.Zero, IntPtr.Zero); var unsafeHandle = UsbAPI.CreateFile( interfaceDetail.DevicePath, UsbAPI.GenericRead | UsbAPI.GenericWrite, UsbAPI.FileShareRead | UsbAPI.FileShareWrite, IntPtr.Zero, UsbAPI.OpenExisting, 0, IntPtr.Zero); var hidHandle = new SafeFileHandle(unsafeHandle, true); if (!hidHandle.IsInvalid) { file = new FileStream(hidHandle, FileAccess.ReadWrite); } break; } } UsbAPI.SetupDiDestroyDeviceInfoList(hDevInfo); if (file == null) { throw new ApplicationException("Device not found"); } return(file); }
public static extern bool SetupDiEnumDeviceInfo( IntPtr deviceInfoSet, uint memberIndex, ref SpDevInfoData deviceInfoData);