NonPNP driver fails to unload after marking the service for deletion

Hi All,

I have a user-space application that launches and interacts with the control driver, once the operation is complete triggers a unload request using service-control APIs.

Stopping of driver service is done in the following way:

ControlService((SC_HANDLE)hService, SERVICE_CONTROL_STOP,&serviceStatus);
DeleteService((SC_HANDLE)hService);
CloseServiceHandle((SC_HANDLE)hService);
CloseServiceHandle((SC_HANDLE)hSCManager);

There are no errors seen in the above sequence of operations. But I do not see EvtDriverUnload() callback of the control driver is called as well.
Upon the next run of the user-space application, the CreateService() call fails with the error as ERROR_SERVICE_MARKED_FOR_DELETE (which means the service was marked for deletion but the operation is not complete yet?)

How can one perform clean unload of the control driver at the first attempt? Am I missing anything that is leading to this behavior?

Thanks!

Do you have any handles open to the device?

Obviously you are omitting error checking / handling code, but why are there casts in the invocations?

It is unlikely to have anything to do with your problem, but why would you declare these variables as anything other than SC_HANDLE, and if already SC_HANDLE, why cast?

Your problem is almost certainly an open handle. Yes, marked for deletion means that it will be deleted when it can be. That means when there are no longer any references to it. It might require a reboot for that to happen, but it usually doesnโ€™t unless there are handle leaks

Yes looks there was a pending IoTarget handle open to remote device which was blocking the unload. Closing it I was able to see driver getting unloaded. Thanks Scott :smile: