WriteFile() keeps failing

Hi,

I’m trying to send data down from from a user-mode application to my network
miniport driver.

I’m creating the connection like this:
m_hDevice = CreateFile(DEVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);

However, when I use WriteFile() to write data to the driver, it fails with
the following message: “The handle is invalid”.
I’ve checked the handle returned by CreateFile() and it’s valid. I am even
able to call ReadFile() without any problems.

In the driver’s dispatch routine I’ve indicated that I can handle
IRP_MJ_WRITE.

What am I doing wrong?

Thanks,

When you have the file opened for FILE_FLAG_OVERLAPPED, you must pass an
OVERLAPPED structure with your ReadFile and WriteFile calls. You must make
sure that the hEvent member of the OVERLAPPED structure is set to 0 or a
valid handle. Perhaps that is where the “invalid handle” is being passed.

At 07:13 AM 1/23/2006, you wrote:

Hi,

I’m trying to send data down from from a user-mode application to my network
miniport driver.

I’m creating the connection like this:
m_hDevice = CreateFile(DEVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);

However, when I use WriteFile() to write data to the driver, it fails with
the following message: “The handle is invalid”.
I’ve checked the handle returned by CreateFile() and it’s valid. I am even
able to call ReadFile() without any problems.

In the driver’s dispatch routine I’ve indicated that I can handle
IRP_MJ_WRITE.

What am I doing wrong?

Thanks,


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@credence.com
To unsubscribe send a blank email to xxxxx@lists.osr.com

Russ Poffenberger
Credence Systems Corp.
xxxxx@credence.com

Ah, yeah. I was passing an OVERLAPPED structure, however I hadn’t
initialized its members to zero.

Another quick question, should I set the entire structure to zero or just
the hEvent member? (not that it would be much of a speed difference, but
it’s always nice to know.)

Thanks!

“Russell Poffenberger” wrote in message
news:xxxxx@ntdev…
> When you have the file opened for FILE_FLAG_OVERLAPPED, you must pass an
> OVERLAPPED structure with your ReadFile and WriteFile calls. You must make
> sure that the hEvent member of the OVERLAPPED structure is set to 0 or a
> valid handle. Perhaps that is where the “invalid handle” is being passed.
>
> At 07:13 AM 1/23/2006, you wrote:
> >Hi,
> >
> >I’m trying to send data down from from a user-mode application to my
network
> >miniport driver.
> >
> >I’m creating the connection like this:
> >m_hDevice = CreateFile(DEVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0,
NULL,
> >OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
> >
> >However, when I use WriteFile() to write data to the driver, it fails
with
> >the following message: “The handle is invalid”.
> >I’ve checked the handle returned by CreateFile() and it’s valid. I am
even
> >able to call ReadFile() without any problems.
> >
> >In the driver’s dispatch routine I’ve indicated that I can handle
> >IRP_MJ_WRITE.
> >
> >What am I doing wrong?
> >
> >Thanks,
> >
> >
> >
> >—
> >Questions? First check the Kernel Driver FAQ at
> >http://www.osronline.com/article.cfm?id=256
> >
> >You are currently subscribed to ntdev as:
xxxxx@credence.com
> >To unsubscribe send a blank email to xxxxx@lists.osr.com
>
> Russ Poffenberger
> Credence Systems Corp.
> xxxxx@credence.com
>
>
>

There are five members to the structure, the first two are reserved for
internal use and don’t matter. The next two are the offset and offsethigh,
which are only important if your driver uses these to specify a file offset
to beging the file I/O operation. The last is the hEvent which must be 0 or
a valid handle. So, if you don’t use the offsets, then you should be OK to
just init the whole thing to 0.

At 07:42 AM 1/23/2006, =?UNKNOWN?Q?S=F8ren?= Dreijer wrote:

Ah, yeah. I was passing an OVERLAPPED structure, however I hadn’t
initialized its members to zero.

Another quick question, should I set the entire structure to zero or just
the hEvent member? (not that it would be much of a speed difference, but
it’s always nice to know.)

Thanks!

“Russell Poffenberger” wrote in message
>news:xxxxx@ntdev…
> > When you have the file opened for FILE_FLAG_OVERLAPPED, you must pass an
> > OVERLAPPED structure with your ReadFile and WriteFile calls. You must make
> > sure that the hEvent member of the OVERLAPPED structure is set to 0 or a
> > valid handle. Perhaps that is where the “invalid handle” is being passed.
> >
> > At 07:13 AM 1/23/2006, you wrote:
> > >Hi,
> > >
> > >I’m trying to send data down from from a user-mode application to my
>network
> > >miniport driver.
> > >
> > >I’m creating the connection like this:
> > >m_hDevice = CreateFile(DEVICE_NAME, GENERIC_READ | GENERIC_WRITE, 0,
>NULL,
> > >OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
> > >
> > >However, when I use WriteFile() to write data to the driver, it fails
>with
> > >the following message: “The handle is invalid”.
> > >I’ve checked the handle returned by CreateFile() and it’s valid. I am
>even
> > >able to call ReadFile() without any problems.
> > >
> > >In the driver’s dispatch routine I’ve indicated that I can handle
> > >IRP_MJ_WRITE.
> > >
> > >What am I doing wrong?
> > >
> > >Thanks,
> > >
> > >
> > >
> > >—
> > >Questions? First check the Kernel Driver FAQ at
> > >http://www.osronline.com/article.cfm?id=256
> > >
> > >You are currently subscribed to ntdev as:
>xxxxx@credence.com
> > >To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> > Russ Poffenberger
> > Credence Systems Corp.
> > xxxxx@credence.com
> >
> >
> >
>
>
>
>—
>Questions? First check the Kernel Driver FAQ at
>http://www.osronline.com/article.cfm?id=256
>
>You are currently subscribed to ntdev as: xxxxx@credence.com
>To unsubscribe send a blank email to xxxxx@lists.osr.com

Russ Poffenberger
Credence Systems Corp.
xxxxx@credence.com