|
|
|
How to install, start, stop and uninstall a driverIn the previous article, we created a simple driver. Now, let's see how we can install it, start it, stop it and uninstall it. In Windows, a driver is considered as a service. So, Installing and starting a driver is a creation and starting of a service. You have to first establish a connection to the Service Control Manager to create, start, stop and delete a service. Opening Service Control ManagerYou can open a connection to the service control manager using OpenSCManager API call with necessary previlages to create, start, stop or delete a service. You can create a new service only form a user account with administrative privilages. SC_HANDLE SchSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); The above statement opens the service control manager with all access rights. The service control handle returned by the function call can be used to create, start, stop or delete a service (a driver). Installing the driver (starting the service)The following function accepts the name of the driver to be installed, the path to the driver sys file and an open handle to the service control manager and installs the driver. bool CdrivermanagerDlg::InstallDriver(CString drivername, CString driverpath, SC_HANDLE servicemanager) The Win32 call CreateService installs the driver. Mainly we have to provide the function with a handle to service control manager, a name for the driver, the path of the driver sys file and the startup type of the driver. We have used SERVICE_DEMAND_START as the startup type. That means the driver has to be started manually. If you need, you can specify to start the driver dirung the boot up of the operating system. But, be careful if you do so. If a bug in your driver makes the syster crash with a blue screen and you installed the driver so that it automatically starts at boot time, you are in a big trouble. If you have not registered a driver unload routine, the situation is worst. If it happend, you will have to start the computer in safe mode with last known good configuration and fix the problem. Starting the driverTo start the driver, you have to open the service control manager as previous. Then, you have to open the driver with OpenService function. The service control handle returned by OpenService can be used to start the driver by StartService function. SC_HANDLE SchSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); The second argument to OpenService function, drivername, is the name we passed to CreateService function when installing the driver. Stoping the driverStoping the driver is similar to starting the driver. You can stop the driver this way only if the driver has registered an unload routine. SERVICE_STATUS serviceStatus; Uninstalling the driverUninstalling the driver is also not much different. SC_HANDLE SchSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); The sample application for this article is a MFC application built with Visual Studio 2008. You will find it useful to install, start, stop and uninstall a driver easily. You can download it here. |