The help you needed in C, C++, MFC
Complete tutorials with free source code
PDF Print E-mail

How to install, start, stop and uninstall a driver

In 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 Manager

You 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)
{
DWORD err;

SC_HANDLE schService=CreateService(
servicemanager,         // handle of service control manager database
drivername,             // address of name of service to start
drivername,             // address of display name
SERVICE_ALL_ACCESS,     // type of access to service
SERVICE_KERNEL_DRIVER,  // type of service
SERVICE_DEMAND_START,   // when to start service
SERVICE_ERROR_NORMAL,   // severity if service fails to start
driverpath,             // address of name of binary file
NULL,                   // service does not belong to a group
NULL,                   // no tag requested
NULL,                   // no dependency names
NULL,                   // use LocalSystem account
NULL                    // no password for service account
);

if (schService==NULL)
{
err=GetLastError();
if(err==ERROR_SERVICE_EXISTS)
{
::MessageBox(0, TEXT("Driver alredy installed!"), TEXT("Message."), 0);
return true;
}
else if(err==ERROR_SERVICE_MARKED_FOR_DELETE)
::MessageBox(0, TEXT("Previous instance of the service is not fully deleted. Try again."), TEXT("Error."), 0);else
{
CString errormsg;
errormsg.Format(TEXT("Driver installation failed!\nError code: %d"), err);
::MessageBox(0, errormsg, TEXT("Error."), 0);
}
}
else
{
::MessageBox(0, TEXT("Driver installed."), TEXT("Message."), 0);
CloseServiceHandle(schService);
return true;
}
return false;
}

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 driver

To 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);

SC_HANDLE schService=OpenService(SchSCManager, drivername, SERVICE_ALL_ACCESS);

StartService(schService, 0, NULL);

The second argument to OpenService function, drivername, is the name we passed to CreateService function when installing the driver.

Stoping the driver

Stoping 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;

SC_HANDLE SchSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

SC_HANDLE schService=OpenService(SchSCManager, drivername, SERVICE_ALL_ACCESS);

ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus);

Uninstalling the driver

Uninstalling the driver is also not much different.

SC_HANDLE SchSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);

SC_HANDLE schService=OpenService(SchSCManager, drivername, SERVICE_ALL_ACCESS);

DeleteService(schService);

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.