windows-servicesdriverswdkkmdf

Starting KMDF driver service after stopping results in mysterious error


I wrote a very basic KMDF driver, installed it, and started it with net start KmdfStart. I then stopped it with net stop KmdfStart. No issue so far.

However when I start it again using net start KmdfStart I get System error 2 has occurred. The system cannot find the file specified. Only restarting the computer helps. The issue happens every time I start & stop, and then attempt to start. The error is shown immediately in the console.

The registry entry HKLM\SYSTEM\CurrentControlSet\Services\KmdfStart has a value ImagePath that is set to System32\drivers\KmdfStart.sys, and that file definitely exists.

How can net start fail with this error, when the file clearly exists?


Solution

  • Forgetting to clean-up prior to the device driver being unloaded is usually the reason for the error message under these circumstances. I've only seen this error message twice before: whilst trying to load a device driver over a network (e.g. Shared Folder); or because of forgetting to clean-up in the device driver prior to unloading the first time - which creates a problem the second time.

    To be precise, by "clean-up" I am referring to un-registering any created symbolic links, devices, etc. If you do not, then when you next try to re-load the device driver (after unloading it), the entry-point can hit but it won't progress with attempt to create the device or symbolic link, since you'd have not cleaned it up in the last session for the device driver.

    If you're doing anything like creating a device (IoCreateDevice/Secure) and symbolic link, make sure you clean it up in the unload routine for the driver. In this example, not cleaning up would cause an issue because you'd be trying to create a device/symbolic link with a name which already exists but is just no longer being used, and you can't have a device/symbolic link created with the same name as another one, otherwise conflict happens on creation attempt.

    Basically, never ever forget to clean-up because it is really important to avoid bugs like these, prevent memory leaks, etc.