PORTS Forum

Ports and Interfaces => USB => Topic started by: Simon. on November 07, 2010, 01:54:30 pm

Title: InvalidOverlappedtoPinvoke .NET error
Post by: Simon. on November 07, 2010, 01:54:30 pm
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 !
Title: Re: InvalidOverlappedtoPinvoke .NET error
Post by: Jan Axelson on November 07, 2010, 02:52:01 pm
Did you allocate nonmanagedOverlapped as I did in my application?

Jan
Title: Re: InvalidOverlappedtoPinvoke .NET error
Post by: Simon. on November 08, 2010, 09:07:32 am
Yes, I have kept Hid.vb unmodified.

Title: Re: InvalidOverlappedtoPinvoke .NET error
Post by: Jan Axelson on November 08, 2010, 08:13:03 pm
I don't have a definitive solution but this might be of interest:

http://www.beefycode.com/post/Using-Overlapped-IO-from-Managed-Code.aspx

It uses C# and DeviceIoControl but shows how to use System.Threading.Overlapped.Pack().

Jan
Title: Re: InvalidOverlappedtoPinvoke .NET error
Post by: Simon. on November 09, 2010, 04:17:07 am
I had also found this. So I tried to do the same, but it seems that it is not possible to do this in VB.NET for the .NET 2.0 framework.

Quoting http://msdn.microsoft.com/en-us/library/a059s071%28v=VS.80%29.aspx :
Visual Basic does not support APIs that consume or return unsafe types.


And I figured that since your generic hid tool worked without it, mine should also.

Anyway, I have found Mike O'Brien's HID Library for .NET. It is much less flexible than your example code but simpler to use (I think I used an old version of this library, found in the 'Archive' folder) since it is all synchronous calls (which is sufficient in my app).

Thanks a lot for your time, Jan.
And thanks also for your generic hid tool, which was really helpful when I was working to get Windows to accept my HID report descriptor :)
Title: Re: InvalidOverlappedtoPinvoke .NET error
Post by: jdunne on November 11, 2010, 11:01:39 am
Hello,

I'm guessing you had the same problem I did.  It looks like you're using a different library to solve your problem, but in case there are issues with it, for a test, just try disabling the break on exception for MDA.  See my post about this:
http://www.lvr.com/forum/index.php?topic=177.0

Hopefully we can come up with a proper solution to this bug.

Joe
Title: Re: InvalidOverlappedtoPinvoke .NET error
Post by: Simon. on November 11, 2010, 11:55:50 am
Thank you Jdunne.

Quote
Well, it turns out that it does work just fine, so I went back to the generichid_cs project and found that generichid_cs disabled the exceptions just as I did.

Oh, well :-)
That's exactly the kind of explanation that I hoped to receive when I posted my question on this forum.
Thanks :-)

Title: Re: InvalidOverlappedtoPinvoke .NET error
Post by: jdunne on November 15, 2010, 12:47:16 pm
Simon,

Its probably a bit late by now, as I'm sure you've moved on with another library.  Anyway, I came up with a solution that will work for VB.NET.  See my post here for the solution:
http://www.lvr.com/forum/index.php?topic=177.0

Good luck.

Joe
Title: Re: InvalidOverlappedtoPinvoke .NET error
Post by: Simon. on November 15, 2010, 01:01:14 pm
Indeed Joe, but thanks anyway :)