from application to driver using Inverted call method

Dear all,

I have used the inverted call method in driver, i have configured the EvtIocontrol with parallel dispatch, then created the manual queue.

From application im calling the fun() which uses the overlapped i/o in async ,send to driver through ioctl to driver where it uses the wdfrequestretriveinputbuffer which has information and writing into register then im pending the request , that time ISR will be triggered and queue to dpc routine where im retreiveing the requet until no entries and fill the output buffer and completing the request .

Im calling the fun() infinite until the keyboard stokes like :
Thread()
{
completionPortHandle = CreateIoCompletionPort(hDevice,
NULL,
1,
0);

if(completionPortHandle == NULL) {

code = GetLastError();

printf(“CreateIoCompletionPort failed with error 0x%x\n”, code);

}
hThread = CreateThread(
NULL, // Default thread security descriptor
0, // Default stack size
CompletionPortThread, // Start routine
completionPortHandle, // Start routine parameter
0, // Run immediately
NULL // Thread ID
);

if (hThread == NULL)
{
code = GetLastError();

printf(“CreateThread failed with error 0x%x\n”, code);

}
return;
}

int fun(buffer data)
{
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(DeviceIoControl(hDevice,
IOCTL_TX,
buffer data,
sizeof(buffer data),
&pstate,
sizeof(SWITCH_STATE),
&numread,
&hKevent_0))
{
res =0;
}
else{
res = GetLastError();
//return numread;
}
if(res == ERROR_IO_PENDING)
{
if(pstate.state == TRUE)
{
if(GetOverlappedResult(hDevice,&hKevent_0,&numread,TRUE))
{
res = 0;
ResetEvent(hKevent_0.hEvent);
return numread;
}
}
else{
res = WaitForSingleObject(hKevent_0.hEvent,500);
if(res == WAIT_OBJECT_0)
{
if(intr_flag_0==0)
return numread;
}
ResetEvent(hKevent_0.hEvent);
return numread;
}

}

main()
{

if(tx mode)
{
Thread();
while(!kbhit)
{
int ret = fun();

if(ret>0)
{
writing into file for log
}
}
else{ // RX mode
RXfun() read the data from driver throug ioctl and save to log
}

Im running the console application in two terminal for rx/tx mode and compare the log ( tx reach very high but rx log is small)

Driver side uses the inverted call method.

Pls how i can improve the performane of driver/console application

}

xxxxx@gmail.com wrote:

Im calling the fun() infinite until the keyboard stokes like :
Thread()
{
completionPortHandle = CreateIoCompletionPort(hDevice,
NULL,
1,
0);

You’re not using the completion port in this code, so I’m not clear why
it is here. Also, if the completion port creation fails, you just flow
on and continue your processing as if it had succeeded, passing a bad
handle to your thread.

int fun(buffer data)
{
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

This serves no purpose. Your OVERLAPPED object (hKevent_0) has its own
event. Also, it looks like hEvent is a global here. That means every
time you run this function, you are overwriting the previous handle.
You just keep creating new ones until you run out of resources.

if(res == ERROR_IO_PENDING)
{
if(pstate.state == TRUE)

I can’t tell what you’re trying to do here, but this can’t be right. If
“pstate” is an output from the ioctl, then you can’t touch the structure
until the I/O is complete. If the result was ERROR_IO_PENDING, then the
I/O isn’t complete.

{
if(GetOverlappedResult(hDevice,&hKevent_0,&numread,TRUE))
{
res = 0;
ResetEvent(hKevent_0.hEvent);
return numread;
}
}
else{
res = WaitForSingleObject(hKevent_0.hEvent,500);
if(res == WAIT_OBJECT_0)
{
if(intr_flag_0==0)
return numread;
}
ResetEvent(hKevent_0.hEvent);
return numread;
}

This is also incorrect. Assuming you got ERROR_IO_PENDING, you always
need to call GetOverlappedResult eventually. You CAN call
WaitForSingleObject to wait for the I/O to finish, but that does not
return “numread” to you.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

I have used t overlapped hevent created using createevent , passed to ioctl
part , completion port will check the status of Io using
getoberlappedstatus which is running in the thread.

If I use the getiverlappedresult , after waiting for event to check for Io
completion . You were saying that not to return numread ?

If I call there inside the getiverlappedresult with waitforsingleobject the
applixatiin won’t exit properly

On 30-Nov-2016 12:41 AM, “Tim Roberts” wrote:

> xxxxx@gmail.com wrote:
> > Im calling the fun() infinite until the keyboard stokes like :
> > Thread()
> > {
> > completionPortHandle = CreateIoCompletionPort(hDevice,
> > NULL,
> > 1,
> > 0);
>
> You’re not using the completion port in this code, so I’m not clear why
> it is here. Also, if the completion port creation fails, you just flow
> on and continue your processing as if it had succeeded, passing a bad
> handle to your thread.
>
> > int fun(buffer data)
> > {
> > hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
>
> This serves no purpose. Your OVERLAPPED object (hKevent_0) has its own
> event. Also, it looks like hEvent is a global here. That means every
> time you run this function, you are overwriting the previous handle.
> You just keep creating new ones until you run out of resources.
>
>
> > if(res == ERROR_IO_PENDING)
> > {
> > if(pstate.state == TRUE)
>
> I can’t tell what you’re trying to do here, but this can’t be right. If
> “pstate” is an output from the ioctl, then you can’t touch the structure
> until the I/O is complete. If the result was ERROR_IO_PENDING, then the
> I/O isn’t complete.
>
>
> > {
> > if(GetOverlappedResult(
> hDevice,&hKevent_0,&numread,TRUE))
> > {
> > res = 0;
> > ResetEvent(hKevent_0.hEvent);
> > return numread;
> > }
> > }
> > else{
> > res = WaitForSingleObject(hKevent_0.
> hEvent,500);
> > if(res == WAIT_OBJECT_0)
> > {
> > if(intr_flag_0==0)
> > return numread;
> > }
> > ResetEvent(hKevent_0.hEvent);
> > return numread;
> > }
>
> This is also incorrect. Assuming you got ERROR_IO_PENDING, you always
> need to call GetOverlappedResult eventually. You CAN call
> WaitForSingleObject to wait for the I/O to finish, but that does not
> return “numread” to you.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: http:> showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer&gt;
></http:></http:>

Prabhakar V wrote:

I have used t overlapped hevent created using createevent , passed to
ioctl part , completion port will check the status of Io using
getoberlappedstatus which is running in the thread.

Nothing in the code you showed us was using the completion port. You
were doing normal overlapped I/O.

If I use the getiverlappedresult , after waiting for event to check
for Io completion . You were saying that not to return numread ?

If I call there inside the getiverlappedresult with
waitforsingleobject the applixatiin won’t exit properly

When you submit an overlapped request, the I/O system will trigger the
event when the request is completed. YOU need to know what was the
final status of the request: Did it succeed or fail? How many bytes
were returned? Only GetOverlappedResult can do that, but you can’t
learn that until the request has completed.

Now, it turns out GetOverlappedResult can do both things: wait AND
return the results. If you pass bWait == TRUE, then it will wait for
the event (using WaitForSingleObject), and then it will return the
status and the number of bytes transferred. You always need to call
GetOverlappedResult eventually.

You CAN call WaitForSingleObject on the event yourself, although there’s
almost never a reason to do so. You would still need to call
GetOverlappedResult to find out the result of the request.

(It is common to use WaitForMultipleObjects when you have several open
requests at one time, but it’s hard to think of a reason to use
WaitForSingleObject instead of just calling GetOverlappedResult, which
you have to call anyway.)


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

@Tim:
You can have a timeout in WaitForSingelObject, which is not possible with GetOverlappedResult. Or you can use GetOverlappedResultEx

This must necessarily be psudocode as a line like this cannot compile

if(tx mode)

to use an IO Completion port, you need to associate the device (file etc.) handle with the completion port after one has been created

normally this is done with code like this

// create an IO Completion Port

hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, dwConcurrency);

// check for failure

// associate a file handle with an IO Completion Port

if(CreateIoCompletionPort(hFile, hIOCP, KEY, 0) == NULL)

{

return false;

}

Although the code you posted could work. The reason that they are typically created fist is that usually an IOCP will service IO from many handles and none are know at startup ? although it is certainly possible to use IOCP with a single handle too.

In this model, you need to create at least one thread to read the completions by using a loop like this

while(true)

{

// wait for IO

dwRet = GetQueuedCompletionStatus(hIOCP,

&dwCB,

&dwKey,

&pOverlapped,

INFINITE);

// check for failures etc.

}

It is normal to have several threads running this sort of loop concurrently

An alternative that should be attractive to you would be to use the thread pool APIs instead as they handle much of this for you and provide other important features. They are generally recommended for new development

https://msdn.microsoft.com/en-us/library/windows/desktop/ms686760(v=vs.85).aspx

Sent from Mailhttps: for Windows 10

From: xxxxx@gmail.commailto:xxxxx
Sent: November 29, 2016 1:48 PM
To: Windows System Software Devs Interest Listmailto:xxxxx
Subject: [ntdev] from application to driver using Inverted call method

Dear all,

I have used the inverted call method in driver, i have configured the EvtIocontrol with parallel dispatch, then created the manual queue.

From application im calling the fun() which uses the overlapped i/o in async ,send to driver through ioctl to driver where it uses the wdfrequestretriveinputbuffer which has information and writing into register then im pending the request , that time ISR will be triggered and queue to dpc routine where im retreiveing the requet until no entries and fill the output buffer and completing the request .

Im calling the fun() infinite until the keyboard stokes like :
Thread()
{
completionPortHandle = CreateIoCompletionPort(hDevice,
NULL,
1,
0);

if(completionPortHandle == NULL) {

code = GetLastError();

printf(“CreateIoCompletionPort failed with error 0x%x\n”, code);

}
hThread = CreateThread(
NULL, // Default thread security descriptor
0, // Default stack size
CompletionPortThread, // Start routine
completionPortHandle, // Start routine parameter
0, // Run immediately
NULL // Thread ID
);

if (hThread == NULL)
{
code = GetLastError();

printf(“CreateThread failed with error 0x%x\n”, code);

}
return;
}

int fun(buffer data)
{
hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(DeviceIoControl(hDevice,
IOCTL_TX,
buffer data,
sizeof(buffer data),
&pstate,
sizeof(SWITCH_STATE),
&numread,
&hKevent_0))
{
res =0;
}
else{
res = GetLastError();
//return numread;
}
if(res == ERROR_IO_PENDING)
{
if(pstate.state == TRUE)
{
if(GetOverlappedResult(hDevice,&hKevent_0,&numread,TRUE))
{
res = 0;
ResetEvent(hKevent_0.hEvent);
return numread;
}
}
else{
res = WaitForSingleObject(hKevent_0.hEvent,500);
if(res == WAIT_OBJECT_0)
{
if(intr_flag_0==0)
return numread;
}
ResetEvent(hKevent_0.hEvent);
return numread;
}

}

main()
{

if(tx mode)
{
Thread();
while(!kbhit)
{
int ret = fun();

if(ret>0)
{
writing into file for log
}
}
else{ // RX mode
RXfun() read the data from driver throug ioctl and save to log
}

Im running the console application in two terminal for rx/tx mode and compare the log ( tx reach very high but rx log is small)

Driver side uses the inverted call method.

Pls how i can improve the performane of driver/console application

}


NTDEV is sponsored by OSR

Visit the list online at: http:

MONTHLY seminars on crash dump analysis, WDF, Windows internals and software drivers!
Details at http:

To unsubscribe, visit the List Server section of OSR Online at http:</http:></http:></http:></mailto:xxxxx></mailto:xxxxx></https:>

Dear all ,

Its happy to guide me always.

Pls check my code and give my inputs


DWORD WINAPI CompletionPortThread(LPVOID PortHandle)
{
DWORD byteCount = 0;
ULONG_PTR compKey = 0;
OVERLAPPED* overlapped = NULL;
BOOL worked ;
// POVL_WRAPPER wrap = NULL;
DWORD res;
// printf(“iocompletion %x \n”,flag_count);
while(TRUE/*intr_flag_0 */) {

// Wait for a completion notification.
overlapped = NULL;

–flag_count;
//printf(“iocompletion %x \n”,flag_count);
worked = GetQueuedCompletionStatus(
PortHandle, // Completion
port handle
&byteCount, // Bytes
transferred
&compKey, // Completion
key… don’t care
&overlapped, // OVERLAPPED
structure
INFINITE);
// Notification time-out interval

//
// If it’s our notification ioctl that’s just been completed…
// don’t do anything special.
//
if (byteCount == 0) {
continue;
}

if (overlapped == NULL) {

// An unrecoverable error occurred in the completion port.
// Wait for the next notification.
continue;
}
}
//ExitThread(0);
return;
}
VOID Thread()
{
DWORD code;
completionPortHandle = CreateIoCompletionPort(hDevice,
NULL,
1,
0);

if(completionPortHandle == NULL) {

code = GetLastError();

printf(“CreateIoCompletionPort failed with error 0x%x\n”, code);

}
hThread = CreateThread(
NULL, // Default thread security descriptor
0, // Default stack size
CompletionPortThread, // Start routine
completionPortHandle, // Start routine parameter
0, // Run immediately
NULL // Thread ID
);

if (hThread == NULL)
{
code = GetLastError();

printf(“CreateThread failed with error 0x%x\n”, code);

// return(code);
}
return;
}

Fun(Buffer is pointer to strcuture)
{
hKevent_0.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(DeviceIoControl(hDevice,
IOCTL_TX,
msg,
sizeof(Buffer is pointer to strcuture),
&pstate,
sizeof(SWITCH_STATE),
&numread,
&hKevent_0))
{
res =0;
}
else{
res = GetLastError();
//return numread;
}
if(res == ERROR_IO_PENDING)
{
if(pstate.state == TRUE)
{
if(GetOverlappedResult(hDevice,&hKevent_0,&numread,TRUE))
{
res = 0;
ResetEvent(hKevent_0.hEvent);
return numread;
}
}
else{
res = WaitForSingleObject(hKevent_0.hEvent,500);
if(res == WAIT_OBJECT_0)
{
if(intr_flag_0==0)
return numread;
}
ResetEvent(hKevent_0.hEvent);
return numread;
}
}
}

main()
{

int mode;
enter the mode =TX(0)/RX(1)
if(tx==0)
{
Enter the buffer strcuture
Call

While(!kbhit())
{
int ret = fun(buffer strcuture);
if(ret>0)
{
Write into TX log
}
else{
while(!kbhit())
{
RX function read from driver
write into log
}
}
}
}
}

Driver side

EvtIoctrol forwarding the request and complet it in DPC routine whever TX
interrupt generated.

ISR()
{

}
InterruptDpc(
__in WDFINTERRUPT WdfInterrupt,
__in WDFOBJECT WdfDevice
)
{

do
{
status = WdfIoQueueRetrieveNextRequest(devExt->InterruptMsgQueue[0],
&notifyRequest);

if (NT_SUCCESS(status))
{
status = WdfRequestRetrieveOutputBuffer(notifyRequest,
sizeof(SWITCH_STATE),
&switchState,
NULL);// BufferLength
if (!NT_SUCCESS(status))
{
// TraceEvents(TRACE_LEVEL_ERROR, DBG_IOCTL,
// “User’s output buffer is too small for this IOCTL, expecting a
SWITCH_STATE\n”);
bytesReturned = sizeof(SWITCH_STATE);
} else
{
//
// Copy the state information saved by the continuous reader.
//
switchState->state= TRUE;
bytesReturned = sizeof(SWITCH_STATE);
}
WdfRequestCompleteWithInformation(notifyRequest, status, bytesReturned);
status = STATUS_SUCCESS;
}
else if(status != STATUS_NO_MORE_ENTRIES)
{
}
notifyRequest = NULL;
}while (status == STATUS_SUCCESS);
}

On Wed, Nov 30, 2016 at 6:30 AM, Marion Bond wrote:

> This must necessarily be psudocode as a line like this cannot compile
>
>
>
> if(tx mode)
>
>
>
>
>
> to use an IO Completion port, you need to associate the device (file etc.)
> handle with the completion port after one has been created
>
>
>
> normally this is done with code like this
>
>
>
> // create an IO Completion Port
>
> hIOCP = CreateIoCompletionPort(INVALID_HANDLE_VALUE, NULL, 0, dwConcurrency);
>
> // check for failure
>
>
>
> // associate a file handle with an IO Completion Port
>
> if(CreateIoCompletionPort(hFile, hIOCP, KEY, 0) == NULL)
>
> {
>
> return false;
>
> }
>
>
>
> Although the code you posted could work. The reason that they are
> typically created fist is that usually an IOCP will service IO from
> many
handles and none are know at startup – although it is certainly
> possible to use IOCP with a single handle too.
>
>
>
> In this model, you need to create at least one thread to read the
> completions by using a loop like this
>
>
>
> while(true)
>
> {
>
> // wait for IO
>
> dwRet = GetQueuedCompletionStatus(hIOCP,
>
> &dwCB,
>
> &dwKey,
>
> &pOverlapped,
>
> INFINITE);
>
> // check for failures etc.
>
> }
>
>
>
> It is normal to have several threads running this sort of loop concurrently
>
>
>
> An alternative that should be attractive to you would be to use the thread
> pool APIs instead as they handle much of this for you and provide other
> important features. They are generally recommended for new development
>
>
>
> https://msdn.microsoft.com/en-us/library/windows/desktop/
> ms686760(v=vs.85).aspx
>
>
>
>
>
> Sent from Mail https: for
> Windows 10
>
>
>
> *From: *xxxxx@gmail.com
> *Sent: *November 29, 2016 1:48 PM
> *To: *Windows System Software Devs Interest List
> *Subject: *[ntdev] from application to driver using Inverted call method
>
>
> Dear all,
>
> I have used the inverted call method in driver, i have configured the
> EvtIocontrol with parallel dispatch, then created the manual queue.
>
> From application im calling the fun() which uses the overlapped i/o in
> async ,send to driver through ioctl to driver where it uses the
> wdfrequestretriveinputbuffer which has information and writing into
> register then im pending the request , that time ISR will be triggered and
> queue to dpc routine where im retreiveing the requet until no entries and
> fill the output buffer and completing the request .
>
> Im calling the fun() infinite until the keyboard stokes like :
> Thread()
> {
> completionPortHandle = CreateIoCompletionPort(hDevice,
> NULL,
> 1,
> 0);
>
> if(completionPortHandle == NULL) {
>
> code = GetLastError();
>
> printf(“CreateIoCompletionPort failed with error 0x%x\n”, code);
>
> }
> hThread = CreateThread(
> NULL, // Default thread security descriptor
> 0, // Default stack size
> CompletionPortThread, // Start routine
> completionPortHandle, // Start routine parameter
> 0, // Run immediately
> NULL // Thread ID
> );
>
> if (hThread == NULL)
> {
> code = GetLastError();
>
> printf(“CreateThread failed with error 0x%x\n”, code);
>
> }
> return;
> }
>
> int fun(buffer data)
> {
> hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
> if(DeviceIoControl(hDevice,
> IOCTL_TX,
> buffer data,
> sizeof(buffer data),
> &pstate,
> sizeof(SWITCH_STATE),
> &numread,
> &hKevent_0))
> {
> res =0;
> }
> else{
> res = GetLastError();
> //return numread;
> }
> if(res == ERROR_IO_PENDING)
> {
> if(pstate.state == TRUE)
> {
> if(GetOverlappedResult(
> hDevice,&hKevent_0,&numread,TRUE))
> {
> res = 0;
> ResetEvent(hKevent_0.hEvent);
> return numread;
> }
> }
> else{
> res = WaitForSingleObject(hKevent_0.
> hEvent,500);
> if(res == WAIT_OBJECT_0)
> {
> if(intr_flag_0==0)
> return numread;
> }
> ResetEvent(hKevent_0.hEvent);
> return numread;
> }
>
> }
>
> main()
> {
>
> if(tx mode)
> {
> Thread();
> while(!kbhit)
> {
> int ret = fun();
>
> if(ret>0)
> {
> writing into file for log
> }
> }
> else{ // RX mode
> RXfun() read the data from driver throug ioctl and save to log
> }
>
> Im running the console application in two terminal for rx/tx mode and
> compare the log ( tx reach very high but rx log is small)
>
> Driver side uses the inverted call method.
>
> Pls how i can improve the performane of driver/console application
>
> }
>
> —
> NTDEV is sponsored by OSR
>
> Visit the list online at: http:> showlists.cfm?list=ntdev>
>
> MONTHLY seminars on crash dump analysis, WDF, Windows internals and
> software drivers!
> Details at http:
>
> To unsubscribe, visit the List Server section of OSR Online at <
> http://www.osronline.com/page.cfm?name=ListServer&gt;
></http:></http:></https:>

DWORD WINAPI CompletionPortThread(LPVOID PortHandle)
{
DWORD byteCount = 0;
ULONG_PTR compKey = 0;
OVERLAPPED* overlapped = NULL;
BOOL worked ;
// POVL_WRAPPER wrap = NULL;
DWORD res;
// printf(“iocompletion %x \n”,flag_count);
while(TRUE/*intr_flag_0 */) {

// Wait for a completion notification.
overlapped = NULL;

–flag_count;
//printf(“iocompletion %x \n”,flag_count);
worked = GetQueuedCompletionStatus(
PortHandle, // Completion
port handle
&byteCount, // Bytes
transferred
&compKey, // Completion
key… don’t care
&overlapped, // OVERLAPPED
structure
INFINITE);
// Notification time-out interval

//
// If it’s our notification ioctl that’s just been completed…
// don’t do anything special.
//
if (byteCount == 0) {
continue;
}

if (overlapped == NULL) {

// An unrecoverable error occurred in the completion port.
// Wait for the next notification.
continue;
}
}
//ExitThread(0);
return;
}

VOID Thread()
{
DWORD code;
completionPortHandle = CreateIoCompletionPort(hDevice,
NULL,
1,
0);

if(completionPortHandle == NULL) {

code = GetLastError();

printf(“CreateIoCompletionPort failed with error 0x%x\n”, code);

}
hThread = CreateThread(
NULL, // Default thread security descriptor
0, // Default stack size
CompletionPortThread, // Start routine
completionPortHandle, // Start routine parameter
0, // Run immediately
NULL // Thread ID
);

if (hThread == NULL)
{
code = GetLastError();

printf(“CreateThread failed with error 0x%x\n”, code);

// return(code);
}
return;
}

On Thu, Dec 1, 2016 at 11:08 PM, Prabhakar V wrote:

> DWORD WINAPI CompletionPortThread(LPVOID PortHandle)
> {
> DWORD byteCount = 0;
> ULONG_PTR compKey = 0;
> OVERLAPPED* overlapped = NULL;
> BOOL worked ;
> // POVL_WRAPPER wrap = NULL;
> DWORD res;
> // printf(“iocompletion %x \n”,flag_count);
> while(TRUE/*intr_flag_0 */) {
>
> // Wait for a completion notification.
> overlapped = NULL;
>
> --flag_count;
> //printf(“iocompletion %x \n”,flag_count);
> worked = GetQueuedCompletionStatus(
> PortHandle, // Completion
> port handle
> &byteCount, // Bytes
> transferred
> &compKey, // Completion
> key… don’t care
> &overlapped, // OVERLAPPED
> structure
> INFINITE);
> // Notification time-out interval
>
> //
> // If it’s our notification ioctl that’s just been completed…
> // don’t do anything special.
> //
> if (byteCount == 0) {
> continue;
> }
>
> if (overlapped == NULL) {
>
> // An unrecoverable error occurred in the completion port.
> // Wait for the next notification.
> continue;
> }
> }
> //ExitThread(0);
> return;
> }
>
>

Fun(Buffer is pointer to strcuture)
{
hKevent_0.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(DeviceIoControl(hDevice,
IOCTL_TX,
msg,
sizeof(Buffer is pointer to strcuture),
&pstate,
sizeof(SWITCH_STATE),
&numread,
&hKevent_0))
{
res =0;
}
else{
res = GetLastError();
//return numread;
}
if(res == ERROR_IO_PENDING)
{
if(pstate.state == TRUE)
{
if(GetOverlappedResult(hDevice,&hKevent_0,&numread,TRUE))
{
res = 0;
ResetEvent(hKevent_0.hEvent);
return numread;
}
}
else{
res = WaitForSingleObject(hKevent_0.hEvent,500);
if(res == WAIT_OBJECT_0)
{
if(intr_flag_0==0)
return numread;
}
ResetEvent(hKevent_0.hEvent);
return numread;
}
}
}

On Thu, Dec 1, 2016 at 11:08 PM, Prabhakar V wrote:

> VOID Thread()
> {
> DWORD code;
> completionPortHandle = CreateIoCompletionPort(hDevice,
> NULL,
> 1,
> 0);
>
> if(completionPortHandle == NULL) {
>
> code = GetLastError();
>
> printf(“CreateIoCompletionPort failed with error 0x%x\n”, code);
>
> }
> hThread = CreateThread(
> NULL, // Default thread security descriptor
> 0, // Default stack size
> CompletionPortThread, // Start routine
> completionPortHandle, // Start routine parameter
> 0, // Run immediately
> NULL // Thread ID
> );
>
> if (hThread == NULL)
> {
> code = GetLastError();
>
> printf(“CreateThread failed with error 0x%x\n”, code);
>
> // return(code);
> }
> return;
> }
>
>
> On Thu, Dec 1, 2016 at 11:08 PM, Prabhakar V
> wrote:
>
>> DWORD WINAPI CompletionPortThread(LPVOID PortHandle)
>> {
>> DWORD byteCount = 0;
>> ULONG_PTR compKey = 0;
>> OVERLAPPED* overlapped = NULL;
>> BOOL worked ;
>> // POVL_WRAPPER wrap = NULL;
>> DWORD res;
>> // printf(“iocompletion %x \n”,flag_count);
>> while(TRUE/*intr_flag_0 */) {
>>
>> // Wait for a completion notification.
>> overlapped = NULL;
>>
>> --flag_count;
>> //printf(“iocompletion %x \n”,flag_count);
>> worked = GetQueuedCompletionStatus(
>> PortHandle, // Completion
>> port handle
>> &byteCount, // Bytes
>> transferred
>> &compKey, // Completion
>> key… don’t care
>> &overlapped, // OVERLAPPED
>> structure
>> INFINITE);
>> // Notification time-out interval
>>
>> //
>> // If it’s our notification ioctl that’s just been completed…
>> // don’t do anything special.
>> //
>> if (byteCount == 0) {
>> continue;
>> }
>>
>> if (overlapped == NULL) {
>>
>> // An unrecoverable error occurred in the completion port.
>> // Wait for the next notification.
>> continue;
>> }
>> }
>> //ExitThread(0);
>> return;
>> }
>>
>>
>

main()
{

int mode;
enter the mode =TX(0)/RX(1)
if(tx==0)
{
Enter the buffer strcuture
Call

While(!kbhit())
{
int ret = fun(buffer strcuture);
if(ret>0)
{
Write into TX log
}
else{
while(!kbhit())
{
RX function read from driver
write into log
}
}
}
}
}

On Thu, Dec 1, 2016 at 11:09 PM, Prabhakar V wrote:

> Fun(Buffer is pointer to strcuture)
> {
> hKevent_0.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
> if(DeviceIoControl(hDevice,
> IOCTL_TX,
> msg,
> sizeof(Buffer is pointer to strcuture),
> &pstate,
> sizeof(SWITCH_STATE),
> &numread,
> &hKevent_0))
> {
> res =0;
> }
> else{
> res = GetLastError();
> //return numread;
> }
> if(res == ERROR_IO_PENDING)
> {
> if(pstate.state == TRUE)
> {
> if(GetOverlappedResult(hDevice,&hKevent_0,&numread,TRUE))
> {
> res = 0;
> ResetEvent(hKevent_0.hEvent);
> return numread;
> }
> }
> else{
> res = WaitForSingleObject(hKevent_0.hEvent,500);
> if(res == WAIT_OBJECT_0)
> {
> if(intr_flag_0==0)
> return numread;
> }
> ResetEvent(hKevent_0.hEvent);
> return numread;
> }
> }
> }
>
> On Thu, Dec 1, 2016 at 11:08 PM, Prabhakar V
> wrote:
>
>> VOID Thread()
>> {
>> DWORD code;
>> completionPortHandle = CreateIoCompletionPort(hDevice,
>> NULL,
>> 1,
>> 0);
>>
>> if(completionPortHandle == NULL) {
>>
>> code = GetLastError();
>>
>> printf(“CreateIoCompletionPort failed with error 0x%x\n”, code);
>>
>> }
>> hThread = CreateThread(
>> NULL, // Default thread security descriptor
>> 0, // Default stack size
>> CompletionPortThread, // Start routine
>> completionPortHandle, // Start routine parameter
>> 0, // Run immediately
>> NULL // Thread ID
>> );
>>
>> if (hThread == NULL)
>> {
>> code = GetLastError();
>>
>> printf(“CreateThread failed with error 0x%x\n”, code);
>>
>> // return(code);
>> }
>> return;
>> }
>>
>>
>> On Thu, Dec 1, 2016 at 11:08 PM, Prabhakar V
>> wrote:
>>
>>> DWORD WINAPI CompletionPortThread(LPVOID PortHandle)
>>> {
>>> DWORD byteCount = 0;
>>> ULONG_PTR compKey = 0;
>>> OVERLAPPED* overlapped = NULL;
>>> BOOL worked ;
>>> // POVL_WRAPPER wrap = NULL;
>>> DWORD res;
>>> // printf(“iocompletion %x \n”,flag_count);
>>> while(TRUE/*intr_flag_0 */) {
>>>
>>> // Wait for a completion notification.
>>> overlapped = NULL;
>>>
>>> --flag_count;
>>> //printf(“iocompletion %x \n”,flag_count);
>>> worked = GetQueuedCompletionStatus(
>>> PortHandle, // Completion
>>> port handle
>>> &byteCount, // Bytes
>>> transferred
>>> &compKey, // Completion
>>> key… don’t care
>>> &overlapped, // OVERLAPPED
>>> structure
>>> INFINITE);
>>> // Notification time-out interval
>>>
>>> //
>>> // If it’s our notification ioctl that’s just been completed…
>>> // don’t do anything special.
>>> //
>>> if (byteCount == 0) {
>>> continue;
>>> }
>>>
>>> if (overlapped == NULL) {
>>>
>>> // An unrecoverable error occurred in the completion port.
>>> // Wait for the next notification.
>>> continue;
>>> }
>>> }
>>> //ExitThread(0);
>>> return;
>>> }
>>>
>>>
>>
>


Driver side

EvtIoctrol forwarding the request and complet it in DPC routine whever TX
interrupt generated.

ISR()
{

}
InterruptDpc(
__in WDFINTERRUPT WdfInterrupt,
__in WDFOBJECT WdfDevice
)
{

do
{
status = WdfIoQueueRetrieveNextRequest(devExt->InterruptMsgQueue[0],
&notifyRequest);

if (NT_SUCCESS(status))
{
status = WdfRequestRetrieveOutputBuffer(notifyRequest,
sizeof(SWITCH_STATE),
&switchState,
NULL);// BufferLength
if (!NT_SUCCESS(status))
{
bytesReturned = sizeof(SWITCH_STATE);
} else
{
switchState->state= TRUE;
bytesReturned = sizeof(SWITCH_STATE);
}
WdfRequestCompleteWithInformation(notifyRequest, status, bytesReturned);
status = STATUS_SUCCESS;
}
else if(status != STATUS_NO_MORE_ENTRIES)
{
}
notifyRequest = NULL;
}while (status == STATUS_SUCCESS);
}

On Thu, Dec 1, 2016 at 11:09 PM, Prabhakar V wrote:

> main()
> {
>
> int mode;
> enter the mode =TX(0)/RX(1)
> if(tx==0)
> {
> Enter the buffer strcuture
> Call
>
> While(!kbhit())
> {
> int ret = fun(buffer strcuture);
> if(ret>0)
> {
> Write into TX log
> }
> else{
> while(!kbhit())
> {
> RX function read from driver
> write into log
> }
> }
> }
> }
> }
>
>
> On Thu, Dec 1, 2016 at 11:09 PM, Prabhakar V
> wrote:
>
>> Fun(Buffer is pointer to strcuture)
>> {
>> hKevent_0.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
>> if(DeviceIoControl(hDevice,
>> IOCTL_TX,
>> msg,
>> sizeof(Buffer is pointer to strcuture),
>> &pstate,
>> sizeof(SWITCH_STATE),
>> &numread,
>> &hKevent_0))
>> {
>> res =0;
>> }
>> else{
>> res = GetLastError();
>> //return numread;
>> }
>> if(res == ERROR_IO_PENDING)
>> {
>> if(pstate.state == TRUE)
>> {
>> if(GetOverlappedResult(hDevice,&hKevent_0,&numread,TRUE))
>> {
>> res = 0;
>> ResetEvent(hKevent_0.hEvent);
>> return numread;
>> }
>> }
>> else{
>> res = WaitForSingleObject(hKevent_0.hEvent,500);
>> if(res == WAIT_OBJECT_0)
>> {
>> if(intr_flag_0==0)
>> return numread;
>> }
>> ResetEvent(hKevent_0.hEvent);
>> return numread;
>> }
>> }
>> }
>>
>> On Thu, Dec 1, 2016 at 11:08 PM, Prabhakar V
>> wrote:
>>
>>> VOID Thread()
>>> {
>>> DWORD code;
>>> completionPortHandle = CreateIoCompletionPort(hDevice,
>>> NULL,
>>> 1,
>>> 0);
>>>
>>> if(completionPortHandle == NULL) {
>>>
>>> code = GetLastError();
>>>
>>> printf(“CreateIoCompletionPort failed with error 0x%x\n”, code);
>>>
>>> }
>>> hThread = CreateThread(
>>> NULL, // Default thread security descriptor
>>> 0, // Default stack size
>>> CompletionPortThread, // Start routine
>>> completionPortHandle, // Start routine parameter
>>> 0, // Run immediately
>>> NULL // Thread ID
>>> );
>>>
>>> if (hThread == NULL)
>>> {
>>> code = GetLastError();
>>>
>>> printf(“CreateThread failed with error 0x%x\n”, code);
>>>
>>> // return(code);
>>> }
>>> return;
>>> }
>>>
>>>
>>> On Thu, Dec 1, 2016 at 11:08 PM, Prabhakar V
>>> wrote:
>>>
>>>> DWORD WINAPI CompletionPortThread(LPVOID PortHandle)
>>>> {
>>>> DWORD byteCount = 0;
>>>> ULONG_PTR compKey = 0;
>>>> OVERLAPPED* overlapped = NULL;
>>>> BOOL worked ;
>>>> // POVL_WRAPPER wrap = NULL;
>>>> DWORD res;
>>>> // printf(“iocompletion %x \n”,flag_count);
>>>> while(TRUE/*intr_flag_0 */) {
>>>>
>>>> // Wait for a completion notification.
>>>> overlapped = NULL;
>>>>
>>>> --flag_count;
>>>> //printf(“iocompletion %x \n”,flag_count);
>>>> worked = GetQueuedCompletionStatus(
>>>> PortHandle, //
>>>> Completion port handle
>>>> &byteCount, // Bytes
>>>> transferred
>>>> &compKey, //
>>>> Completion key… don’t care
>>>> &overlapped, //
>>>> OVERLAPPED structure
>>>> INFINITE);
>>>> // Notification time-out interval
>>>>
>>>> //
>>>> // If it’s our notification ioctl that’s just been completed…
>>>> // don’t do anything special.
>>>> //
>>>> if (byteCount == 0) {
>>>> continue;
>>>> }
>>>>
>>>> if (overlapped == NULL) {
>>>>
>>>> // An unrecoverable error occurred in the completion port.
>>>> // Wait for the next notification.
>>>> continue;
>>>> }
>>>> }
>>>> //ExitThread(0);
>>>> return;
>>>> }
>>>>
>>>>
>>>
>>
>

Prabhakar V wrote:

Its happy to guide me always.

Pls check my code and give my inputs

Did you not like my previous answer? Because you haven’t changed anything.

You are not using the completion port for anything. Your code would
work exactly the same if you removed the completion port stuff altogether.

You still have not explained this:

if(res == ERROR_IO_PENDING)
{
if(pstate.state == TRUE)
{
if(GetOverlappedResult(hDevice,&hKevent_0,&numread,TRUE))
{

Why are you checking “pstate.state” here? That’s part of your data
structure. Why should that have any effect on the way you check your
results?

else{
res = WaitForSingleObject(hKevent_0.hEvent,500);
if(res == WAIT_OBJECT_0)
{
if(intr_flag_0==0)
return numread;
}
ResetEvent(hKevent_0.hEvent);
return numread;
}

This is wrong. If you don’t call GetOverlappedResult, then numread has
a random value.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

I have not read your code in detail, so there may be other issues but these issues are obvious

  1. Your worker thread will never terminate. This may not be a problem for your now, but I always like my code to shutdown cleanly. Normally this is done by using PostQueuedCompletionStatus to send an exit signal. This allows you to dynamically add / remove worker threads ? again the thread pool does much of this for you

  2. The IO you are doing is essentially serial and the completion port serves no purpose. In your Fun function, you should allocate an OVERLAPPED structure (possibly adding your own extra context afterward) from the heap, initiating the IO (DeviceIOControl) and then return. At some point later, the IO will complete and the worker thread will pick up the OVERLAPPED structure. The worker thread should then do the work required with the data and then free the OVERLAPPED back to the heap

Sent from Mailhttps: for Windows 10

From: Prabhakar Vmailto:xxxxx
Sent: December 1, 2016 12:39 PM
To: Marion Bondmailto:xxxxx
Cc: Windows System Software Devs Interest Listmailto:xxxxx
Subject: Re: [ntdev] from application to driver using Inverted call method

main()
{

int mode;
enter the mode =TX(0)/RX(1)
if(tx==0)
{
Enter the buffer strcuture
Call

While(!kbhit())
{
int ret = fun(buffer strcuture);
if(ret>0)
{
Write into TX log
}
else{
while(!kbhit())
{
RX function read from driver
write into log
}
}
}
}
}

On Thu, Dec 1, 2016 at 11:09 PM, Prabhakar V > wrote:
Fun(Buffer is pointer to strcuture)
{
hKevent_0.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if(DeviceIoControl(hDevice,
IOCTL_TX,
msg,
sizeof(Buffer is pointer to strcuture),
&pstate,
sizeof(SWITCH_STATE),
&numread,
&hKevent_0))
{
res =0;
}
else{
res = GetLastError();
//return numread;
}
if(res == ERROR_IO_PENDING)
{
if(pstate.state == TRUE)
{
if(GetOverlappedResult(hDevice,&hKevent_0,&numread,TRUE))
{
res = 0;
ResetEvent(hKevent_0.hEvent);
return numread;
}
}
else{
res = WaitForSingleObject(hKevent_0.hEvent,500);
if(res == WAIT_OBJECT_0)
{
if(intr_flag_0==0)
return numread;
}
ResetEvent(hKevent_0.hEvent);
return numread;
}
}
}

On Thu, Dec 1, 2016 at 11:08 PM, Prabhakar V > wrote:
VOID Thread()
{
DWORD code;
completionPortHandle = CreateIoCompletionPort(hDevice,
NULL,
1,
0);

if(completionPortHandle == NULL) {

code = GetLastError();

printf(“CreateIoCompletionPort failed with error 0x%x\n”, code);

}
hThread = CreateThread(
NULL, // Default thread security descriptor
0, // Default stack size
CompletionPortThread, // Start routine
completionPortHandle, // Start routine parameter
0, // Run immediately
NULL // Thread ID
);

if (hThread == NULL)
{
code = GetLastError();

printf(“CreateThread failed with error 0x%x\n”, code);

// return(code);
}
return;
}

On Thu, Dec 1, 2016 at 11:08 PM, Prabhakar V > wrote:
DWORD WINAPI CompletionPortThread(LPVOID PortHandle)
{
DWORD byteCount = 0;
ULONG_PTR compKey = 0;
OVERLAPPED* overlapped = NULL;
BOOL worked ;
// POVL_WRAPPER wrap = NULL;
DWORD res;
// printf(“iocompletion %x \n”,flag_count);
while(TRUE/*intr_flag_0 */) {

// Wait for a completion notification.
overlapped = NULL;

–flag_count;
//printf(“iocompletion %x \n”,flag_count);
worked = GetQueuedCompletionStatus(
PortHandle, // Completion port handle
&byteCount, // Bytes transferred
&compKey, // Completion key… don’t care
&overlapped, // OVERLAPPED structure
INFINITE);
// Notification time-out interval

//
// If it’s our notification ioctl that’s just been completed…
// don’t do anything special.
//
if (byteCount == 0) {
continue;
}

if (overlapped == NULL) {

// An unrecoverable error occurred in the completion port.
// Wait for the next notification.
continue;
}
}
//ExitThread(0);
return;
}</mailto:xxxxx></mailto:xxxxx></mailto:xxxxx></https:>

Dear all,
I want that app that runs n tx mode, send the first frame to driver upon the tx interrupt then req complete in dpc then it returns to app to send the next frame…

I have did the Linux driver where I have used wait/wake method, in ioctl side I were waited for tx interrupt when tx interrupt come means where I’m wake then it returns to app to send the next frame… so contioulsy it sending data upto keyboard hit.

  1. app will be run in two terminal tx/Rx mode

  2. when it run tx mode it will contioulsy send the frame to driver where it uses the overlapped and waited for signal using getoverlappedresult with bwait true. Completionthread runs infinitely and check for getoverlappedstatus with timeout infinite which is running in while loop.

  3. so application for tx mode whether it blocks tat means it not running and it not sending any frame to driver side it were blocked , if doesn’t block means it will be properly exit.

In driver side I have forward the request upon the tx interrupt I queued to dpc routine and where im retreiving the request and complete and also check for request upto no entries. In application i have used overlapped ,created the events and Embedded to it and passed to deviceiocontrol , called getoverlappedresult then wait for single object then reset event n return the value. Im app main thread where I’m calling the tx function where it does the debiceioctrol with overlapped call getoverlappedresult then wait for single object and also running the completion thread n does the getoverlappedstatus. When I hit the keyboard strokes using !kbhit infinitely calling tbis tx function in one terminal then other terminal I meal callingthe Rx function.

Pls suggest me how I can exit the application for tx mode which is running infinitely

Regards,
Prabhakar vinayagam