开发驱动程序时,每次都用INF文件安装再加载实在是很麻烦,就写个程序来实现。
但是在实现驱动程序开机启动时却遇到了问题。
函数原型如下:
Creates a service object and adds it to the specified service control manager database.
SC_HANDLE WINAPI CreateService( __in SC_HANDLE hSCManager, __in LPCTSTR lpServiceName, __in LPCTSTR lpDisplayName, __in DWORD dwDesiredAccess, __in DWORD dwServiceType, __in DWORD dwStartType, __in DWORD dwErrorControl, __in LPCTSTR lpBinaryPathName, __in LPCTSTR lpLoadOrderGroup, __out LPDWORD lpdwTagId, __in LPCTSTR lpDependencies, __in LPCTSTR lpServiceStartName, __in LPCTSTR lpPassword );
指定驱动程序何时启动的参数是 dwStartType,
参数说明如下:
The service start options. This parameter can be one of the following values.
Value | Meaning |
---|---|
SERVICE_AUTO_START |
A service started automatically by the service control manager during system startup. For more information, seeAutomatically Starting Services. |
SERVICE_BOOT_START |
A device driver started by the system loader. This value is valid only for driver services. |
SERVICE_DEMAND_START |
A service started by the service control manager when a process calls the StartServicefunction. For more information, see Starting Services on Demand. |
SERVICE_DISABLED |
A service that cannot be started. Attempts to start the service result in the error code ERROR_SERVICE_DISABLED. |
SERVICE_SYSTEM_START |
A device driver started by the IoInitSystem function. This value is valid only for driver services. |
再具体点,SERVICE_BOOT_START 和 SERVICE_SYSTEM_START,通常用于配置加载设备驱动程序的方式。
SERVICE_AUTO_START、SERVICE_DISABLED 和 SERVICE_DEMAND_START。对应的标准启动类型:自动、禁用和手动,可以使用“计算机管理”管理工具中的“服务”进行配置。
更详细的说明如下:
dwStartType 值分0、1、2、3、4,数值越小就越早启动,SERVICE_BOOT_START(0)是内核刚刚初始化之后,此时加载的都是与系统核心有关的重要的驱动程序,例如磁盘驱动;SERVICE_SYSTEM_START(1)稍完一些;SERVICE_AUTO_START(2)是在登录界面出现的时候开始,如果登录较快,很可能驱动还没有加载就登录进去了;SERVICE_DEMAND_START(3)是在需要的时候动态加载;SERVICE_DISABLED(4)是不加载,要加载之前必须把Start值改为小于4的值。
SERVICE_BOOT_START和SERVICE_SYSTEM_START启动驱动程序时,注册表还没有完全加载,只有HKEY_LOCAL_MACHINE\SYSTEM键可以访问。
想当然的,就把dwStartType设置为SERVICE_AUTO_START,驱动程序安装和加载都正常,但是我重启后发现驱动程序并没有像说明的那样开机启动。也不知道为什么。
然后,我就试着把dwStartType设置为SERVICE_BOOT_START,但是在调用该函数的时候却执行出错了,不解,哪位高人给指点一下啊!
下面是微软给的一个说明:
What Determines When a Driver Is Loaded
What Happens to File Systems During System Boot