Hello,
I am trying to read data from a HID device from VB.NET.
It works fine using Jan's great generic hid application.
Now I have copy-pasted his code into my project, but when I try to do read data (input interrupt transfer) using this:
ar = MyReadInputReportDelegate.BeginInvoke _
(hidHandle, _
readHandle, _
writeHandle, _
myDeviceDetected, _
inputReportBuffer, _
success, _
New AsyncCallback(AddressOf GetInputReportData), _
MyReadInputReportDelegate)
VB.NET stops my program and complains that a "Managed Debugging Assistant" has detected a problem in read() from Hid.vb, here:
success = FileIO.ReadFile _
(readHandle, _
nonmanagedBuffer, _
inputReportBuffer.Length, _
numberOfBytesRead, _
nonmanagedOverlapped)
The culprit seems to be "nonmanagedOverlapped" which has apparently not been allocated as .NET expects.
The error message is:
InvalidOverlappedToPinvoke was detected
Message: An overlapped pointer (0xblahblah) that was not allocated
on the GC heap was passed via PInvoke to the Win32 function 'ReadFile' in
module 'kernel32.DLL'. If the AppDomain is shut down, this can cause heap
corruption when the async I/O completes. The best solution is to pass a
NativeOverlapped structure retrieved from a call to
System.Threading.Overlapped.Pack(). If the AppDomain exits, the CLR will keep
this structure alive and pinned until the I/O completes.
where blahblah is the value of nonmanagedOverlapped.
Any help would be greatly appreciated. I am not too sure about what additional information might be relevant.
What I really do not understand is why it works in Jan's project and not in mine.
Thanks !