Hello All
I am trying to capture URB for USB devices, I have written some code
for that, Now my goal is to transfer all those URB's to user mode
application for further processing,
I am not getting how to send URB's and IRP's to user mode application,
Could anyone tell me how to send URB to user mode application, user
mode application would access this data for further processing. It
would be great if some one explains me using some code.

-AJay

Re: How to send URB and IRP to user mode application? by Ian

Ian
Fri Sep 08 05:18:06 CDT 2006

On 08/09/2006 07:36, ajay.sonawane@gmail.com wrote:
> Hello All
> I am trying to capture URB for USB devices, I have written some code
> for that, Now my goal is to transfer all those URB's to user mode
> application for further processing,
> I am not getting how to send URB's and IRP's to user mode application,
> Could anyone tell me how to send URB to user mode application, user
> mode application would access this data for further processing. It
> would be great if some one explains me using some code.

Have a look at SnoopyPro: http://sourceforge.net/projects/usbsnoop/

It's a little old (2002), but comes with source code (GPL).

Re: How to send URB and IRP to user mode application? by ajay

ajay
Fri Sep 08 05:34:19 CDT 2006


USBSnoop writes data ( URB) to a log file but we need to send all those
URB's to user mode application where it would get further processed. My
filter driver gets lots of URB's so I need a proper way of sending
data to user mode application. As well as user mode should be capable
of processing the data.

Ian Abbott wrote:
> On 08/09/2006 07:36, ajay.sonawane@gmail.com wrote:
> > Hello All
> > I am trying to capture URB for USB devices, I have written some code
> > for that, Now my goal is to transfer all those URB's to user mode
> > application for further processing,
> > I am not getting how to send URB's and IRP's to user mode application,
> > Could anyone tell me how to send URB to user mode application, user
> > mode application would access this data for further processing. It
> > would be great if some one explains me using some code.
>
> Have a look at SnoopyPro: http://sourceforge.net/projects/usbsnoop/
>
> It's a little old (2002), but comes with source code (GPL).


Re: How to send URB and IRP to user mode application? by Ian

Ian
Fri Sep 08 07:32:55 CDT 2006

On 08/09/2006 11:34, ajay.sonawane@gmail.com wrote:
> USBSnoop writes data ( URB) to a log file but we need to send all those
> URB's to user mode application where it would get further processed. My
> filter driver gets lots of URB's so I need a proper way of sending
> data to user mode application. As well as user mode should be capable
> of processing the data.

I only suggested it as an example to look at. Another example is
libusb-win32. Or you could write a UMDF driver for your device.

Ian.

Re: How to send URB and IRP to user mode application? by soviet_bloke

soviet_bloke
Fri Sep 08 07:54:00 CDT 2006

Hi mate

The easiest thing to do in your case is to use events and shared
memory. Create a couple of synchronization events in the user mode, and
pass their handles, along with the address of exchange buffer that
resides in your application's address space, to your driver. Your
driver can map the buffer to the kernel address space via MDL (or, in
case if buffer does not cross the page boundary, just with
MmGetPhysicalAddress()-MmMapIoSpace() pair), so that it can access
memory in context of any process. Concerning the events, your driver
can get pointers to KEVENTs that correspond to handles with
ObReferenceObjectByHandle(). It is understandable that the above tasks
have to be done in the context of your application in response to IOCTL
that it sends to your driver.

At this point your driver will be able to access access the buffer in
context of any process, plus wait on events - it will write data to the
buffer and wait on event, and application will read/modify buffer and
then set event.

Anton Bassov


Anton Bassov

ajay.sonawane@gmail.com wrote:
> USBSnoop writes data ( URB) to a log file but we need to send all those
> URB's to user mode application where it would get further processed. My
> filter driver gets lots of URB's so I need a proper way of sending
> data to user mode application. As well as user mode should be capable
> of processing the data.
>
> Ian Abbott wrote:
> > On 08/09/2006 07:36, ajay.sonawane@gmail.com wrote:
> > > Hello All
> > > I am trying to capture URB for USB devices, I have written some code
> > > for that, Now my goal is to transfer all those URB's to user mode
> > > application for further processing,
> > > I am not getting how to send URB's and IRP's to user mode application,
> > > Could anyone tell me how to send URB to user mode application, user
> > > mode application would access this data for further processing. It
> > > would be great if some one explains me using some code.
> >
> > Have a look at SnoopyPro: http://sourceforge.net/projects/usbsnoop/
> >
> > It's a little old (2002), but comes with source code (GPL).


Re: How to send URB and IRP to user mode application? by Doron

Doron
Sat Sep 09 00:39:56 CDT 2006

and how is this easier then pending an IOCTL and completing it? To send the
URB you just need to flatten the data structure and make it linear. how you
dothis is up to you.

d

--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.


<soviet_bloke@hotmail.com> wrote in message
news:1157720040.134016.324730@b28g2000cwb.googlegroups.com...
> Hi mate
>
> The easiest thing to do in your case is to use events and shared
> memory. Create a couple of synchronization events in the user mode, and
> pass their handles, along with the address of exchange buffer that
> resides in your application's address space, to your driver. Your
> driver can map the buffer to the kernel address space via MDL (or, in
> case if buffer does not cross the page boundary, just with
> MmGetPhysicalAddress()-MmMapIoSpace() pair), so that it can access
> memory in context of any process. Concerning the events, your driver
> can get pointers to KEVENTs that correspond to handles with
> ObReferenceObjectByHandle(). It is understandable that the above tasks
> have to be done in the context of your application in response to IOCTL
> that it sends to your driver.
>
> At this point your driver will be able to access access the buffer in
> context of any process, plus wait on events - it will write data to the
> buffer and wait on event, and application will read/modify buffer and
> then set event.
>
> Anton Bassov
>
>
> Anton Bassov
>
> ajay.sonawane@gmail.com wrote:
>> USBSnoop writes data ( URB) to a log file but we need to send all those
>> URB's to user mode application where it would get further processed. My
>> filter driver gets lots of URB's so I need a proper way of sending
>> data to user mode application. As well as user mode should be capable
>> of processing the data.
>>
>> Ian Abbott wrote:
>> > On 08/09/2006 07:36, ajay.sonawane@gmail.com wrote:
>> > > Hello All
>> > > I am trying to capture URB for USB devices, I have written some code
>> > > for that, Now my goal is to transfer all those URB's to user mode
>> > > application for further processing,
>> > > I am not getting how to send URB's and IRP's to user mode
>> > > application,
>> > > Could anyone tell me how to send URB to user mode application, user
>> > > mode application would access this data for further processing. It
>> > > would be great if some one explains me using some code.
>> >
>> > Have a look at SnoopyPro: http://sourceforge.net/projects/usbsnoop/
>> >
>> > It's a little old (2002), but comes with source code (GPL).
>



Re: How to send URB and IRP to user mode application? by soviet_bloke

soviet_bloke
Sat Sep 09 18:46:11 CDT 2006

Doron,

> and how is this easier then pending an IOCTL and completing it?

In fact, as long as driver does not depend on application's decisions,
this may be not a bad option either...

Anton Bassov

Doron Holan [MS] wrote:
> and how is this easier then pending an IOCTL and completing it? To send the
> URB you just need to flatten the data structure and make it linear. how you
> dothis is up to you.
>
> d
>
> --
> Please do not send e-mail directly to this alias. this alias is for
> newsgroup purposes only.
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> <soviet_bloke@hotmail.com> wrote in message
> news:1157720040.134016.324730@b28g2000cwb.googlegroups.com...
> > Hi mate
> >
> > The easiest thing to do in your case is to use events and shared
> > memory. Create a couple of synchronization events in the user mode, and
> > pass their handles, along with the address of exchange buffer that
> > resides in your application's address space, to your driver. Your
> > driver can map the buffer to the kernel address space via MDL (or, in
> > case if buffer does not cross the page boundary, just with
> > MmGetPhysicalAddress()-MmMapIoSpace() pair), so that it can access
> > memory in context of any process. Concerning the events, your driver
> > can get pointers to KEVENTs that correspond to handles with
> > ObReferenceObjectByHandle(). It is understandable that the above tasks
> > have to be done in the context of your application in response to IOCTL
> > that it sends to your driver.
> >
> > At this point your driver will be able to access access the buffer in
> > context of any process, plus wait on events - it will write data to the
> > buffer and wait on event, and application will read/modify buffer and
> > then set event.
> >
> > Anton Bassov
> >
> >
> > Anton Bassov
> >
> > ajay.sonawane@gmail.com wrote:
> >> USBSnoop writes data ( URB) to a log file but we need to send all those
> >> URB's to user mode application where it would get further processed. My
> >> filter driver gets lots of URB's so I need a proper way of sending
> >> data to user mode application. As well as user mode should be capable
> >> of processing the data.
> >>
> >> Ian Abbott wrote:
> >> > On 08/09/2006 07:36, ajay.sonawane@gmail.com wrote:
> >> > > Hello All
> >> > > I am trying to capture URB for USB devices, I have written some code
> >> > > for that, Now my goal is to transfer all those URB's to user mode
> >> > > application for further processing,
> >> > > I am not getting how to send URB's and IRP's to user mode
> >> > > application,
> >> > > Could anyone tell me how to send URB to user mode application, user
> >> > > mode application would access this data for further processing. It
> >> > > would be great if some one explains me using some code.
> >> >
> >> > Have a look at SnoopyPro: http://sourceforge.net/projects/usbsnoop/
> >> >
> >> > It's a little old (2002), but comes with source code (GPL).
> >


Re: How to send URB and IRP to user mode application? by Maxim

Maxim
Sat Sep 09 19:44:24 CDT 2006

> In fact, as long as driver does not depend on application's decisions,
> this may be not a bad option either...

Shared memory is bad design. It is not protected from unsynchronized
modifications.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com


Re: How to send URB and IRP to user mode application? by soviet_bloke

soviet_bloke
Sun Sep 10 00:23:10 CDT 2006

Maxim,

> Shared memory is bad design. It is not protected from unsynchronized
> modifications.

This is true. However, sometimes you may need to share memory between
your app and driver (for example, when driver passes data to
application and then waits for the response - asynchronously completing
IRP may work only if driver passes data and goes upon its own business,
i.e. does not depend on app's decision)

Anton Bassov

Maxim S. Shatskih wrote:
> > In fact, as long as driver does not depend on application's decisions,
> > this may be not a bad option either...
>
> Shared memory is bad design. It is not protected from unsynchronized
> modifications.
>
> --
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> maxim@storagecraft.com
> http://www.storagecraft.com


Re: How to send URB and IRP to user mode application? by Doron

Doron
Mon Sep 11 00:59:29 CDT 2006

your solution (shared memory) doesn't solve the basic problem you are
pointing out (the driver depending on the app's decision). You just changed
how you notified the app. The pended IRP is still a better solution,
regardles of the next state that the driver will move into

d

--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.


<soviet_bloke@hotmail.com> wrote in message
news:1157865790.345923.258800@m79g2000cwm.googlegroups.com...
> Maxim,
>
>> Shared memory is bad design. It is not protected from unsynchronized
>> modifications.
>
> This is true. However, sometimes you may need to share memory between
> your app and driver (for example, when driver passes data to
> application and then waits for the response - asynchronously completing
> IRP may work only if driver passes data and goes upon its own business,
> i.e. does not depend on app's decision)
>
> Anton Bassov
>
> Maxim S. Shatskih wrote:
>> > In fact, as long as driver does not depend on application's decisions,
>> > this may be not a bad option either...
>>
>> Shared memory is bad design. It is not protected from unsynchronized
>> modifications.
>>
>> --
>> Maxim Shatskih, Windows DDK MVP
>> StorageCraft Corporation
>> maxim@storagecraft.com
>> http://www.storagecraft.com
>



Re: How to send URB and IRP to user mode application? by ajay

ajay
Mon Sep 11 03:48:10 CDT 2006

Doron,
I am a newbie in driver development, I am not aware of pended IRP.
Could you please elaborate on using pended IRP and it could be helpful
in my case where I need to setup a communication between user mode app
and driver and sending bulk of URB's.
It would be great for me if you explain all these things with some type
of source code, like what code should I write in user mode app and
driver.

-AJay

Doron Holan [MS] wrote:
> your solution (shared memory) doesn't solve the basic problem you are
> pointing out (the driver depending on the app's decision). You just changed
> how you notified the app. The pended IRP is still a better solution,
> regardles of the next state that the driver will move into
>
> d
>
> --
> Please do not send e-mail directly to this alias. this alias is for
> newsgroup purposes only.
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> <soviet_bloke@hotmail.com> wrote in message
> news:1157865790.345923.258800@m79g2000cwm.googlegroups.com...
> > Maxim,
> >
> >> Shared memory is bad design. It is not protected from unsynchronized
> >> modifications.
> >
> > This is true. However, sometimes you may need to share memory between
> > your app and driver (for example, when driver passes data to
> > application and then waits for the response - asynchronously completing
> > IRP may work only if driver passes data and goes upon its own business,
> > i.e. does not depend on app's decision)
> >
> > Anton Bassov
> >
> > Maxim S. Shatskih wrote:
> >> > In fact, as long as driver does not depend on application's decisions,
> >> > this may be not a bad option either...
> >>
> >> Shared memory is bad design. It is not protected from unsynchronized
> >> modifications.
> >>
> >> --
> >> Maxim Shatskih, Windows DDK MVP
> >> StorageCraft Corporation
> >> maxim@storagecraft.com
> >> http://www.storagecraft.com
> >


Re: How to send URB and IRP to user mode application? by Don

Don
Mon Sep 11 05:31:57 CDT 2006

The code is old, but take a look at
http://www.osronline.com/article.cfm?id=94.


--
Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
http://www.windrvr.com
Remove StopSpam from the email to reply



<ajay.sonawane@gmail.com> wrote in message
news:1157964490.790955.147330@e3g2000cwe.googlegroups.com...
> Doron,
> I am a newbie in driver development, I am not aware of pended IRP.
> Could you please elaborate on using pended IRP and it could be helpful
> in my case where I need to setup a communication between user mode app
> and driver and sending bulk of URB's.
> It would be great for me if you explain all these things with some type
> of source code, like what code should I write in user mode app and
> driver.
>
> -AJay
>
> Doron Holan [MS] wrote:
>> your solution (shared memory) doesn't solve the basic problem you are
>> pointing out (the driver depending on the app's decision). You just
>> changed
>> how you notified the app. The pended IRP is still a better solution,
>> regardles of the next state that the driver will move into
>>
>> d
>>
>> --
>> Please do not send e-mail directly to this alias. this alias is for
>> newsgroup purposes only.
>> This posting is provided "AS IS" with no warranties, and confers no
>> rights.
>>
>>
>> <soviet_bloke@hotmail.com> wrote in message
>> news:1157865790.345923.258800@m79g2000cwm.googlegroups.com...
>> > Maxim,
>> >
>> >> Shared memory is bad design. It is not protected from unsynchronized
>> >> modifications.
>> >
>> > This is true. However, sometimes you may need to share memory between
>> > your app and driver (for example, when driver passes data to
>> > application and then waits for the response - asynchronously completing
>> > IRP may work only if driver passes data and goes upon its own business,
>> > i.e. does not depend on app's decision)
>> >
>> > Anton Bassov
>> >
>> > Maxim S. Shatskih wrote:
>> >> > In fact, as long as driver does not depend on application's
>> >> > decisions,
>> >> > this may be not a bad option either...
>> >>
>> >> Shared memory is bad design. It is not protected from unsynchronized
>> >> modifications.
>> >>
>> >> --
>> >> Maxim Shatskih, Windows DDK MVP
>> >> StorageCraft Corporation
>> >> maxim@storagecraft.com
>> >> http://www.storagecraft.com
>> >
>



Re: How to send URB and IRP to user mode application? by Doron

Doron
Mon Sep 11 09:41:12 CDT 2006

and download KMDF, http://www.microsoft.com/whdc/driver/wdf/KMDF_pkg.mspx,
and look at the osrusbfx2 sample.

d

--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.


"Don Burn" <burn@stopspam.acm.org> wrote in message
news:uEUvI2Y1GHA.4976@TK2MSFTNGP02.phx.gbl...
> The code is old, but take a look at
> http://www.osronline.com/article.cfm?id=94.
>
>
> --
> Don Burn (MVP, Windows DDK)
> Windows 2k/XP/2k3 Filesystem and Driver Consulting
> http://www.windrvr.com
> Remove StopSpam from the email to reply
>
>
>
> <ajay.sonawane@gmail.com> wrote in message
> news:1157964490.790955.147330@e3g2000cwe.googlegroups.com...
>> Doron,
>> I am a newbie in driver development, I am not aware of pended IRP.
>> Could you please elaborate on using pended IRP and it could be helpful
>> in my case where I need to setup a communication between user mode app
>> and driver and sending bulk of URB's.
>> It would be great for me if you explain all these things with some type
>> of source code, like what code should I write in user mode app and
>> driver.
>>
>> -AJay
>>
>> Doron Holan [MS] wrote:
>>> your solution (shared memory) doesn't solve the basic problem you are
>>> pointing out (the driver depending on the app's decision). You just
>>> changed
>>> how you notified the app. The pended IRP is still a better solution,
>>> regardles of the next state that the driver will move into
>>>
>>> d
>>>
>>> --
>>> Please do not send e-mail directly to this alias. this alias is for
>>> newsgroup purposes only.
>>> This posting is provided "AS IS" with no warranties, and confers no
>>> rights.
>>>
>>>
>>> <soviet_bloke@hotmail.com> wrote in message
>>> news:1157865790.345923.258800@m79g2000cwm.googlegroups.com...
>>> > Maxim,
>>> >
>>> >> Shared memory is bad design. It is not protected from unsynchronized
>>> >> modifications.
>>> >
>>> > This is true. However, sometimes you may need to share memory between
>>> > your app and driver (for example, when driver passes data to
>>> > application and then waits for the response - asynchronously
>>> > completing
>>> > IRP may work only if driver passes data and goes upon its own
>>> > business,
>>> > i.e. does not depend on app's decision)
>>> >
>>> > Anton Bassov
>>> >
>>> > Maxim S. Shatskih wrote:
>>> >> > In fact, as long as driver does not depend on application's
>>> >> > decisions,
>>> >> > this may be not a bad option either...
>>> >>
>>> >> Shared memory is bad design. It is not protected from unsynchronized
>>> >> modifications.
>>> >>
>>> >> --
>>> >> Maxim Shatskih, Windows DDK MVP
>>> >> StorageCraft Corporation
>>> >> maxim@storagecraft.com
>>> >> http://www.storagecraft.com
>>> >
>>
>
>



Re: How to send URB and IRP to user mode application? by soviet_bloke

soviet_bloke
Mon Sep 11 18:04:03 CDT 2006

Doron,

> your solution (shared memory) doesn't solve the basic problem you are
> pointing out (the driver depending on the app's decision). You just changed
> how you notified the app. The pended IRP is still a better solution,
> regardles of the next state that the driver will move into


I think the best thing to do is to work through the example.

Let's say you want to prevent execution of unauthorized programs and/or
loading of unauthorized DLLs. Your driver captures all attempts to
create an executable section, and ask the controller application
whether it should abort the request (in fact, the application does not
decide anything itself - it just asks the user's permission).These
requests may be made at any time and by different threads
simultaneously. However, there is only one thread that asks the user
whether section creation should be allowed or aborted (this thread runs
in context of your application)


Therefore, the question is following - how can it all be done by
pending IRP, without
any exchange buffer that is shared by app and driver????

Anton Bassov


Doron Holan [MS] wrote:
> your solution (shared memory) doesn't solve the basic problem you are
> pointing out (the driver depending on the app's decision). You just changed
> how you notified the app. The pended IRP is still a better solution,
> regardles of the next state that the driver will move into
>
> d
>
> --
> Please do not send e-mail directly to this alias. this alias is for
> newsgroup purposes only.
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> <soviet_bloke@hotmail.com> wrote in message
> news:1157865790.345923.258800@m79g2000cwm.googlegroups.com...
> > Maxim,
> >
> >> Shared memory is bad design. It is not protected from unsynchronized
> >> modifications.
> >
> > This is true. However, sometimes you may need to share memory between
> > your app and driver (for example, when driver passes data to
> > application and then waits for the response - asynchronously completing
> > IRP may work only if driver passes data and goes upon its own business,
> > i.e. does not depend on app's decision)
> >
> > Anton Bassov
> >
> > Maxim S. Shatskih wrote:
> >> > In fact, as long as driver does not depend on application's decisions,
> >> > this may be not a bad option either...
> >>
> >> Shared memory is bad design. It is not protected from unsynchronized
> >> modifications.
> >>
> >> --
> >> Maxim Shatskih, Windows DDK MVP
> >> StorageCraft Corporation
> >> maxim@storagecraft.com
> >> http://www.storagecraft.com
> >


Re: How to send URB and IRP to user mode application? by Maxim

Maxim
Mon Sep 11 18:18:03 CDT 2006

> Therefore, the question is following - how can it all be done by
> pending IRP, without
> any exchange buffer that is shared by app and driver????

Pending IRP - data portion (request) from driver to app.
Some other IOCTL - data portion (response) from app to driver.

--
Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
maxim@storagecraft.com
http://www.storagecraft.com


Re: How to send URB and IRP to user mode application? by soviet_bloke

soviet_bloke
Tue Sep 12 00:32:12 CDT 2006

Maxim,


If you do it this way, you will need 2 IOCTLs and 3 synchronization
events(all are originally in unsignalled state). When your driver
receives IOCTL1, it signals event1, marks IRP as pending, and returns
STATUS_PENDING. When some thread tries to create executable section, it
waits on event1, then completes IRP and waits on event2. App gets the
data, asks user's permission and sends IOCTL2 with the response. Driver
writes a response into some global variable, signals event2 and waits
on event3. The thread that tries to create a section checks the status
of the variable, signals event3 and then either proceeds with the
original request or aborts it. Once event3 gets signalled, your driver
completes IOCTL2 request, and, at this point, app sends IOCTL1 again,
so that the whole process starts over again.

Seems to be fine and dandy, but consider what happens if app
terminates......

Now compare it to polling a shared buffer(certainly, with
KeDelayExecutionTHread())from your driver in context of a thread that
tries to create executable section. You have to poll it in _TRY block.
If app terminates, the address of a buffer becomes meaningless, so that
exception gets raised if you access it. Therefore,
in such case you break out of the polling loop, and that's it (writing
to the buffer also has to get done in _TRY block - if exception gets
raised, you will never enter the polling loop, in the first place). I
know polling is supposed to be bad design, due to performance reasons.
However, in our case it just does not matter - we have to wait for the
HUMAN(!!!) response, so that we measure time in terms of seconds

In other words, everything depends on the situation - sometimes even
"improper" design can have its own advantages

Anton Bassov





Maxim S. Shatskih wrote:
> > Therefore, the question is following - how can it all be done by
> > pending IRP, without
> > any exchange buffer that is shared by app and driver????
>
> Pending IRP - data portion (request) from driver to app.
> Some other IOCTL - data portion (response) from app to driver.
>
> --
> Maxim Shatskih, Windows DDK MVP
> StorageCraft Corporation
> maxim@storagecraft.com
> http://www.storagecraft.com


Re: How to send URB and IRP to user mode application? by Doron

Doron
Tue Sep 12 00:39:20 CDT 2006

globals? no, you can use pseudo handles.
many events? no, you can reuse the same event for the same sequence for the
same query since the 2 IOCTLs do not necessary need to be pended at the same
time. you can also use an IOCP to maximum CPU usage.

tightly spinning on a piece of memory is tremendously inefficient. and it
doesn't scale at all while the IRP solution easily scales to many
applications.

d

--
Please do not send e-mail directly to this alias. this alias is for
newsgroup purposes only.
This posting is provided "AS IS" with no warranties, and confers no rights.


<soviet_bloke@hotmail.com> wrote in message
news:1158039132.682453.121050@i3g2000cwc.googlegroups.com...
> Maxim,
>
>
> If you do it this way, you will need 2 IOCTLs and 3 synchronization
> events(all are originally in unsignalled state). When your driver
> receives IOCTL1, it signals event1, marks IRP as pending, and returns
> STATUS_PENDING. When some thread tries to create executable section, it
> waits on event1, then completes IRP and waits on event2. App gets the
> data, asks user's permission and sends IOCTL2 with the response. Driver
> writes a response into some global variable, signals event2 and waits
> on event3. The thread that tries to create a section checks the status
> of the variable, signals event3 and then either proceeds with the
> original request or aborts it. Once event3 gets signalled, your driver
> completes IOCTL2 request, and, at this point, app sends IOCTL1 again,
> so that the whole process starts over again.
>
> Seems to be fine and dandy, but consider what happens if app
> terminates......
>
> Now compare it to polling a shared buffer(certainly, with
> KeDelayExecutionTHread())from your driver in context of a thread that
> tries to create executable section. You have to poll it in _TRY block.
> If app terminates, the address of a buffer becomes meaningless, so that
> exception gets raised if you access it. Therefore,
> in such case you break out of the polling loop, and that's it (writing
> to the buffer also has to get done in _TRY block - if exception gets
> raised, you will never enter the polling loop, in the first place). I
> know polling is supposed to be bad design, due to performance reasons.
> However, in our case it just does not matter - we have to wait for the
> HUMAN(!!!) response, so that we measure time in terms of seconds
>
> In other words, everything depends on the situation - sometimes even
> "improper" design can have its own advantages
>
> Anton Bassov
>
>
>
>
>
> Maxim S. Shatskih wrote:
>> > Therefore, the question is following - how can it all be done by
>> > pending IRP, without
>> > any exchange buffer that is shared by app and driver????
>>
>> Pending IRP - data portion (request) from driver to app.
>> Some other IOCTL - data portion (response) from app to driver.
>>
>> --
>> Maxim Shatskih, Windows DDK MVP
>> StorageCraft Corporation
>> maxim@storagecraft.com
>> http://www.storagecraft.com
>



Re: How to send URB and IRP to user mode application? by soviet_bloke

soviet_bloke
Tue Sep 12 02:08:27 CDT 2006

Doron,


> many events? no, you can reuse the same event for the same sequence for the
> same query since the 2 IOCTLs do not necessary need to be pended at the same
> time. you can also use an IOCP to maximum CPU usage.


You cannot do all that with a single event. First of all, there cannot
be more than one outstanding IRP for IOCTL1 (because there is only one
thread that deals with the user and sends IOCTLs), but multiple threads
may be trying to create a section. Therefore, you need event1 to make
sure that only one of them may complete IRP. After some thread has
completed it, it has to wait for the reply, while all other threads
keep on waiting one event1 . It cannot wait on event1, can it????

In fact, it does not really matter how many events are there - the only
thing that matters is that it the event may get signalled only in
context of a thread that is owned by controller application, but
threads that have nothing to do with our application wait on it. What
happens if our application gets terminated abnormally????? The answer
is obvious - from now on not a single process can get launched, so that
we cannot even restart our app . The only way out is to reboot the
machine

> tightly spinning on a piece of memory is tremendously inefficient.

Fully agree. However, don't forget that we deal with the HUMAN(!!!)
response, so that we measure time in terms of SECONDS here. Therefore,
we don't need to poll our buffer that tightly - say, twice a second is
more than enough

>and it
> doesn't scale at all while the IRP solution easily scales to many
> applications.

Well, in fact we have to deal only with controller here......

Anton Bassov


Doron Holan [MS] wrote:
> globals? no, you can use pseudo handles.
> many events? no, you can reuse the same event for the same sequence for the
> same query since the 2 IOCTLs do not necessary need to be pended at the same
> time. you can also use an IOCP to maximum CPU usage.
>
> tightly spinning on a piece of memory is tremendously inefficient. and it
> doesn't scale at all while the IRP solution easily scales to many
> applications.
>
> d
>
> --
> Please do not send e-mail directly to this alias. this alias is for
> newsgroup purposes only.
> This posting is provided "AS IS" with no warranties, and confers no rights.
>
>
> <soviet_bloke@hotmail.com> wrote in message
> news:1158039132.682453.121050@i3g2000cwc.googlegroups.com...
> > Maxim,
> >
> >
> > If you do it this way, you will need 2 IOCTLs and 3 synchronization
> > events(all are originally in unsignalled state). When your driver
> > receives IOCTL1, it signals event1, marks IRP as pending, and returns
> > STATUS_PENDING. When some thread tries to create executable section, it
> > waits on event1, then completes IRP and waits on event2. App gets the
> > data, asks user's permission and sends IOCTL2 with the response. Driver
> > writes a response into some global variable, signals event2 and waits
> > on event3. The thread that tries to create a section checks the status
> > of the variable, signals event3 and then either proceeds with the
> > original request or aborts it. Once event3 gets signalled, your driver
> > completes IOCTL2 request, and, at this point, app sends IOCTL1 again,
> > so that the whole process starts over again.
> >
> > Seems to be fine and dandy, but consider what happens if app
> > terminates......
> >
> > Now compare it to polling a shared buffer(certainly, with
> > KeDelayExecutionTHread())from your driver in context of a thread that
> > tries to create executable section. You have to poll it in _TRY block.
> > If app terminates, the address of a buffer becomes meaningless, so that
> > exception gets raised if you access it. Therefore,
> > in such case you break out of the polling loop, and that's it (writing
> > to the buffer also has to get done in _TRY block - if exception gets
> > raised, you will never enter the polling loop, in the first place). I
> > know polling is supposed to be bad design, due to performance reasons.
> > However, in our case it just does not matter - we have to wait for the
> > HUMAN(!!!) response, so that we measure time in terms of seconds
> >
> > In other words, everything depends on the situation - sometimes even
> > "improper" design can have its own advantages
> >
> > Anton Bassov
> >
> >
> >
> >
> >
> > Maxim S. Shatskih wrote:
> >> > Therefore, the question is following - how can it all be done by
> >> > pending IRP, without
> >> > any exchange buffer that is shared by app and driver????
> >>
> >> Pending IRP - data portion (request) from driver to app.
> >> Some other IOCTL - data portion (response) from app to driver.
> >>
> >> --
> >> Maxim Shatskih, Windows DDK MVP
> >> StorageCraft Corporation
> >> maxim@storagecraft.com
> >> http://www.storagecraft.com
> >


Re: How to send URB and IRP to user mode application? by ajay

ajay
Tue Sep 12 05:08:54 CDT 2006

I am posting some code from USBSnoop. I need to send URB from
MyDispatchInternalIOCTL to user mode application. User mode app will
receieve all these URB in one worker therad. I am not getting how
should I send all these URB to user mode applications. You can see
MyDispatchInternalIOCTL and let me know which mechanism shoudl I use to
send the URB. Lot of discussion is happening here but it seems that my
problem left undiscussed. Could anyone explaine how to send URB to user
mode application and how user mode app will receive the data. It woul
be really helpful from me if someone explain me with help of example
and some source code.



NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT
pdo)
{
NTSTATUS status;
PDEVICE_OBJECT fido, fdo;
PDEVICE_EXTENSION pdx;
PDRIVER_OBJECT d;

status = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION),NULL,
FILE_DEVICE_UNKNOWN, 0, FALSE, &fido);
if (!NT_SUCCESS(status))
{
// can't create device object
LogPrintf("UsbSnoop - IoCreateDevice failed - %x\n", status);
return status;
}

// can't create device object
// Benoit PAPILLAULT 13/07/2001
// fido->DeviceExtension is a user define structure whose size is
passed
// to IoCreateDevice(). We can store whatever we want inside.

pdx = (PDEVICE_EXTENSION) fido->DeviceExtension;

// From this point forward, any error will have side effects that need
to
// be cleaned up. Using a try-finally block allows us to modify the
program
// easily without losing track of the side effects.

__try
{
IoInitializeRemoveLock(&pdx->RemoveLock, 0, 0, 255);
pdx->DeviceObject = fido;
pdx->Pdo = pdo;


fdo = IoAttachDeviceToDeviceStack(fido, pdo);

pdx->LowerDeviceObject = fdo;
fido->Flags |= fdo->Flags & (DO_DIRECT_IO | DO_BUFFERED_IO |
DO_POWER_PAGABLE | DO_POWER_INRUSH);
fido->DeviceType = fdo->DeviceType;
fido->Characteristics = fdo->Characteristics;
fido->AlignmentRequirement = fdo->AlignmentRequirement;

// Clear the "initializing" flag so that we can get IRPs

fido->Flags &= ~DO_DEVICE_INITIALIZING;

// we make a copy of fdo->DriverObject
d =
(PDRIVER_OBJECT)ExAllocatePool(NonPagedPool,sizeof(DRIVER_OBJECT));
if (d != NULL)
{
*d = *fdo->DriverObject;

// we make some changes to this copy
d->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] =
MyDispatchInternalIOCTL;
d->MajorFunction[IRP_MJ_PNP] = MyDispatchPnp;

// here is the trick : we save the original DriverObject
// and next, it points to our modified copy
pdx->OriginalDriverObject = fdo->DriverObject;
fdo->DriverObject = d;
}
else
}
__finally
{
if (!NT_SUCCESS(status))
{
IoDeleteDevice(fido);
}
}

return status;
}




NTSTATUS MyDispatchInternalIOCTL(IN PDEVICE_OBJECT fdo, IN PIRP Irp)
{
// we want to recover OriginalDriverObject which is stored in fido,
// so we need to recover fido first, by the AttachedDevice field from
FDO.

PDEVICE_OBJECT fido = fdo->AttachedDevice;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fido->DeviceExtension;

// try to print the URB

PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
ULONG dwControlCode = stack->Parameters.DeviceIoControl.IoControlCode;
if (dwControlCode == IOCTL_INTERNAL_USB_SUBMIT_URB)
{
struct Buffer b;
ULONG uSequenceNumber =
InterlockedIncrement((PLONG)&pdx->uSequenceNumber);

BufferInit(&b);

BufferPrintf(&b," >>> URB %d going down >>> \n", uSequenceNumber);

// we cannot call ZwWriteFile() cause it must be running at
// IRQL_PASSIVE_LEVEL (0) and most of the time,
MyDispatchInternalIOCTL
// is running at IRQL_DISPATCH_LEVEL (2).
// LogPrintf(" KeGetCurrentIrql() = %d\n",KeGetCurrentIrql()));

// here we get URB
PURB pUrb = (PURB) stack->Parameters.Others.Argument1;

// inspired from the macro code of IoSetCompletionRoutine
// it just makes a big BSOD

// ok. in fact, it worked, but that particular case,
// the first parameter of the IoCompletionRoutine is NULL instead of
being fido !!!

// normally, there should be a call to
IoCopyCurrentIrpStackLocationToNext()
// which might not be there. So, we are surely replacing other
callbacks.

// first, we saved every information we'll modify later

PCONTEXT Context =
(PCONTEXT)ExAllocatePool(NonPagedPool,sizeof(CONTEXT));
if (Context != NULL)
{
Context->CompletionRoutine = stack->CompletionRoutine;
Context->Context = stack->Context;
Context->Control = stack->Control;
Context->pUrb = pUrb;
Context->uSequenceNumber = uSequenceNumber;
Context->Stack = stack;

stack->CompletionRoutine = MyInternalIOCTLCompletion;
stack->Context = Context;
stack->Control = SL_INVOKE_ON_SUCCESS | SL_INVOKE_ON_ERROR |
SL_INVOKE_ON_CANCEL;

}
else
LogPrintf(" ExAllocatePool failed! Can't redirect
CompletionRoutine\n");

LogBuffer(&b);
BufferDone(&b);
}

return
pdx->OriginalDriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL](fdo,Irp);
}

-AJay

soviet_bloke@hotmail.com wrote:
> Doron,
>
>
> > many events? no, you can reuse the same event for the same sequence for the
> > same query since the 2 IOCTLs do not necessary need to be pended at the same
> > time. you can also use an IOCP to maximum CPU usage.
>
>
> You cannot do all that with a single event. First of all, there cannot
> be more than one outstanding IRP for IOCTL1 (because there is only one
> thread that deals with the user and sends IOCTLs), but multiple threads
> may be trying to create a section. Therefore, you need event1 to make
> sure that only one of them may complete IRP. After some thread has
> completed it, it has to wait for the reply, while all other threads
> keep on waiting one event1 . It cannot wait on event1, can it????
>
> In fact, it does not really matter how many events are there - the only
> thing that matters is that it the event may get signalled only in
> context of a thread that is owned by controller application, but
> threads that have nothing to do with our application wait on it. What
> happens if our application gets terminated abnormally????? The answer
> is obvious - from now on not a single process can get launched, so that
> we cannot even restart our app . The only way out is to reboot the
> machine
>
> > tightly spinning on a piece of memory is tremendously inefficient.
>
> Fully agree. However, don't forget that we deal with the HUMAN(!!!)
> response, so that we measure time in terms of SECONDS here. Therefore,
> we don't need to poll our buffer that tightly - say, twice a second is
> more than enough
>
> >and it
> > doesn't scale at all while the IRP solution easily scales to many
> > applications.
>
> Well, in fact we have to deal only with controller here......
>
> Anton Bassov
>
>
> Doron Holan [MS] wrote:
> > globals? no, you can use pseudo handles.
> > many events? no, you can reuse the same event for the same sequence for the
> > same query since the 2 IOCTLs do not necessary need to be pended at the same
> > time. you can also use an IOCP to maximum CPU usage.
> >
> > tightly spinning on a piece of memory is tremendously inefficient. and it
> > doesn't scale at all while the IRP solution easily scales to many
> > applications.
> >
> > d
> >
> > --
> > Please do not send e-mail directly to this alias. this alias is for
> > newsgroup purposes only.
> > This posting is provided "AS IS" with no warranties, and confers no rights.
> >
> >
> > <soviet_bloke@hotmail.com> wrote in message
> > news:1158039132.682453.121050@i3g2000cwc.googlegroups.com...
> > > Maxim,
> > >
> > >
> > > If you do it this way, you will need 2 IOCTLs and 3 synchronization
> > > events(all are originally in unsignalled state). When your driver
> > > receives IOCTL1, it signals event1, marks IRP as pending, and returns
> > > STATUS_PENDING. When some thread tries to create executable section, it
> > > waits on event1, then completes IRP and waits on event2. App gets the
> > > data, asks user's permission and sends IOCTL2 with the response. Driver
> > > writes a response into some global variable, signals event2 and waits
> > > on event3. The thread that tries to create a section checks the status
> > > of the variable, signals event3 and then either proceeds with the
> > > original request or aborts it. Once event3 gets signalled, your driver
> > > completes IOCTL2 request, and, at this point, app sends IOCTL1 again,
> > > so that the whole process starts over again.
> > >
> > > Seems to be fine and dandy, but consider what happens if app
> > > terminates......
> > >
> > > Now compare it to polling a shared buffer(certainly, with
> > > KeDelayExecutionTHread())from your driver in context of a thread that
> > > tries to create executable section. You have to poll it in _TRY block.
> > > If app terminates, the address of a buffer becomes meaningless, so that
> > > exception gets raised if you access it. Therefore,
> > > in such case you break out of the polling loop, and that's it (writing
> > > to the buffer also has to get done in _TRY block - if exception gets
> > > raised, you will never enter the polling loop, in the first place). I
> > > know polling is supposed to be bad design, due to performance reasons.
> > > However, in our case it just does not matter - we have to wait for the
> > > HUMAN(!!!) response, so that we measure time in terms of seconds
> > >
> > > In other words, everything depends on the situation - sometimes even
> > > "improper" design can have its own advantages
> > >
> > > Anton Bassov
> > >
> > >
> > >
> > >
> > >
> > > Maxim S. Shatskih wrote:
> > >> > Therefore, the question is following - how can it all be done by
> > >> > pending IRP, without
> > >> > any exchange buffer that is shared by app and driver????
> > >>
> > >> Pending IRP - data portion (request) from driver to app.
> > >> Some other IOCTL - data portion (response) from app to driver.
> > >>
> > >> --
> > >> Maxim Shatskih, Windows DDK MVP
> > >> StorageCraft Corporation
> > >> maxim@storagecraft.com
> > >> http://www.storagecraft.com
> > >


Re: How to send URB and IRP to user mode application? by ajay

ajay
Tue Sep 12 05:11:23 CDT 2006

Hello,
Let me again elaborate on my problem. for that I am posting some code
from USBSnoop. I need to send URB from MyDispatchInternalIOCTL to user
mode application. User mode app will receieve all these URB in one
worker therad. I am not getting how should I send all these URB to user
mode applications. You can see MyDispatchInternalIOCTL and let me know
which mechanism shoudl I use to send the URB. Lot of discussion is
happening here but it seems that my problem left undiscussed. Could
anyone explaine how to send URB to user mode application and how user
mode app will receive the data. It woul be really helpful from me if
someone explain me with help of example and some source code.


NTSTATUS AddDevice(IN PDRIVER_OBJECT DriverObject, IN PDEVICE_OBJECT
pdo)
{
NTSTATUS status;
PDEVICE_OBJECT fido, fdo;
PDEVICE_EXTENSION pdx;
PDRIVER_OBJECT d;

status = IoCreateDevice(DriverObject, sizeof(DEVICE_EXTENSION),NULL,
FILE_DEVICE_UNKNOWN, 0, FALSE, &fido);
if (!NT_SUCCESS(status))
{
// can't create device object
LogPrintf("UsbSnoop - IoCreateDevice failed - %x\n", status);
return status;
}

// can't create device object
// Benoit PAPILLAULT 13/07/2001
// fido->DeviceExtension is a user define structure whose size is
passed
// to IoCreateDevice(). We can store whatever we want inside.

pdx = (PDEVICE_EXTENSION) fido->DeviceExtension;

// From this point forward, any error will have side effects that need
to
// be cleaned up. Using a try-finally block allows us to modify the
program
// easily without losing track of the side effects.

__try
{
IoInitializeRemoveLock(&pdx->RemoveLock, 0, 0, 255);
pdx->DeviceObject = fido;
pdx->Pdo = pdo;


fdo = IoAttachDeviceToDeviceStack(fido, pdo);

pdx->LowerDeviceObject = fdo;
fido->Flags |= fdo->Flags & (DO_DIRECT_IO | DO_BUFFERED_IO |
DO_POWER_PAGABLE | DO_POWER_INRUSH);
fido->DeviceType = fdo->DeviceType;
fido->Characteristics = fdo->Characteristics;
fido->AlignmentRequirement = fdo->AlignmentRequirement;

// Clear the "initializing" flag so that we can get IRPs

fido->Flags &= ~DO_DEVICE_INITIALIZING;

// we make a copy of fdo->DriverObject
d =
(PDRIVER_OBJECT)ExAllocatePool(NonPagedPool,sizeof(DRIVER_OBJECT));
if (d != NULL)
{
*d = *fdo->DriverObject;

// we make some changes to this copy
d->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL] =
MyDispatchInternalIOCTL;
d->MajorFunction[IRP_MJ_PNP] = MyDispatchPnp;

// here is the trick : we save the original DriverObject
// and next, it points to our modified copy
pdx->OriginalDriverObject = fdo->DriverObject;
fdo->DriverObject = d;
}
else
}
__finally
{
if (!NT_SUCCESS(status))
{
IoDeleteDevice(fido);
}
}

return status;
}




NTSTATUS MyDispatchInternalIOCTL(IN PDEVICE_OBJECT fdo, IN PIRP Irp)
{
// we want to recover OriginalDriverObject which is stored in fido,
// so we need to recover fido first, by the AttachedDevice field from
FDO.

PDEVICE_OBJECT fido = fdo->AttachedDevice;
PDEVICE_EXTENSION pdx = (PDEVICE_EXTENSION) fido->DeviceExtension;

// try to print the URB

PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(Irp);
ULONG dwControlCode = stack->Parameters.DeviceIoControl.IoControlCode;
if (dwControlCode == IOCTL_INTERNAL_USB_SUBMIT_URB)
{
struct Buffer b;
ULONG uSequenceNumber =
InterlockedIncrement((PLONG)&pdx->uSequenceNumber);

BufferInit(&b);

BufferPrintf(&b," >>> URB %d going down >>> \n", uSequenceNumber);

// we cannot call ZwWriteFile() cause it must be running at
// IRQL_PASSIVE_LEVEL (0) and most of the time,
MyDispatchInternalIOCTL
// is running at IRQL_DISPATCH_LEVEL (2).
// LogPrintf(" KeGetCurrentIrql() = %d\n",KeGetCurrentIrql()));

// here we get URB
PURB pUrb = (PURB) stack->Parameters.Others.Argument1;

// inspired from the macro code of IoSetCompletionRoutine
// it just makes a big BSOD

// ok. in fact, it worked, but that particular case,
// the first parameter of the IoCompletionRoutine is NULL instead of
being fido !!!

// normally, there should be a call to
IoCopyCurrentIrpStackLocationToNext()
// which might not be there. So, we are surely replacing other
callbacks.

// first, we saved every information we'll modify later

PCONTEXT Context =
(PCONTEXT)ExAllocatePool(NonPagedPool,sizeof(CONTEXT));
if (Context != NULL)
{
Context->CompletionRoutine = stack->CompletionRoutine;
Context->Context = stack->Context;
Context->Control = stack->Control;
Context->pUrb = pUrb;
Context->uSequenceNumber = uSequenceNumber;
Context->Stack = stack;

stack->CompletionRoutine = MyInternalIOCTLCompletion;
stack->Context = Context;
stack->Control = SL_INVOKE_ON_SUCCESS | SL_INVOKE_ON_ERROR |
SL_INVOKE_ON_CANCEL;

}
else
LogPrintf(" ExAllocatePool failed! Can't redirect
CompletionRoutine\n");

LogBuffer(&b);
BufferDone(&b);
}

return
pdx->OriginalDriverObject->MajorFunction[IRP_MJ_INTERNAL_DEVICE_CONTROL](fdo,Irp);
}

-AJay
soviet_bloke@hotmail.com wrote:
> Doron,
>
>
> > many events? no, you can reuse the same event for the same sequence for the
> > same query since the 2 IOCTLs do not necessary need to be pended at the same
> > time. you can also use an IOCP to maximum CPU usage.
>
>
> You cannot do all that with a single event. First of all, there cannot
> be more than one outstanding IRP for IOCTL1 (becaus