Cancel WSK I / O on Driver Unload

I recently studied how to write drivers with the Windows DDK. After creating several test drivers experimenting with system threads and synchronization, I decided to take it up a notch and write a driver that actually does something, although it's useless.

Currently, my driver connects to another computer using the Winsock kernel, and only loops and drives away everything that I send until it receives the "exit" command, because of which it exits the loop. In my loop after calling WskReceive() to retrieve some data from another computer, I use KeWaitForMultipleObjects() to wait for either of the two SynchronizationEvents. BlockEvent is set by my IRP CompletionRoutine() so that my stream knows that it has received some data from the socket. EndEvent is installed by my DriverUnload() program to tell the thread that it has been unloaded and needs to complete it.

When I send the exit command, the stream ends without problems, and I can safely unload the driver later. If I try to stop the driver while it is still waiting for data from another computer, however, it has blue screens with an error: DRIVER_UNLOADED_WITHOUT_CANCELLING_PENDING_OPERATIONS

After I get the EndEvent, but before exiting the loop, I tried to cancel the IRP with IoCancelIrp() and terminate it with IoCompleteRequest() , but both of them give me the DRIVER_IRQL_NOT_LESS_OR_EQUAL error.

Then I tried calling WskDisconnect() , hoping that this would complete the receive operation, but that returned me to the CANCELLING_PENDING_OPERATIONS error.

How to cancel my pending I / O operation from my WSK socket in IRQL, which I run on when the driver is unloaded?

+4
source share

Source: https://habr.com/ru/post/1310535/


All Articles