Author Topic: InvalidOverlappedtoPinvoke .NET error  (Read 25965 times)

Simon.

  • Member
  • ***
  • Posts: 5
InvalidOverlappedtoPinvoke .NET error
« 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 !

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: InvalidOverlappedtoPinvoke .NET error
« Reply #1 on: November 07, 2010, 02:52:01 pm »
Did you allocate nonmanagedOverlapped as I did in my application?

Jan

Simon.

  • Member
  • ***
  • Posts: 5
Re: InvalidOverlappedtoPinvoke .NET error
« Reply #2 on: November 08, 2010, 09:07:32 am »
Yes, I have kept Hid.vb unmodified.


Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: InvalidOverlappedtoPinvoke .NET error
« Reply #3 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

Simon.

  • Member
  • ***
  • Posts: 5
Re: InvalidOverlappedtoPinvoke .NET error
« Reply #4 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 :)
« Last Edit: November 09, 2010, 04:19:42 am by Simon. »

jdunne

  • Member
  • ***
  • Posts: 26
Re: InvalidOverlappedtoPinvoke .NET error
« Reply #5 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

Simon.

  • Member
  • ***
  • Posts: 5
Re: InvalidOverlappedtoPinvoke .NET error
« Reply #6 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 :-)


jdunne

  • Member
  • ***
  • Posts: 26
Re: InvalidOverlappedtoPinvoke .NET error
« Reply #7 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

Simon.

  • Member
  • ***
  • Posts: 5
Re: InvalidOverlappedtoPinvoke .NET error
« Reply #8 on: November 15, 2010, 01:01:14 pm »
Indeed Joe, but thanks anyway :)