none
How to cancel IRP in StartIO queue RRS feed

  • Question

  • I have set cancel routine when called IoStartPacket()

    case IOCTL_StartIO_Write:
                    DebugPrint(("IOCTL_StartIO_Read\n"));
                    IoMarkIrpPending(pIrp);
                    IoStartPacket(pDO, pIrp, 0, OnCancelIrp);
                    Status = STATUS_PENDING;


    VOID
    OnCancelIrp(
    	IN PDEVICE_OBJECT 	DeviceObject,
    	IN PIRP 			Irp)
    {
    	KIRQL	oldirql;
    
    	DebugPrint(("Enter OnCancelIrp"));
    
    	if(Irp == DeviceObject->CurrentIrp){
    		oldirql = Irp->CancelIrql;
    		IoReleaseCancelSpinLock(Irp->CancelIrql);
    		IoStartNextPacket(DeviceObject, TRUE);
    		KeLowerIrql(oldirql);
    	}
    	else{
    		KeRemoveEntryDeviceQueue(&DeviceObject->DeviceQueue, &Irp->Tail.Overlay.DeviceQueueEntry);
    		IoReleaseCancelSpinLock(Irp->CancelIrql);
    	}
    
    	Irp->IoStatus.Status = STATUS_CANCELLED;
    	Irp->IoStatus.Information = 0;
    	IoCompleteRequest(Irp, IO_NO_INCREMENT);
    
    	DebugPrint(("Leave OnCancelIrp"));
    }

    In my App, I call _beginthreadex to do I/O request and I tried to cancel the IRP in my APP.

    but it crash when I comment the Sleep(100)

    why it need to sleep a piece of time?

    and how to know the thread ID?

    int Read_StartIO()
    {
        HANDLE  *hThread;
        hThread = (HANDLE*)malloc(sizeof(HANDLE)*count);
    
        for(i=0; i<count; i++){
            hThread[i] = (HANDLE)_beginthreadex(NULL, 0, Thread, &hndFile, 0, NULL);
            Sleep(100);
        }
    
        WaitForMultipleObjects(count, hThread, TRUE, INFINITE);
        free(hThread);
        return 0;
    }
    UINT WINAPI Thread(LPVOID context)
    {
        OVERLAPPED  overlap = {0};
        DWORD       ReturnedLength;     // Number of bytes returned
    
        overlap.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    
        IoctlResult = DeviceIoControl(
                            hndFile,          
                            IoctlCode,     
                            NULL,       
                            0, 
                            &OutputBuffer,       
                            DataLength,     
                            &ReturnedLength,   
                            &overlap          
                            );
    
        CancelIo(*(PHANDLE)context);  
        WaitForSingleObject(overlap.hEvent, INFINITE);
        printf("@ %d\n",OutputBuffer[0]);
    
        return 0;
    }


    • Edited by _Wayne56 Wednesday, July 18, 2018 9:55 AM
    Wednesday, July 18, 2018 9:53 AM

Answers

  • UINT ThreadId;
    
    hThread[i] = (HANDLE)_beginthreadex(NULL, 0, Thread, &hndFile, 0, &ThreadId);
    
    

    • Marked as answer by _Wayne56 Thursday, July 19, 2018 2:12 AM
    Thursday, July 19, 2018 1:54 AM

All replies

  • The question I would ask is why are you using StartIo?  This is a serialized handling of IRP's which was pretty much being rejected 20 years ago.   A bigger question is why are you writing a WDM driver at all?   KMDF is truly the way to go, all you are doing with WDM is adding work for yourself, and bugs in the driver.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    • Marked as answer by Doron Holan [MSFT] Wednesday, July 18, 2018 9:01 PM
    • Unmarked as answer by _Wayne56 Thursday, July 19, 2018 1:24 AM
    Wednesday, July 18, 2018 2:26 PM
  • The question I would ask is why are you using StartIo?  This is a serialized handling of IRP's which was pretty much being rejected 20 years ago.   A bigger question is why are you writing a WDM driver at all?   KMDF is truly the way to go, all you are doing with WDM is adding work for yourself, and bugs in the driver.


    Don Burn Windows Driver Consulting Website: http://www.windrvr.com

    I'm a beginner of driver development.
    My Department head wants me to practice WDM, WDF.
    so it's just a practice.

    • Edited by _Wayne56 Thursday, July 19, 2018 1:25 AM
    Thursday, July 19, 2018 1:24 AM
  • UINT ThreadId;
    
    hThread[i] = (HANDLE)_beginthreadex(NULL, 0, Thread, &hndFile, 0, &ThreadId);
    
    

    • Marked as answer by _Wayne56 Thursday, July 19, 2018 2:12 AM
    Thursday, July 19, 2018 1:54 AM