The README for the KMDF NDIS protocol sample driver walks you through installation then manually starting the protocol driver as a service. Is there a way to start the service at installation time rather than as a post install step?
When i try to change the StartType code to 0x2 (SERVICE_AUTO_START) I get a warning that says 0x2 is not compatible with a ServiceType of 0x1 (SERVICE_KERNEL_DRIVER).
Preface: it’s important to mention that all this discussion applies only to SCM-managed drivers, aka non-PNP drivers. If you have a PNP driver, then PNP governs the lifetime of your driver, and those rules are very different from SCM’s rules.
Anyway, there’s two orthogonal points:
- Starting a driver when the system boots
- Starting a driver after your installer is finished
These points are deliberately orthogonal: many drivers do not want to be started the moment that their service record is created. Typically, the driver would be installed as one small piece of a larger installer workflow. Maybe the installer has to lay down a half-dozen drivers, an armada of registry keys, and spray the desktop with an ocean of icons; all before the drivers can be started successfully.
Then you’d organize your installation workflow into phases:
- Copy files
- Install drivers
- Write registry keys
- Start driver
- Start usermode services
- Start the GUI
In that case, when you reach step 4, you can either call the StartService
API, or execute its command-line wrapper sc.exe start my_service
.
But for simple projects, your driver might be completely self-contained, and you don’t need the fanciness of separating installation into multiple phases. Then in this case, the SPSVCSINST_STARTSERVICE
convenience flag will tell the OS to start the driver as soon as it’s installed. It’s the equivalent of calling StartService immediately after install; it just saves you the trouble of calling that function yourself.
Meanwhile, orthogonally, you might want the driver to be started at boot. In that case, for drivers, you should use either StartType=SERVICE_SYSTEM_START
or StartType=SERVICE_BOOT_START
. The former means your driver starts faster (since the native storage stack has already been loaded), and more system services are available at the time of DriverEntry (for example, the SOFTWARE registry hive has been mounted). The latter should be used only if your driver is essential to mounting the OS’s boot or paging volume, since it comes with caveats.
Again I’m very thankful for your response. The justification and completeness is most excellent.