VB 驅動加載類(直接使用NtLoadDriver加載方式)

加載驅動需要有加載驅動的權限

使用前先加載“SE_LOAD_DRIVER_PRIVILEGE”權限

 

  1. Option Explicit
  2. Private Const STATUS_IMAGE_ALREADY_LOADED = 
  3. Private Const HKEY_CLASSES_ROOT = 
  4. Private Const HKEY_CURRENT_USER = 
  5. Private Const HKEY_LOCAL_MACHINE = 
  6. Private Const HKEY_USERS = 
  7. Private Const HKEY_PERFORMANCE_DATA = 
  8. Private Const HKEY_CURRENT_CONFIG = 
  9. Private Const HKEY_DYN_DATA = 
  10. Private Const REG_SZ = 1                            ' 字符串值
  11. Private Const REG_EXPAND_SZ = 2                     ' 可擴充字符串值
  12. Private Const REG_BINARY = 3                        ' 二進制值
  13. Private Const REG_DWORD = 4                         ' DWORD值
  14. Private Const REG_MULTI_SZ = 7
  15. Private Const READ_CONTROL = 
  16. Private Const KEY_QUERY_VALUE = 
  17. Private Const KEY_SET_VALUE = 
  18. Private Const KEY_CREATE_SUB_KEY = 
  19. Private Const KEY_ENUMERATE_SUB_KEYS = 
  20. Private Const KEY_NOTIFY = 
  21. Private Const KEY_CREATE_LINK = 
  22. Private Const KEY_READ = KEY_QUERY_VALUE + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + READ_CONTROL
  23. Private Const KEY_WRITE = KEY_SET_VALUE + KEY_CREATE_SUB_KEY + READ_CONTROL
  24. Private Const KEY_EXECUTE = KEY_READ
  25. Private Const KEY_ALL_ACCESS = KEY_QUERY_VALUE + KEY_SET_VALUE + KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
  26. Private Type UNICODE_STRING
  27.     uLength As Integer
  28.     uMaximumLength As Integer
  29.     pBuffer As Long
  30. End Type
  31. Private Declare Sub RtlInitUnicodeString Lib "ntdll.dll" (DestinationString As Any, ByVal SourceString As Long)
  32. Private Declare Function NtLoadDriver Lib "ntdll.dll" (ByVal DriverServiceName As LongAs Long
  33. Private Declare Function NtUnloadDriver Lib "ntdll.dll" (ByVal DriverServiceName As LongAs Long
  34. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, _
  35.                                                                                     ByVal lpSubKey As String, _
  36.                                                                                     ByVal Reserved As Long, _
  37.                                                                                     ByVal lpClass As String, _
  38.                                                                                     ByVal dwOptions As Long, _
  39.                                                                                     ByVal samDesired As Long, _
  40.                                                                                     lpSecurityAttributes As Any, _
  41.                                                                                     phkResult As Long, _
  42.                                                                                     lpdwDisposition As Long _
  43.                                                                                     ) As Long
  44. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, _
  45.                                                                                   ByVal lpValueName As String, _
  46.                                                                                   ByVal Reserved As Long, _
  47.                                                                                   ByVal dwType As Long, _
  48.                                                                                   lpData As Any, _
  49.                                                                                   ByVal cbData As Long _
  50.                                                                                   ) As Long
  51. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As LongAs Long
  52. Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" (ByVal hKey As Long, _
  53.                                                                             ByVal lpSubKey As String, _
  54.                                                                             phkResult As Long _
  55.                                                                             ) As Long
  56. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, _
  57.                                                                                 ByVal lpSubKey As String _
  58.                                                                                 ) As Long
  59. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  60. '驅動控制相關
  61. Private Const OPEN_EXISTING As Long = 3
  62. Private Const GENERIC_READ As Long = 
  63. Private Const GENERIC_WRITE As Long = 
  64. Private Const FILE_ATTRIBUTE_NORMAL = 
  65. Private Const FILE_DEVICE_UNKNOWN As Long = 
  66. Private Const FILE_SHARE_READ = 
  67. Private Const FILE_SHARE_WRITE = 
  68. Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As StringByVal dwDesiredAccess As LongByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As LongByVal dwFlagsAndAttributes As LongByVal hTemplateFile As LongAs Long
  69. Private Declare Function CloseHandle Lib "KERNEL32.DLL" (ByVal hObject As LongAs Long
  70. Private Declare Function DeviceIoControl Lib "KERNEL32.DLL" (ByVal hDevice As LongByVal dwIoControlCode As LongByRef lpInBuffer As Any, ByVal nInBufferSize As LongByRef lpOutBuffer As Any, ByVal nOutBufferSize As LongByRef lpBytesReturned As LongByRef lpOverlapped As Any) As Long
  71. Private Declare Function lstrlen Lib "kernel32" Alias "lstrlenA" (ByVal lpString As StringAs Long
  72. Private mstrDriverName As String '驅動名稱
  73. Private mstrDisplayName As String
  74. Private mhManager As Long
  75. Private mhDriver As Long
  76. Private mblnShare As Boolean
  77. Public Function CHLoadDriver(ByVal lpDriverPath As StringAs Boolean
  78.     Dim lngSuccess As Long
  79.     Dim hKey As Long
  80.     Dim DriverPath As UNICODE_STRING
  81.     lpDriverPath = "/??/" & lpDriverPath
  82.     lngSuccess = RegCreateKeyEx(HKEY_LOCAL_MACHINE, _
  83.                                 "System/CurrentControlSet/Services/" & mstrDriverName, _
  84.                                 0, _
  85.                                 vbNullString, _
  86.                                 0, _
  87.                                 KEY_ALL_ACCESS, _
  88.                                 ByVal 0&, _
  89.                                 hKey, _
  90.                                 ByVal 0& _
  91.                                 )
  92.     If lngSuccess <> 0 Then
  93.         Exit Function
  94.     End If
  95.     lngSuccess = RegSetValueEx(hKey, _
  96.                                "Type", _
  97.                                0, _
  98.                                REG_DWORD, _
  99.                                1, _
  100.                                4 _
  101.                                )
  102.     If lngSuccess <> 0 Then
  103.         RegCloseKey hKey
  104.         Exit Function
  105.     End If
  106.     lngSuccess = RegSetValueEx(hKey, _
  107.                                "ErrorControl", _
  108.                                0, _
  109.                                REG_DWORD, _
  110.                                1, _
  111.                                4 _
  112.                                )
  113.     If lngSuccess <> 0 Then
  114.         RegCloseKey hKey
  115.         Exit Function
  116.     End If
  117.     lngSuccess = RegSetValueEx(hKey, _
  118.                                "Start", _
  119.                                0, _
  120.                                REG_DWORD, _
  121.                                3, _
  122.                                4 _
  123.                                )
  124.     If lngSuccess <> 0 Then
  125.         RegCloseKey hKey
  126.         Exit Function
  127.     End If
  128.     lngSuccess = RegSetValueEx(hKey, _
  129.                                "ImagePath", _
  130.                                0, _
  131.                                REG_EXPAND_SZ, _
  132.                                ByVal lpDriverPath, _
  133.                                lstrlen(lpDriverPath) _
  134.                                ) 'Len(lpDriverPath) '這裏不能用len也不能用lenb不然有中文目錄時會出錯
  135.     If lngSuccess <> 0 Then
  136.         RegCloseKey hKey
  137.         Exit Function
  138.     End If
  139.     
  140.     RtlInitUnicodeString DriverPath, StrPtr("/Registry/Machine/System/CurrentControlSet/Services/" & mstrDriverName)
  141.     lngSuccess = NtLoadDriver(VarPtr(DriverPath))
  142.     If lngSuccess = STATUS_IMAGE_ALREADY_LOADED Or lngSuccess = 0 Then
  143.         CHLoadDriver = True
  144.     End If
  145.     RegCloseKey hKey
  146. End Function
  147. Public Function CHUnLoadDriver() As Boolean
  148.     Dim hKey As Long
  149.     Dim lngSuccess As Long
  150.     Dim DriverPath As UNICODE_STRING
  151.     RtlInitUnicodeString DriverPath, StrPtr("/Registry/Machine/System/CurrentControlSet/Services/" & mstrDriverName)
  152.     lngSuccess = NtUnloadDriver(VarPtr(DriverPath))
  153. '    lngSuccess = RegOpenKey(HKEY_LOCAL_MACHINE, _
  154. '                          "System/CurrentControlSet/Services/" & mstrDriverName, _
  155. '                          hKey _
  156. '                          )
  157. '    If lngSuccess <> 0 Then
  158. '        Exit Function
  159. '    End If
  160. '    lngSuccess = RegDeleteKey(hKey, "Enum")
  161. '    RegCloseKey hKey
  162. '    lngSuccess = RegOpenKey(HKEY_LOCAL_MACHINE, _
  163. '                          "System/CurrentControlSet/Services", _
  164. '                          hKey _
  165. '                          )
  166. '    If lngSuccess <> 0 Then
  167. '        Exit Function
  168. '    End If
  169. '    lngSuccess = RegDeleteKey(hKey, mstrDriverName)
  170.     lngSuccess = RegDeleteKey(HKEY_LOCAL_MACHINE, "System/CurrentControlSet/Services/" & mstrDriverName & "/Enum")
  171.     If lngSuccess <> 0 Then
  172.         Exit Function
  173.     End If
  174.     lngSuccess = RegDeleteKey(HKEY_LOCAL_MACHINE, "System/CurrentControlSet/Services/" & mstrDriverName)
  175.     CHUnLoadDriver = lngSuccess = 0
  176. End Function
  177. Public Function CHDeviceIoControl(ByVal dwIoControlCode As LongByVal lpInBuffer As LongByVal nInBufferSize As LongByVal lpOutBuffer As LongByVal nOutBufferSize As LongByRef lpBytesReturned As LongAs Boolean
  178.     CHDeviceIoControl = DeviceIoControl(mhDriver, dwIoControlCode, lpInBuffer, nInBufferSize, lpOutBuffer, nOutBufferSize, lpBytesReturned, ByVal 0&)
  179. End Function
  180. Public Property Get DriverHandle() As Long
  181.     Dim dwShareAccess As Long
  182.     If mhDriver = 0 Then
  183.         If mblnShare Then
  184.             dwShareAccess = FILE_SHARE_READ Or FILE_SHARE_WRITE
  185.         End If
  186.         mhDriver = CreateFile("//./" & mstrDriverName, GENERIC_READ Or GENERIC_WRITE, dwShareAccess, ByVal 0&, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)
  187.     End If
  188.     DriverHandle = mhDriver
  189. End Property
  190. 'Private Property Let DriverName(ByVal New_Value As Long)
  191. '    mhDriver = New_Value
  192. 'End Property
  193. Public Property Get ShareControl() As Boolean
  194.     ShareControl = mblnShare
  195. End Property
  196. Public Property Let ShareControl(ByVal New_Value As Boolean)
  197.     mblnShare = New_Value
  198. End Property
  199. Public Property Get DriverName() As String
  200.     DriverName = mstrDriverName
  201. End Property
  202. Public Property Let DriverName(ByVal New_Value As String)
  203.     mstrDriverName = New_Value
  204. End Property
  205. Public Property Get DisplayName() As String
  206.     DriverName = mstrDisplayName
  207. End Property
  208. Public Property Let DisplayName(ByVal New_Value As String)
  209.     mstrDisplayName = New_Value
  210. End Property
  211. Private Sub Class_Initialize()
  212.     mblnShare = True
  213. End Sub
  214. Private Sub Class_Terminate()
  215.     CloseHandle mhDriver
  216. End Sub
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章