VB 驱动控制类
来源:互联网 发布:淘宝运动服套装 编辑:程序博客网 时间:2024/05/29 16:09
- Option Explicit
- Private Const STANDARD_RIGHTS_REQUIRED =
- Private Const SC_MANAGER_CONNECT =
- Private Const SC_MANAGER_CREATE_SERVICE =
- Private Const SC_MANAGER_ENUMERATE_SERVICE =
- Private Const SC_MANAGER_LOCK =
- Private Const SC_MANAGER_QUERY_LOCK_STATUS =
- Private Const SC_MANAGER_MODIFY_BOOT_CONFIG =
- Private Const SC_MANAGER_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
- SC_MANAGER_CONNECT Or _
- SC_MANAGER_CREATE_SERVICE Or _
- SC_MANAGER_ENUMERATE_SERVICE Or _
- SC_MANAGER_LOCK Or _
- SC_MANAGER_QUERY_LOCK_STATUS Or _
- SC_MANAGER_MODIFY_BOOT_CONFIG)
- Private Const SERVICE_KERNEL_DRIVER = 1
- Private Type SERVICE_STATUS
- dwCurrentState As Long
- dwServiceType As Long
- dwControlsAccepted As Long
- dwWin32ExitCode As Long
- dwServiceSpecificExitCode As Long
- dwCheckPoint As Long
- dwWaitHint As Long
- End Type
- Private Const SERVICE_AUTO_START = 2
- Private Const SERVICE_DEMAND_START = 3
- Private Const SERVICE_ERROR_NORMAL = 1
- Private Const SERVICE_CONTROL_STOP =
- Private Const SERVICE_CONTROL_PAUSE =
- Private Const SERVICE_CONTROL_CONTINUE =
- Private Const SERVICE_CONTROL_INTERROGATE =
- Private Const SERVICE_CONTROL_SHUTDOWN =
- Private Const SERVICE_QUERY_CONFIG = &H1
- Private Const SERVICE_CHANGE_CONFIG = &H2
- Private Const SERVICE_QUERY_STATUS = &H4
- Private Const SERVICE_ENUMERATE_DEPENDENTS = &H8
- Private Const SERVICE_START = &H10
- Private Const SERVICE_STOP = &H20
- Private Const SERVICE_PAUSE_CONTINUE = &H40
- Private Const SERVICE_INTERROGATE = &H80
- Private Const SERVICE_USER_DEFINED_CONTROL = &H100
- Private Const SERVICE_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED Or _
- SERVICE_QUERY_CONFIG Or _
- SERVICE_CHANGE_CONFIG Or _
- SERVICE_QUERY_STATUS Or _
- SERVICE_ENUMERATE_DEPENDENTS Or _
- SERVICE_START Or _
- SERVICE_STOP Or _
- SERVICE_PAUSE_CONTINUE Or _
- SERVICE_INTERROGATE Or _
- SERVICE_USER_DEFINED_CONTROL)
- Private Declare Function OpenSCManager _
- Lib "advapi32" Alias "OpenSCManagerW" _
- (ByVal lpMachineName As Long, ByVal lpDatabaseName As Long, _
- ByVal dwDesiredAccess As Long) As Long
-
- Private Declare Function CreateService _
- Lib "advapi32" Alias "CreateServiceW" _
- (ByVal hSCManager As Long, ByVal lpServiceName As Long, _
- ByVal lpDisplayName As Long, ByVal dwDesiredAccess As Long, _
- ByVal dwServiceType As Long, ByVal dwStartType As Long, _
- ByVal dwErrorControl As Long, ByVal lpBinaryPathName As Long, _
- ByVal lpLoadOrderGroup As Long, ByVal lpdwTagId As Long, _
- ByVal lpDependencies As Long, ByVal lpServiceStartName As Long, _
- ByVal lpPassword As Long) As Long
-
- Private Declare Function DeleteService _
- Lib "advapi32" (ByVal hService As Long) As Long
-
- Private Declare Function CloseServiceHandle _
- Lib "advapi32" (ByVal hSCObject As Long) As Long
-
- Private Declare Function OpenService _
- Lib "advapi32" Alias "OpenServiceW" _
- (ByVal hSCManager As Long, ByVal lpServiceName As Long, _
- ByVal dwDesiredAccess As Long) As Long
-
- Private Declare Function StartService Lib "advapi32" _
- Alias "StartServiceW" (ByVal hService As Long, _
- ByVal dwNumServiceArgs As Long, ByVal lpServiceArgVectors As Long) As Long
-
- Private Declare Function ChangeServiceConfig2 Lib "advapi32" Alias "ChangeServiceConfig2W" (ByVal hService As Long, _
- ByVal dwInfoLevel As Long, lpInfo As Any) As Long
-
- Private Declare Function ControlService Lib "advapi32" _
- (ByVal hService As Long, ByVal dwControl As Long, _
- lpServiceStatus As SERVICE_STATUS) As Long
-
- Private Const OPEN_EXISTING As Long = 3
- Private Const GENERIC_READ As Long =
- Private Const GENERIC_WRITE As Long =
- Private Const FILE_ATTRIBUTE_NORMAL =
- Private Const FILE_DEVICE_UNKNOWN As Long =
- Private Const FILE_SHARE_READ =
- Private Const FILE_SHARE_WRITE =
- Private Declare Function CreateFile Lib "KERNEL32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
- Private Declare Function CloseHandle Lib "KERNEL32.DLL" (ByVal hObject As Long) As Long
- Public Declare Function DeviceIoControl Lib "KERNEL32.DLL" (ByVal hDevice As Long, ByVal dwIoControlCode As Long, ByRef lpInBuffer As Any, ByVal nInBufferSize As Long, ByRef lpOutBuffer As Any, ByVal nOutBufferSize As Long, ByRef lpBytesReturned As Long, ByRef lpOverlapped As Any) As Long
- Private mstrDriverName As String
- Private mstrDisplayName As String
- Private mhManager As Long
- Private mhDriver As Long
- Private mblnShare As Boolean
- Public Function CHLoadDriver(ByVal lpDriverPath As String) As Boolean
- Dim hService As Long
- Dim blnOk As Boolean
- mhManager = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS)
- If mhManager = 0 Then Exit Function
- hService = CreateService(mhManager, _
- StrPtr(mstrDriverName), _
- StrPtr(mstrDisplayName), _
- SERVICE_ALL_ACCESS, _
- SERVICE_KERNEL_DRIVER, _
- SERVICE_DEMAND_START, _
- SERVICE_ERROR_NORMAL, _
- StrPtr(lpDriverPath), _
- 0, _
- 0, _
- 0, _
- 0, _
- 0)
- If hService Then
- If StartService(hService, 0, 0) Then
- blnOk = True
- End If
- Call CloseServiceHandle(hService)
- Else
- hService = OpenService(mhManager, StrPtr(mstrDriverName), SERVICE_START)
- If hService Then
- If StartService(hService, 0, 0) Then
- blnOk = True
- End If
- Call CloseServiceHandle(hService)
- End If
- End If
- LoadDriver = blnOk
- End Function
- Public Function CHUnLoadDriver() As Boolean
- Dim objState As SERVICE_STATUS
- Dim hService As Long
- Dim blnOk As Boolean
- hService = OpenService(mhManager, StrPtr(mstrDriverName), SERVICE_ALL_ACCESS)
- If hService Then
- If ControlService(hService, SERVICE_CONTROL_STOP, objState) Then
- If DeleteService(hService) Then
- blnOk = True
- End If
- End If
- Call CloseServiceHandle(hService)
- End If
- Call CloseServiceHandle(mhManager)
- UnLoadDriver = blnOk
- End Function
- Public Function CHDeviceIoControl(ByVal dwIoControlCode As Long, ByVal lpInBuffer As Long, ByVal nInBufferSize As Long, ByVal lpOutBuffer As Long, ByVal nOutBufferSize As Long, ByRef lpBytesReturned As Long) As Boolean
- CHDeviceIoControl = DeviceIoControl(mhDriver, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, ByVal 0&)
- End Function
- Public Property Get DriverHandle() As Long
- If mhDriver = 0 Then
- If mblnShare Then
- mhDriver = CreateFile("//./" & mstrDriverName, GENERIC_READ Or GENERIC_WRITE, FILE_SHARE_READ Or FILE_SHARE_WRITE, ByVal 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
- Else
- mhDriver = CreateFile("//./" & mstrDriverName, GENERIC_READ Or GENERIC_WRITE, 0, ByVal 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
- End If
- End If
- DriverHandle = mhDriver
- End Property
- Public Property Get ShareControl() As Boolean
- ShareControl = mblnShare
- End Property
- Public Property Let ShareControl(ByVal New_Value As Boolean)
- mblnShare = New_Value
- End Property
- Public Property Get DriverName() As String
- DriverName = mstrDriverName
- End Property
- Public Property Let DriverName(ByVal New_Value As String)
- mstrDriverName = New_Value
- End Property
- Public Property Get DisplayName() As String
- DriverName = mstrDisplayName
- End Property
- Public Property Let DisplayName(ByVal New_Value As String)
- mstrDisplayName = New_Value
- End Property
- Private Sub Class_Initialize()
- mblnShare = True
- End Sub
- Private Sub Class_Terminate()
- CloseHandle mhDriver
- End Sub