469,117 Members | 1,293 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,117 developers. It's quick & easy.

ReadFile OVERLAPPED

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("ReadF ile"));
}

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(HIDOverl apped.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


Jun 21 '07 #1
4 12999
>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("ReadF ile"));
}
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.
Jun 21 '07 #2
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 (wa****@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.

Jun 22 '07 #3
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(HIDOverl apped.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]" <wa****@online.microsoft.comwrote in message
news:Ig**************@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 (wa****@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.

Jun 22 '07 #4
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/is...urrentAffairs/
I think this should be useful for you.
Regards,
Walter Wang (wa****@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.

Jun 25 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Mustafa Ahmad Malik | last post: by
3 posts views Thread by Shawn August | last post: by
2 posts views Thread by GTi | last post: by
2 posts views Thread by CeZaR | last post: by
1 post views Thread by dvestal | last post: by
15 posts views Thread by Ketchup | last post: by
7 posts views Thread by =?Utf-8?B?TmljayBCdXJraXR0?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Mortomer39 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.