From: Eric Renken on
I am trying to do an Overlapped ReadFile on a HID device and it just isn't
working for me. The WaitForSingleObject keeps giving me an error "The
system cannot find the file specified."

This code need to work in 64bit and 32bit, so I am using IntPtr for pointers
instead of int.

Here is the structure of OVERLAPPED
[StructLayout(LayoutKind.Sequential)]
public struct OVERLAPPED
{
public IntPtr Internal;
public IntPtr InternalHigh;
public int Offset;
public int OffsetHigh;
public IntPtr hEvent;
}

Here is how I setup the event:

IntPtr eventObject = FileIOAPIDeclarations.CreateEvent( IntPtr.Zero,
System.Convert.ToInt32(false), System.Convert.ToInt32(false), "");

Here is the declaration of the CreateEvent:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static public extern IntPtr CreateEvent(IntPtr SecurityAttributes, int
bManualReset, int bInitialState, string lpName);

I set the OVERLAPPED.Offset and OffsetHigh = 0, and then I set hEvent =
eventObject.

I then call my ReadFile

fixed (byte* p = buffer)
{
result = FileIOAPIDeclarations.ReadFile(_hidHandle, p, buffer.Length,
&bytesRead, ref HIDOverlapped);
Debug.WriteLine(MyDebugging.ResultOfAPICall("ReadFile"));
}

The decliration for the ReadFile is this:

[DllImport("kernel32.dll", SetLastError = true)]
static public extern unsafe int ReadFile(IntPtr hFile, void* lpBuffer, int
nNumberOfBytesToRead, int* lpNumberOfBytesRead, ref
FileIOAPIDeclarations.OVERLAPPED lpOverlapped);


This ReadFile declaration and call is required for 64 bit to work correctly.

After I call the ReadFile I do get a message from the GetLastError() that
"Overlapped I/O operation is in process."

Then I call the WaitForSingleObject

result = FileIOAPIDeclarations.WaitForSingleObject(HIDOverlapped.hEvent,
3000);

WaitForSingleObject declaration

[DllImport("kernel32.dll", SetLastError = true)]
static public extern int WaitForSingleObject(IntPtr hHandle, int
dwMilliseconds);

After 3 seconds this always returns timeout (0x102, 258) and the
GetLastError() is: "The system cannot find the file specified."

To me this is telling me that one of my structures is off, but I am not sure
which one.

Thanks in advance for any help.

Eric Renken




From: Mattias Sj�gren on
>Here is the declaration of the CreateEvent:
> [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
> static public extern IntPtr CreateEvent(IntPtr SecurityAttributes, int
>bManualReset, int bInitialState, string lpName);

FYI you can use bool for the boolean parameters, so you don't have to
use Convert.ToInt32.


>
>I set the OVERLAPPED.Offset and OffsetHigh = 0, and then I set hEvent =
>eventObject.
>
>I then call my ReadFile
>
> fixed (byte* p = buffer)
> {
> result = FileIOAPIDeclarations.ReadFile(_hidHandle, p, buffer.Length,
>&bytesRead, ref HIDOverlapped);
> Debug.WriteLine(MyDebugging.ResultOfAPICall("ReadFile"));
> }

Not sure if this is what's causing your problem. But the p pointer is
only valid inside the fixed block. It's not suitable for asynch
procedure calls. You have to make sure you pass a pointer that's valid
until you've finished reading. You can do that by either manually
pinning the array or allocating a buffer from a native heap.


Mattias

--
Mattias Sj�gren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
From: Walter Wang [MSFT] on
I agree with Mattias here. Eric, you should be able to use GCHandle to pin
the buffer or use Marshal.AllocHGlobal.

Let us know if this helps.


Regards,
Walter Wang (wawang(a)online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

From: Eric Renken on
Well this code works:

GCHandle gch = GCHandle.Alloc(buffer, GCHandleType.Pinned);
result = FileIOAPIDeclarations.ReadFile(_hidHandle,
gch.AddrOfPinnedObject(), buffer.Length, &bytesRead, ref HIDOverlapped);

if (result != 0)
success = true;

result = FileIOAPIDeclarations.WaitForSingleObject(HIDOverlapped.hEvent,
500);

gch.Free();

_readHandle = IntPtr.Zero;

switch (result)
{
case FileIOAPIDeclarations.WAIT_OBJECT_0:
success = true;
break;

case FileIOAPIDeclarations.WAIT_TIMEOUT:
success = false;
break;
}


Thank you all, the really strange thing is that our device that you do a
Write and an immediate Read to doesn't work with this code. I still get a
Time-out Error. With a new device we are working on this code works great;
however I am finding that I need to close and then reopen the connection for
every read/time-out. I am working in a loop here, so I keep monitoring this
device for data, if someone has a better way that would also be great,
anyway I find that if I call the read again after the time-out and then I
put data on the device I have to wait until that data is put onto the device
there for as many times as the time-out occurred.

What I am saying is that if the Read timed-out 5 times, I have to put data
on the device 6 times before the Read returns any data.

Thanks again,

Eric Renken



"Walter Wang [MSFT]" <wawang(a)online.microsoft.com> wrote in message
news:IgrMwjKtHHA.2116(a)TK2MSFTNGHUB02.phx.gbl...
>I agree with Mattias here. Eric, you should be able to use GCHandle to pin
> the buffer or use Marshal.AllocHGlobal.
>
> Let us know if this helps.
>
>
> Regards,
> Walter Wang (wawang(a)online.microsoft.com, remove 'online.')
> Microsoft Online Community Support
>
> ==================================================
> When responding to posts, please "Reply to Group" via your newsreader so
> that others may learn and benefit from your issue.
> ==================================================
>
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>


From: Walter Wang [MSFT] on
Hi Eric,

Thanks for the update.

I was just informed by Jeffrey Richter that he has a new article on MSDN
Magazine about asynchronous device I/O in managed code:

#Concurrent Affairs: Asynchronous Device Operations -- MSDN Magazine, June
2007
http://msdn.microsoft.com/msdnmag/issues/07/06/ConcurrentAffairs/


I think this should be useful for you.


Regards,
Walter Wang (wawang(a)online.microsoft.com, remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.