Saturday, April 2, 2011

[Visual Basic] Load Driver Module

This is Module for Visual Basic where you can Load the driver using this code :)

Save this as LoadDriver.bas  or  anything_file_name.bas



Option Explicit
  
Private Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)
Private Declare Function OpenSCManager Lib "advapi32.dll" Alias "OpenSCManagerA" (ByVal lpMachineName As String, ByVal lpDatabaseName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function OpenService Lib "advapi32.dll" Alias "OpenServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal dwDesiredAccess As Long) As Long
Private Declare Function StartService Lib "advapi32.dll" Alias "StartServiceA" (ByVal hService As Long, ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
Private Declare Function CreateService Lib "advapi32.dll" Alias "CreateServiceA" (ByVal hSCManager As Long, ByVal lpServiceName As String, ByVal lpDisplayName As String, ByVal dwDesiredAccess As Long, ByVal dwServiceType As Long, ByVal dwStartType As Long, ByVal dwErrorControl As Long, ByVal lpBinaryPathName As String, ByVal lpLoadOrderGroup As Long, ByVal lpdwTagId As Long, ByVal lpDependencies As Long, ByVal lp As Long, ByVal lpPassword As Long) As Long
Private Declare Function ControlService Lib "advapi32.dll" (ByVal hService As Long, ByVal dwControl As Long, ByVal lpServiceStatus As Long) As Long
Private Declare Function DeleteService Lib "advapi32.dll" (ByVal hService As Long) As Long
Private Declare Function CloseServiceHandle Lib "advapi32.dll" (ByVal hSCObject As Long) As Long
Private Declare Function QueryServiceStatus Lib "advapi32.dll" (ByVal hService As Long, ByVal lpServiceStatus As Long) As Long

Private Type SERVICE_STATUS
dwServiceType As Long
dwCurrentState As Long
dwControlsAccepted As Long
dwWin32ExitCode As Long
dwServiceSpecificExitCode As Long
dwCheckPoint As Long
dwWaitHint As Long
End Type

Private Const SC_MANAGER_ALL_ACCESS As Long = 983103
Private Const SERVICE_ALL_ACCESS As Long = 983551
Private Const SERVICE_KERNEL_DRIVER As Long = 1
Private Const SERVICE_DEMAND_START As Long = 3
Private Const SERVICE_ERROR_NORMAL As Long = 1
Private Const ERROR_SERVICE_EXISTS As Long = 1073
Private Const ERROR_SERVICE_ALREADY_RUNNING As Long = 1056
Private Const SERVICE_STOPPED As Long = 1
Private Const SERVICE_STOP_PENDING As Long = 3
Private Const SERVICE_RUNNING As Long = 4
Private Const SERVICE_CONTROL_STOP As Long = 1

Public Sub LoadDriver(ByVal DriverName As String, ByVal FilePath As String)
Dim hSCMgr As Long, hService As Long, status As SERVICE_STATUS
hSCMgr = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
Assert hSCMgr <> 0
hService = CreateService(hSCMgr, DriverName, DriverName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, FilePath, 0, 0, 0, 0, 0)
If hService = 0 Then
       Assert Err.LastDllError = ERROR_SERVICE_EXISTS
       hService = OpenService(hSCMgr, DriverName, SERVICE_ALL_ACCESS)
       Assert hService <> 0
End If
If StartService(hService, 0, 0) = 0 Then Assert Err.LastDllError = ERROR_SERVICE_ALREADY_RUNNING
Do
       Sleep 50
       Assert QueryServiceStatus(hService, VarPtr(status)) <> 0
Loop While status.dwCurrentState <> SERVICE_RUNNING
CloseServiceHandle hSCMgr
CloseServiceHandle hService
End Sub

Public Sub UnloadDriver(ByVal DriverName As String)
Dim hSCMgr As Long, hService As Long, status As SERVICE_STATUS
hSCMgr = OpenSCManager(vbNullString, vbNullString, SC_MANAGER_ALL_ACCESS)
Assert hSCMgr <> 0
hService = OpenService(hSCMgr, DriverName, SERVICE_ALL_ACCESS)
If hService = 0 Then Exit Sub
Assert QueryServiceStatus(hService, VarPtr(status)) <> 0
If status.dwCurrentState <> SERVICE_STOPPED And status.dwCurrentState <> SERVICE_STOP_PENDING Then
       Assert ControlService(hService, SERVICE_CONTROL_STOP, VarPtr(status)) <> 0
       Do
         Sleep 50
         Assert QueryServiceStatus(hService, VarPtr(status)) <> 0
       Loop While status.dwCurrentState <> SERVICE_STOPPED
End If
Assert DeleteService(hService) <> 0
CloseServiceHandle hSCMgr
CloseServiceHandle hService
End Sub

Private Sub Assert(ByVal bBool As Boolean)
If Not bBool Then
       MsgBox "Assertion Failed!", vbCritical, "Error"
       End
End If
End Sub

0 comments for "[Visual Basic] Load Driver Module"

Post a Comment

background