How to install driver dynamically or install driver as a service for Windows






3.85/5 (7 votes)
Install driver dynamically or install driver as a service for Windows.
Introduction
Usually when we complete a driver compilation, we use the inf file to install the driver. But sometimes we hope to install drivers dynamically or install a driver as a service for Windows. In this article, we assume that the driver has been compiled and used directly. The driver must have 32-bit and 64-bit versions so that we can experiment under 32-bit and 64-bit Operating Systems. How to compile a driver is not the task of this article.
About the path of the driver file which will be installed: usually we may place the installation program and driver file in the same directory. In most cases, it is feasible. But if you want to install the driver as a service for Windows on a 64-bit Operating System environment, it will fail. In this case the directory of the driver file must be "C:\\Windows\\System32\\drivers" so we use "C:\\Windows\\System32\\drivers" instead of the current directory. The driver file is copied to the directory in advance.
Using the code
- Install a driver invoke function
GJ_Install_ADL_Driver_BOOT_START()
. Uninstall a driver invoke functionGJ_Uninstall_ADL_Driver()
. - Setting the directory of the driver file refers to the function
SetupDriverName
. The code for how to set the current path is available in the full source code. - The key to installing drivers dynamically or installing a driver as a service is the API function
CreateService
.
Code example:
//
#include "install.h"
void CInstall_ADL_driver_BOOT_STARTDlg::OnBnClickedIntsall()
{
// TODO: Add your control notification handler code here
//Because if the current OS is 64bit,gjglly.sys must
//copy to "C:\\Windows\\System32\\drivers",
//so we use "C:\\Windows\\System32\\drivers" instead of current directory.
//Refer to function SetupDriverName.
GJ_Install_ADL_Driver_BOOT_START();
}
void CInstall_ADL_driver_BOOT_STARTDlg::OnBnClickedUninstall()
{
// TODO: Add your control notification handler code here
GJ_Uninstall_ADL_Driver();
}//
//
BOOLEAN
SetupDriverName(
__inout_bcount_full(BufferLength) PCHAR DriverLocation,
__in ULONG BufferLength
)
{
HANDLE fileHandle;
DWORD driverLocLen = 0;
//
//
//Because if the current OS is 64bit,gjglly.sys must copy to "C:\\Windows\\System32\\drivers",
//so we use "C:\\Windows\\System32\\drivers" instead of current directory.
//
//
/* //Get the current directory.
GetModuleFileNameA(GetModuleHandle(NULL), DriverLocation, BufferLength);
char* pszSlash = strrchr(DriverLocation, '\\');
pszSlash[0]=0;
*///Get the current directory.
//
GetSystemDirectory(DriverLocation, BufferLength);//"C:\\Windows\\System32\\drivers"
//
//
// Setup path name to driver file.
//
//if (FAILED( StringCbCatA(DriverLocation, BufferLength,
// "<a href="file://%22driver_name%22.sys/">\\"DRIVER_NAME".sys</a>") )) //Get the current directory.
if (FAILED( StringCbCatA(DriverLocation, BufferLength,
"<a href="file://drivers//%22DRIVER_NAME%22.sys">\\drivers\\"DRIVER_NAME".sys</a>") ))
{
return FALSE;
}
//
//Following code will failed in 64bit OS
/*
//
// Insure driver file is in the specified directory.
//
//
//
if ((fileHandle = CreateFileA(DriverLocation,
GENERIC_READ,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
)) == INVALID_HANDLE_VALUE) {
//
//
//printf("%s.sys is not loaded.\n", DRIVER_NAME);
//
//
// Indicate failure.
//
//
return FALSE;
}
//
//
// Close open file handle.
//
//
if (fileHandle) {
CloseHandle(fileHandle);
}
//
// Indicate success.
//
*/
//
return TRUE;
//
} // SetupDriverName
//
//
schService = CreateService(SchSCManager, // 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_BOOT_START, // when to start service !!! SERVICE_DEMAND_START SERVICE_AUTO_START
SERVICE_ERROR_NORMAL, // severity if service fails to start
ServiceExe, // 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
);
//
You can change the sixth parameter "dwStartType
" to set the startup type.
dwStartType [in]
The service start options can be one of the following values:
Value | Meaning |
SERVICE_AUTO_START 0x00000002 |
A service started automatically by the service control manager during system startup. For more information, see Automatically Starting Services. |
SERVICE_BOOT_START 0x00000000 |
A device driver started by the system loader. This value is valid only for driver services. |
SERVICE_DEMAND_START 0x00000003 |
A service started by the service control manager when a process calls
the StartService
function. For more information, see Starting Services on Demand. |
SERVICE_DISABLED 0x00000004 |
A service that cannot be started. Attempts to start the service results in the error code ERROR_SERVICE_DISABLED . |
SERVICE_SYSTEM_START 0x00000001 |
A device driver started by the IoInitSystem function. This value is valid only for driver services. |