E
Eric Renken
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
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