a quick question about DeviceIoControl...

Hi,

I have driver which i am communicating with from user mode using DeviceIoControl this driver is generating data at relativity slow pace so i want the thread thats communicating with the driver to wait until the driver has completed the request.

by wait i mean using waitforsingleobject or similar functions so the thread doesn’t waste CPU time

(basically the thread is in an endless loop constantly communicating with the driver)

any ideas on how i can achieve this?

(sorry for the noob question)

Thanks,
Bill

At 14:51 07/04/2011, xxxxx@yahoo.com wrote:

Hi,

I have driver which i am communicating with from user mode using
DeviceIoControl this driver is generating data at relativity slow
pace so i want the thread thats communicating with the driver to
wait until the driver has completed the request.

by wait i mean using waitforsingleobject or similar functions so the
thread doesn’t waste CPU time

(basically the thread is in an endless loop constantly communicating
with the driver)

any ideas on how i can achieve this?

(sorry for the noob question)

Thanks,
Bill

Use overlapped I/O with the hEvent field set, then you can wait on the event.

Mark

Ummmm… Doesn’t DeviceIoControl DEFAULT to synchronous processing from Win32? So, you just open the handle and issue the call?

P

Dear Bill J. West,

Like suggested by Mark, you can use KEVENT for wait until event is cleared for enable any process.

For example :

PsCreateSystemThread (
handleThread,
THREAD_ALL_ACCESS,
NULL,
NULL,
fcTrackingThread,
&Context // DEVICE_CONTEXT
);

fcTrackingThread(PDEVICE_CONTEXT devContext)
{

PVOID EventsToWait[5];
UCHAR nbEventToWait = 0;

EventsToWait[i++] = devContext->eventStop;
EventsToWait[i++] = devContext->eventStart;
EventsToWait[i++] = devContext->eventResume;
EventsToWait[i++] = devContext->eventSurpriseRemoval;

nbEventToWait = i;

While (!terminate)
{
NTSTATUS ntStatus = STATUS_SUCCESS;

ntStatus = KeWaitForMultipleObject (
nbEventToWait, // = 4
…);

switch (ntStatus)
{
case 0 :
evState1 = KeResetEvent ( devContext->eventStop );

if(evState1)
{
// Process any treatment when eventStop is “cleared”
KeSetEvent( &eventThreadKilled, IO_NO_INCREMENT, FALSE);
terminate = TRUE;
}
} // Switch (ntStatus)

} // While (terminate)

PsTerminateSystemThread ( STATUS_SUCCESS );

}

Hope to help you,

Best regards,

Moulefrite

Thank you all for your replays , @ peter i can not do that since the driver is queuing the IRPS

i am still confused a bit on this point “do i need to change the driver in order to do this?”

Can’t i do something like this in user mode or do i need to edit the driver code?

OVERLAPPED iostruct;
iostruct.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);

DWORD result = DeviceIoControl(Handetodevice,
IOCTL_CODE,
NULL,NULL,Buffer,
BufferLength,&BytesWritten,&iostruct);

WaitForSingleObject(iostruct.hEvent,INFINITE);

Thank you !!

Ignore Kamels’ reply, sorry that was by for more complexity than is needed, and pay particular attention to Peters. That’s how DeviceIoControl works. You don’t even need to open it for OVERLAPPED. Simply send the device your IOCTL and the app will block until the driver has “something” that fits the IRP you sent it. You need the complexity of Kamels reply and OVERLAPPED IO when you DON’T want the app to block.

Gary G. Little

----- Original Message -----
From: xxxxx@yahoo.com
To: “Windows System Software Devs Interest List”
Sent: Thursday, April 7, 2011 8:51:19 AM
Subject: [ntdev] a quick question about DeviceIoControl…

Hi,

I have driver which i am communicating with from user mode using DeviceIoControl this driver is generating data at relativity slow pace so i want the thread thats communicating with the driver to wait until the driver has completed the request.

by wait i mean using waitforsingleobject or similar functions so the thread doesn’t waste CPU time

(basically the thread is in an endless loop constantly communicating with the driver)

any ideas on how i can achieve this?

(sorry for the noob question)

Thanks,
Bill


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Doesn’t matter what the driver is doing. If you open a non overlapped handle, the io manager makes all io synchronous . It also serilaizes them so your driver will only see one io request at a time.

d

debt from my phone

-----Original Message-----
From: xxxxx@yahoo.com
Sent: Thursday, April 07, 2011 7:31 AM
To: Windows System Software Devs Interest List
Subject: RE:[ntdev] a quick question about DeviceIoControl…

Thank you all for your replays , @ peter i can not do that since the driver is queuing the IRPS

i am still confused a bit on this point “do i need to change the driver in order to do this?”

Can’t i do something like this in user mode or do i need to edit the driver code?

OVERLAPPED iostruct;
iostruct.hEvent = CreateEvent(NULL,TRUE,FALSE,NULL);

DWORD result = DeviceIoControl(Handetodevice,
IOCTL_CODE,
NULL,NULL,Buffer,
BufferLength,&BytesWritten,&iostruct);

WaitForSingleObject(iostruct.hEvent,INFINITE);

Thank you !!


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Sorry Bill and Gary, my level in english is very bad, and I have a lot of problem for “really” understand all qestion posted here.
But If I have posted a shit answer I beg you to forgive me.

Best regards,

Moulefrite

So you want to issue multiple requests overlapped and wait for them to
complete.

Use a completion port. Otherwise you will have to have an event per
request, WaitForMultipleObjects(), etc. and it gets rather ugly and
inefficient fast.

Good Luck,
Dave Cattley

In which case you would need to then pay ATTENTION to Kamel’s post … :slight_smile:

Gary G. Little

----- Original Message -----
From: “David R. Cattley”
To: “Windows System Software Devs Interest List”
Sent: Thursday, April 7, 2011 9:41:16 AM
Subject: RE: [ntdev] a quick question about DeviceIoControl…



So you want to issue multiple requests overlapped and wait for them to
complete.

Use a completion port. Otherwise you will have to have an event per
request, WaitForMultipleObjects(), etc. and it gets rather ugly and
inefficient fast.

Good Luck,
Dave Cattley


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

Thanks for your response !!

last question when i do DeviceIoControl call to the driver (with out an overlapped handle) and lets the driver takes 10 minutes to complete the request will the thread that did the call wait for 10 minutes ? (i.e it wont get scheduled as such it wont waste CPU cycles -> NOTE: this my is aim to not waste CPU cycles by froce the thread wait until the driver completed the request)

No it won’t be scheduled if it is waiting on an event. What is a
concern is that you are that concerned about CPU cycles while asking
basic questions. I’ve seen way too many crappy implementations that
resulted from the basis that we must conserve every cycle from the
beginning of development.

What should be done is implement a clean approach and test it. If the
speed is good enough you are done, otherwise profile the solution and
start making changes where they do the most good. People who try to
code from day one for the fast performance rarely get there.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

xxxxx@yahoo.com” wrote in message
news:xxxxx@ntdev:

> Thanks for your response !!
>
> last question when i do DeviceIoControl call to the driver (with out an overlapped handle) and lets the driver takes 10 minutes to complete the request will the thread that did the call wait for 10 minutes ? (i.e it wont get scheduled as such it wont waste CPU cycles -> NOTE: this my is aim to not waste CPU cycles by froce the thread wait until the driver completed the request)

I solved the problem thanks to your help! :slight_smile:

Thank you.

Yay! Thanks for letting us know.

It’s nice to actually SOLVE a problem every now and again :slight_smile:

Peter
OSR