Kernel mode driver: How do I know who issued a CreateFile() ?

Hi!

I’m writing a small driver, and I have a problem:

I’m holding a buffer inside the driver, and I want multiple
clients to be using it. Every time a client issues a read
operation (ReadFile) - I want to give him the data from were he
stopped-getting
it the last time.

For this purpose, I wrote an interface for a buffer that
holds 1 “writing” pointer, and ‘n’ “reading” pointers - one for each
of the n clients.

The problem: How do I associate a read request with a client?
In other words: How do I know which pointer is associated with
which client?

thanks in advance,

  • Barak

Barak Mandelovich Mercury Interactive ltd.
xxxxx@conduct.com 19 Shabazi st.
Tel: +972 3 539 9286 Yehud, 56100
Fax: +972 3 533 1617 Israel

Barak,

I assume a client is defined by a process. If so use
PsGetCurrentProcess()
to get the process of the caller of the dispatch routine, ie the read, write
or create.
Note: that if someone places a filter driver above your driver, they could
cause
problems with this approach.

Don Burn

----- Original Message -----
From: “Barak Mandelovich”
To: “NT Developers Interest List”
Sent: Sunday, March 19, 2000 11:24 AM
Subject: [ntdev] Kernel mode driver: How do I know who issued a CreateFile()
?

> Hi!
>
> I’m writing a small driver, and I have a problem:
>
> I’m holding a buffer inside the driver, and I want multiple
> clients to be using it. Every time a client issues a read
> operation (ReadFile) - I want to give him the data from were he
> stopped-getting
> it the last time.
>
> For this purpose, I wrote an interface for a buffer that
> holds 1 “writing” pointer, and ‘n’ “reading” pointers - one for each
> of the n clients.
>
> The problem: How do I associate a read request with a client?
> In other words: How do I know which pointer is associated with
> which client?
>
> thanks in advance,
>
> - Barak
>
>
>
> –
> ----------------------------------------------------
> Barak Mandelovich Mercury Interactive ltd.
> xxxxx@conduct.com 19 Shabazi st.
> Tel: +972 3 539 9286 Yehud, 56100
> Fax: +972 3 533 1617 Israel
> ----------------------------------------------------

From: “Barak Mandelovich”
Sent: Sunday, March 19, 2000 11:24 AM

> Hi!
>
> I’m writing a small driver, and I have a problem:
>
> I’m holding a buffer inside the driver, and I want multiple
> clients to be using it. Every time a client issues a read
> operation (ReadFile) - I want to give him the data from were he
> stopped-getting
> it the last time.
>
> For this purpose, I wrote an interface for a buffer that
> holds 1 “writing” pointer, and ‘n’ “reading” pointers - one for each
> of the n clients.
>
> The problem: How do I associate a read request with a client?
> In other words: How do I know which pointer is associated with
> which client?

Generally, each request processed by a driver comes with a FILE_OBJECT
pointed to by the ‘FileObject’ member of your driver’s IO_STACK_LOCATION.
Each FILE_OBJECT, in a sense, represents “one” client of your driver.

During each client’s IRP_MJ_CREATE, you can store a pointer to a per-client
data structure in the associated FILE_OBJECT’s ‘FsContext’ or ‘FsContext2’
fields. Then, when each client calls back (during some other IRP_xxx), you
can retrieve the client data struct from the FILE_OBJECT.

When you process IRP_MJ_CLOSE for a particular client, you can likewise free
the data structure for that client.

In any case, you can now store the state of each client’s “read” and “write”
pointers in the associated data client structure and update it on each read
or write request from that client.

Regards,

Matt Arnold
Professional Music Products
Mark of the Unicorn, Inc.
http://www.motu.com

To obtain my real address, please replace “biteme” with “motu”. My use of a
fake address is an anti-spam tactic. Sorry for the inconvenience.

> The problem: How do I associate a read request with a client?

In other words: How do I know which pointer is associated with
which client?

Use one of the “get current process/thread” functions in IRP_MJ_CREATE
path.

Max