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],
¬ifyRequest);
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>
></http:></http:></https:>