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 function GJ_Uninstall_ADL_Driver()
. Code example:
#include "install.h"
void CInstall_ADL_driver_BOOT_STARTDlg::OnBnClickedIntsall()
{
GJ_Install_ADL_Driver_BOOT_START();
}
void CInstall_ADL_driver_BOOT_STARTDlg::OnBnClickedUninstall()
{
GJ_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.
BOOLEAN
SetupDriverName(
__inout_bcount_full(BufferLength) PCHAR DriverLocation,
__in ULONG BufferLength
)
{
HANDLE fileHandle;
DWORD driverLocLen = 0;
GetSystemDirectory(DriverLocation, BufferLength); if (FAILED( StringCbCatA(DriverLocation, BufferLength,
"<a href="file: {
return FALSE;
}
return TRUE;
}
- The key to installing drivers dynamically or installing a driver as a service is the API function
CreateService
.
schService = CreateService(SchSCManager, DriverName, DriverName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_BOOT_START, SERVICE_ERROR_NORMAL, ServiceExe, NULL, NULL, NULL, NULL, NULL );
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. |