unit DriverFuncs;
interface
uses winsvc, Classes, SysUtils, Variants, Windows, driverConSTS;
function InstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
string):
Boolean;
function RemoveDriver(hSCManager: SC_HANDLE; DriverId:
string): Boolean;
function StartDriver(hSCManager: SC_HANDLE; DriverId:
string): Boolean;
function StopDriver(hSCManager: SC_HANDLE; DriverId:
string): Boolean;
function SystemInstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
string): Boolean;
function IsSystemInstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
string): Boolean;
function ManageDriver(DriverId, DriverPath:
string; _Function: Integer):
Boolean;
function OpenDriver():Boolean;
implementation
var
gHandle:THANDLE;
//-----------------------------------------------------------------------------
//
// Open Driver
//
//-----------------------------------------------------------------------------
function OpenDriver():boolean;
begin
gHandle:= CreateFile(Driver_DRIVER_ID,
GENERIC_READ
or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0
);
if gHandle = INVALID_HANDLE_VALUE
then
begin
result:=FALSE;
end
else
result:=TRUE;
end;
//-----------------------------------------------------------------------------
//
// Manage Driver
//
//-----------------------------------------------------------------------------
function ManageDriver(DriverId, DriverPath:
string; _Function: Integer):
Boolean;
var
hSCManager: SC_HANDLE;
rCode: Boolean;
error: Dword;
begin
hSCManager := 0;
rCode := False;
error := NO_ERROR;
if (DriverId = '
')
and (DriverPath = '
')
then
begin
result := FALSE;
end;
hSCManager := OpenSCManager(
nil,
nil, SC_MANAGER_ALL_ACCESS);
if hSCManager = 0
then
begin
result := FALSE;
end;
if _Function=OLS_DRIVER_INSTALL
then begin
if InstallDriver(hSCManager, DriverId, DriverPath)
then
rCode := rCode = StartDriver(hSCManager, DriverId);
end;
if _Function=OLS_DRIVER_REMOVE
then begin
if IsSystemInstallDriver(hSCManager, DriverId,
DriverPath)=false
then
begin
StopDriver(hSCManager, DriverId);
rCode := RemoveDriver(hSCManager, DriverId);
end;
end;
if _Function=OLS_DRIVER_SYSTEM_INSTALL
then begin
if IsSystemInstallDriver(hSCManager, DriverId, DriverPath)
then
rCode := True
else
begin
if OpenDriver()=false
then
begin
StopDriver(hSCManager, DriverId);
RemoveDriver(hSCManager, DriverId);
if InstallDriver(hSCManager, DriverId, DriverPath)
then
begin
StartDriver(hSCManager, DriverId);
end;
OpenDriver();
end;
rCode := SystemInstallDriver(hSCManager, DriverId, DriverPath);
end;
end;
if _Function=OLS_DRIVER_SYSTEM_UNINSTALL
then begin
if IsSystemInstallDriver(hSCManager, DriverId, DriverPath)=false
then
rCode:=True
else begin
if not gHandle=INVALID_HANDLE_VALUE
then begin
CloseHandle(gHandle);
gHandle:=INVALID_HANDLE_VALUE;
end;
if StopDriver(hSCManager, DriverId)
then begin
rCode:=RemoveDriver(hSCManager,DriverId);
end;
if not hSCManager=0
then begin
CloseServiceHandle(hSCManager);
end;
result:=rCode;
end;
end;
end;
//-----------------------------------------------------------------------------
//
// Install Driver
//
//-----------------------------------------------------------------------------
function InstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
string):Boolean;
var
hService: SC_HANDLE;
rCode: Boolean;
error: Dword;
begin
hService := 0;
rCode := False;
error := NO_ERROR;
hService:= CreateService(hSCManager,
pchar(DriverId),
pchar(DriverId),
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
pchar(DriverPath),
Nil,
Nil,
Nil,
Nil,
Nil);
if hService= 0
then
error:= GetLastError();
if error=ERROR_SERVICE_EXISTS
then
rCode:= TRUE
else
rCode:= TRUE;
CloseServiceHandle(hService);
result:=rCode;
end;
//-----------------------------------------------------------------------------
//
// System Install Driver
//
//-----------------------------------------------------------------------------
function SystemInstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
string): Boolean;
var
hService:SC_HANDLE;
rCode:boolean;
begin
hService:=0;
rCode:=False;
hService:=OpenService(hSCManager, pchar(DriverId), SERVICE_ALL_ACCESS);
if not hService= 0
then begin
rCode:= ChangeServiceConfig(hService,
SERVICE_KERNEL_DRIVER,
SERVICE_AUTO_START,
SERVICE_ERROR_NORMAL,
pchar(DriverPath),
Nil,
Nil,
Nil,
Nil,
Nil,
Nil
);
CloseServiceHandle(hService);
end;
result:=rCode;
end;
//-----------------------------------------------------------------------------
//
// Remove Driver
//
//-----------------------------------------------------------------------------
function RemoveDriver(hSCManager: SC_HANDLE; DriverId:
string): Boolean;
var
hService:SC_HANDLE;
rCode:Boolean;
begin
hService:=0;
rCode:=False;
hService:= OpenService(hSCManager,pchar(DriverId), SERVICE_ALL_ACCESS);
if hService=0
then begin
rCode:= TRUE
end
else begin
rCode := DeleteService(hService);
CloseServiceHandle(hService);
end;
result:=rCode;
end;
//-----------------------------------------------------------------------------
//
// Start Driver
//
//-----------------------------------------------------------------------------
function StartDriver(hSCManager: SC_HANDLE; DriverId:
string): Boolean;
var
hService:SC_HANDLE;
rCode:Boolean;
error:DWORD;
b:pchar;
begin
b:=nil;
hService:=0;
rCode:=False;
error:=NO_ERROR;
hService:= OpenService(hSCManager, pchar(DriverId), SERVICE_ALL_ACCESS);
if not hService=0
then begin
if StartService(hService, 0, b) = false
then
error:= GetLastError();
if error=ERROR_SERVICE_ALREADY_RUNNING
then
rCode:=True
else begin
rCode:=True;
CloseServiceHandle(hService);
end;
end;
result:=rCode;
end;
//-----------------------------------------------------------------------------
//
// Stop Driver
//
//-----------------------------------------------------------------------------
function StopDriver(hSCManager: SC_HANDLE; DriverId:
string): Boolean;
var
hService:SC_HANDLE;
rCode:Boolean;
error:DWORD;
serviceStatus:_SERVICE_STATUS;
begin
hService:=0;
rCode:=False;
error:=NO_ERROR;
hService:= OpenService(hSCManager, pchar(DriverId), SERVICE_ALL_ACCESS);
if not hService=0
then begin
rCode:= ControlService(hService, SERVICE_CONTROL_STOP,serviceStatus);
error:= GetLastError();
CloseServiceHandle(hService);
end;
result:=rCode;
end;
//-----------------------------------------------------------------------------
//
// IsSystemInstallDriver
//
//-----------------------------------------------------------------------------
function IsSystemInstallDriver(hSCManager: SC_HANDLE; DriverId, DriverPath:
string): Boolean;
var
ServiceConfig: PQueryServiceConfig;
hService : SC_HANDLE;
Ss : TServiceStatus;
dw_size:dword;
MemToFree: integer;
begin
result:=true;
exit;
hService := OpenService(hSCManager, PChar(DriverId), SERVICE_ALL_ACCESS);
if not hService = 0
then
begin
Exit;
end;
try
QueryServiceConfig(hService,
nil, 0, dw_size);
// Get Buffer Length
GetMem(ServiceConfig, dw_size + 1);
MemToFree := dw_size + 1;
try
if not QueryServiceConfig(hService, ServiceConfig, dw_size + 1, dw_size)
then // Get Buffer Length
begin
Exit;
end;
finally
FreeMem(ServiceConfig);
end;
if ServiceConfig.dwServiceType=SERVICE_AUTO_START
then
result:=True;
finally
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
end;
end;
end.