驱动加载模版
1 #include "stdafx.h" 2 #include <WINDOWS.H> 3 4 5 #ifndef _LOADNTDRIVER_H_ 6 #define _LOADNTDRIVER_H_ 7 8 9 BOOL InstallDriver( 10 SC_HANDLE SchSCManager, 11 LPCTSTR DriverName, 12 LPCTSTR ServiceExe 13 ); 14 15 BOOL RemoveDriver( 16 SC_HANDLE SchSCManager, 17 LPCTSTR DriverName 18 ); 19 20 BOOL StartDriver( 21 SC_HANDLE SchSCManger, 22 LPCTSTR DriverName 23 ); 24 25 26 BOOL StopDriver( 27 SC_HANDLE SchSCManger, 28 LPCTSTR DriverName 29 ); 30 31 BOOL GetDriverPath( 32 LPSTR DriverLocation 33 ); 34 35 36 37 38 #endif
LoadNTDriver.h
1 // LoadNTDriver.cpp : Defines the entry point for the console application. 2 // 3 4 #include "stdafx.h" 5 #include "LoadNTDriver.h" 6 7 8 /****************************************************************** 9 * InstallDriver 10 * 功能:创建服务、安装驱动 11 * 参数:SchSCManager, 服务控制器句柄 12 * DriverName, 服务器名 13 * ServiceExe, 驱动的可执行程序路径 14 *******************************************************************/ 15 BOOL InstallDriver( 16 SC_HANDLE SchSCManager, 17 LPCTSTR DriverName, 18 LPCTSTR ServiceExe 19 ) 20 { 21 SC_HANDLE schService; 22 DWORD err; 23 24 schService = CreateService( SchSCManager, 25 DriverName, 26 DriverName, 27 SERVICE_ALL_ACCESS, 28 SERVICE_KERNEL_DRIVER, 29 SERVICE_DEMAND_START, 30 SERVICE_ERROR_NORMAL, 31 ServiceExe, 32 NULL, NULL, NULL, NULL, NULL 33 ); 34 if ( NULL == schService ) 35 { 36 //创建服务失败 37 err = GetLastError(); 38 //服务已经存在 39 if ( ERROR_SERVICE_EXISTS == err ) 40 { 41 return TRUE; 42 } 43 else 44 { 45 return FALSE; 46 } 47 } 48 49 if ( schService ) 50 { 51 CloseServiceHandle( schService ); 52 } 53 54 return TRUE; 55 } 56 57 58 /****************************************************************** 59 * RemoveDriver 60 * 功能:删除驱动服务 61 * 参数:SchSCManager, 服务控制器句柄 62 * DriverName, 服务器名 63 *******************************************************************/ 64 BOOL RemoveDriver( 65 SC_HANDLE SchSCManager, 66 LPCTSTR DriverName 67 ) 68 { 69 SC_HANDLE schService; 70 BOOLEAN rCode; 71 72 //打开服务 73 schService = OpenService( SchSCManager, DriverName, SERVICE_ALL_ACCESS ); 74 if ( NULL == schService ) 75 { 76 return FALSE; 77 } 78 79 if ( DeleteService( schService ) ) 80 { 81 rCode = TRUE; 82 } 83 else 84 { 85 rCode = FALSE; 86 } 87 88 if ( schService ) 89 { 90 CloseServiceHandle( schService ); 91 } 92 93 94 return rCode; 95 } 96 97 /****************************************************************** 98 * StartDriver 99 * 功能:启动服务,加载执行驱动 100 * 参数:SchSCManager, 服务控制器句柄 101 * DriverName, 服务器名 102 *******************************************************************/ 103 BOOL StartDriver( 104 SC_HANDLE SchSCManger, 105 LPCTSTR DriverName 106 ) 107 { 108 SC_HANDLE schService; 109 DWORD dwErr; 110 111 schService = OpenService( SchSCManger, DriverName, SERVICE_ALL_ACCESS ); 112 if ( NULL == schService ) 113 { 114 return FALSE; 115 } 116 117 if ( !StartService( schService, 0, NULL ) ) 118 { 119 dwErr = GetLastError(); 120 if ( ERROR_SERVICE_ALREADY_RUNNING == dwErr ) 121 { 122 return TRUE; 123 } 124 } 125 else 126 { 127 return FALSE; 128 } 129 130 if ( schService ) 131 { 132 CloseServiceHandle( schService ); 133 } 134 135 return TRUE; 136 137 } 138 139 140 /****************************************************************** 141 * StartDriver 142 * 功能:启动服务,加载执行驱动 143 * 参数:SchSCManager, 服务控制器句柄 144 * DriverName, 服务器名 145 *******************************************************************/ 146 BOOL StopDriver( 147 SC_HANDLE SchSCManger, 148 LPCTSTR DriverName 149 ) 150 { 151 BOOLEAN rCode; 152 SC_HANDLE schService; 153 SERVICE_STATUS serviceSrarus; 154 155 schService = OpenService( SchSCManger, DriverName, SERVICE_ALL_ACCESS ); 156 if ( NULL == schService ) 157 { 158 return FALSE; 159 } 160 161 if ( ControlService( schService, SERVICE_CONTROL_STOP, &serviceSrarus ) ) 162 { 163 rCode = TRUE; 164 } 165 else 166 { 167 rCode = FALSE; 168 } 169 170 if ( schService ) 171 { 172 CloseServiceHandle( schService ); 173 } 174 175 return rCode; 176 } 177 178 179 /****************************************************************** 180 * GetDriverPath 181 * 功能:获得服务驱动路径 182 * 参数:DriverLocation, 返回驱动的路径 183 *******************************************************************/ 184 BOOL GetDriverPath( 185 LPSTR DriverLocation 186 ) 187 { 188 189 DWORD driverLocLen = 0; 190 191 driverLocLen = GetCurrentDirectory( MAX_PATH, DriverLocation ); 192 if ( !driverLocLen ) 193 { 194 return FALSE; 195 } 196 197 lstrcat( DriverLocation, "\\" ); 198 lstrcat( DriverLocation, DRIVER_NAME ); 199 lstrcat( DriverLocation, ".sys" ); 200 201 202 203 return TRUE; 204 }
LoadNTDriver.cpp