Click here to Skip to main content
15,507,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been using LibUsbDotNet for a few years now and all of my development has been done on a W7 machine running (currently) VS2022. LibUsbDotNet provides the 'WinForms' end of my projects - the other ends are all AtTiny or AtMega microcontrollers using Objective Development's firmware-only USB driver VUSB. I have made an in-circuit programmer for Atmel devices, a controller and throttles for DCC trains and a system for controlling and monitoring my Electric Master Clock system. These all work fine including a bootloader interface to avoid having to plug my in-circuit programmer in every time I make a change to the microcontroller code
A problem arises when running the WinForms app under W10. If my USB device is connected before starting the WinForms app then all is well. If I start the WimForms app first and then plug in the USB device it is not detected (If I run the WinForms app on a W7 machine it all works fine and detects insertions/removals correctly). Worse, if the WinForms app is run in Debug mode, it crashes when the USB decide is plugged in (again, it's fine on a W7 machine)

What I have tried:

In order to simplify the problem I have made a very simple app which just detects insertions and removals and logs the result on a text box.
Code shown below :-


using LibUsbDotNet.DeviceNotify;


namespace USB_Test
public partial class Form1 : Form
private readonly IDeviceNotifier UsbDeviceNotifier;
private readonly TextBox text_box;

public Form1()
Text = "USB test";

UsbDeviceNotifier = DeviceNotifier.OpenDeviceNotifier();

UsbDeviceNotifier.OnDeviceNotify += OnDeviceNotifyEvent;

text_box = new TextBox
Multiline = true,
ReadOnly = true,
Width = 200,
Height = 400,
Top = 20,
Left = 20


Width = 300;

Height = text_box.Bottom + 50;


private void OnDeviceNotifyEvent(object? sender, DeviceNotifyEventArgs e)

text_box.AppendText(e.ToString()); // Dump the event info to output.



This works fine on W7 machine whether in debug or release mode. On W10 machine it just crashes in debug mode and appears to do nothing in release mode
Compiled with Net 6 and using V2.2.8 of LibUsbDotNet

Any ideas please ???
Error occurs in 'Main' :-
static void Main()
            // To customize application configuration such as set high DPI settings or default font,
            // see
            Application.Run(new Form1());     <------ error here

Error shown as :-

System.OverflowException: 'Arithmetic operation resulted in an overflow.'
This exception was originally thrown at this call stack:
    [External Code]

The error details show it occurred in LibUsbDotNet.DeviceNotify so I guess that is a dead-end as far as Windows is concerned and will have to get hold of the author ????
Updated 14-May-22 11:50am
Richard MacCutchan 14-May-22 8:27am    
It's impossible to guess what might be going wrong in such cases. The only thing you can do is use the debugger to try and find where the crash occurs, and try and establish why.

1 solution

You're going to have to get a hold of the authors of the library.
Share this answer
Member 4582654 15-May-22 14:02pm    
Somewhat difficult to get hold of author. However, when trying to come up with an alternative I found another USB Library - built it and tried to run with my simple test program - it too crashed on W10. Eventually had a 'Road to Damascus' moment - if I build the WinForms apps in x86 instead of 'any CPU' it all woks fine (the notes on the alternative USB Library mentioned it). I did the same with the app using LibUsbDotNet and also works fine!!! Still a W10 problem though but, hey ho, I can put up with that
Thanks everyone
Dave Kreskowiak 15-May-22 15:50pm    
Well, I used HidSharp in my project for UPS monitoring over USB. It works flawlessly, on Win10, compiled either x86 or x64 without any changes to the code.

I seriously doubt it's a Win 10 issue.
Member 4582654 15-May-22 17:03pm    
Ah, thanks. I'll have a look at HIDSharp. May very well not be a W10 issue but still doesn't explain difference between W7 and W10 operation. But, as I said, I can live with having to build as x86
Dave Kreskowiak 15-May-22 19:33pm    
It could be a problem with the library not written to support the updated security environments in Windows 10. Only the authors of the libraries are going to know.
Member 4582654 16-May-22 13:13pm    
Managed to find source code for LibUsbDotNet so was able to build in debug mode and find out where the error occurs. In the DeviceNotifier module (where the exception occurs), the LParam of a Message, which is an IntPtr type, is converted to an integer by its Int32() method and then tested for zero. The problem is that, on a 64 bit system, the value of this instance is too large or too small to represent as a 32-bit signed integer - ergo an exception. I've changed to using the ToInt64() method and all is now fine

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900