Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to do asynchronous I/O on a device. My problem is ReadFile returns ERROR_INVALID_HANDLE.

I believe both Handle and OVERLAPPED hEvent are initialized so would very much appreciate it if someone could tell me what I'm doing wrong.

What I called a device is the Windows driver sample ndisprot which is a NDIS protocol driver.

What I have tried:

I have synchronous I/O and asynchronous I/O using ReadFileEx/WriteFileEx working.

This is the code I'm using:
HANDLE Handle = CreateFileA(
    ndisprot630,
    GENERIC_READ | GENERIC_WRITE,
    0,
    NULL,
    OPEN_EXISTING,
    FILE_FLAG_OVERLAPPED,
    NULL
);
if (Handle == INVALID_HANDLE_VALUE)
{
    DEBUGP("CreateFile failed, error 0x%x\n", GetLastError());
    return(INVALID_HANDLE_VALUE);
}

OVERLAPPED ol = { 0 };
ol.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
char buf[1500];

if (!ReadFile(Handle, buf, sizeof(buf), NULL, &ol))
{
    if (GetLastError() != ERROR_IO_PENDING)
    {
        DEBUGP("Error starting ReadFile 0x%x\n", GetLastError());
    }
}
Posted
Updated 14-Dec-23 23:03pm
v3
Comments
Richard MacCutchan 14-Dec-23 12:13pm    
I just ran that code and it works fine. So check your actual test again.
Dave Kreskowiak 14-Dec-23 16:44pm    
You said you're trying to open "a device". What device? What's the path you're using to describe that device? Without that, answers are only going to be guesses.
dridu 15-Dec-23 4:50am    
I'm using the Windows driver sample ndisprot(630) which is a NDIS protocol driver.
Dave Kreskowiak 15-Dec-23 9:28am    
And how is "ndisprot630" defined? Are you even sure the device supports overlapped I/O?

As with any file Read/Write or Create make sure the file path has your access rights to begin with, i.e. try creating a notepad text file in that location manually to test access. If that fails than everything else would fail with programmatic approach.
 
Share this answer
 
 
Share this answer
 
Comments
dridu 15-Dec-23 7:43am    
I have now read your link but unfortunately fail to get what you wanted me to learn from it?
Richard MacCutchan 15-Dec-23 8:00am    
Maybe that is because we still have no real idea what your problem is.
dridu 15-Dec-23 8:17am    
I have the Windows sample driver ndisprot(630) which I'm trying to start an asynchronous read with using the provided code. But my problem is ReadFile in the provided code returns ERROR_INVALID_HANDLE. From this, what would you like me to explain further?
Richard MacCutchan 15-Dec-23 8:40am    
OK, so that is what you say in your original question. So you need to talk to whoever provided the sample code to find out what you are doing wrong.
Richard MacCutchan 15-Dec-23 9:06am    
I have had a look at the documentation, and also some of the sample code, but I cannot find anywhere where you can do a simple file read via this device.
Between CreateFile and ReadFile to the driver you have to actually connect driver to NDIS device using DeviceIoControl(Handle, IOCTL_NDISPROT_OPEN_DEVICE....
 
Share this answer
 
Here is an article where the functions CreateFile() and DeviceIOControl() are used to get a pointer to the output buffer that receives the result from NDIS-Device.
https://www.codeproject.com/Articles/24756/How-to-query-miniport-driver-information-802-11-OI
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900