|
NT式驱动在操作系统中,被当成一种服务来管理。我们可以在HKLM\system\currentcontrolset\services键下查找到所有安装的NT式驱动的信息,这里也可以看到windows服务的相关信息。安装NT式驱动时,安装程序会在这里建立起相关的信息。如果我们手动地来创建这些记录来安装驱动也是可以的,不熟悉时还是比较麻烦的,网上也有许多操控NT驱动的工具可供下载。这里我提供操控NT驱动的源码,用户可以下下来编译成控制台程序或者改成窗体程序都是可以的。
1 /**/////////////////////////////////////////////////////////////////////////// 2 // file InstallNTDriver.cpp 3 // 4 // Copyright (c) 2010 robert xiao. All Rights Reserved. robert.xiao2010@gmail.com 5 // 6 // Permission to use, copy, modify, and distribute this software and its 7 // documentation for any purpose, without fee, and without a written 8 // agreement, is hereby granted, provided that the above copyright notice, 9 // this paragraph and the following two paragraphs appear in all copies, 10 // modifications, and distributions. 11 // 12 // 13 // IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT, 14 // INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST 15 // PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 16 // EVEN IF THE AUTHOR HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 17 // 18 // THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT 19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 20 // PARTICULAR PURPOSE. THE SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF 21 // ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS". THE AUTHOR HAS NO OBLIGATION 22 // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 23 // 24 /**/////////////////////////////////////////////////////////////////////////// 25 #pragma once 26 #include <windows.h> 27 #include <winsvc.h> 28 #include <conio.h> 29 #include <stdio.h> 30 #include <shlwapi.h> 31 #include <iostream> 32 #include <string> 33 using namespace std; 34 35 #pragma comment(lib, "shlwapi.lib") 36 37 #define WARN_MSG(X) printf X 38 #define ERROR_MSG(X) printf X 39 #define MAX_FNAME_LEN 256 40 /**/////////////////////////////////////////////////////////////////////////// 41 // forward declaration. 42 void Quit() { 43 exit(0); 44 } 45 void InstallMyDriver(); 46 void StartMyDevice(); 47 void PauseMyDevice(); 48 void ResumeMyDevice(); 49 void StopMyDevice(); 50 void UnstallMyDriver(); 51 52 /**/////////////////////////////////////////////////////////////////////////// 53 // global variable and structure 54 55 typedef void (* pf)(); 56 57 enum SERVICE_COMMAND_TYPE {SCT_START, SCT_PAUSE, SCT_CONTINUE, SCT_STOP}; 58 void _controlServiceImpl(char *szName, SERVICE_COMMAND_TYPE t); 59 60 struct { 61 int _nIndex; 62 char _chMsg[100]; 63 pf _fu; 64 }g_works[] = { 65 0, "Install NT Driver", InstallMyDriver, 66 1, "Start Device.", StartMyDevice, 67 2, "Stop Device.", StopMyDevice, 68 3, "UnInstall Driver.", UnstallMyDriver, 69 -1, "Quit.", Quit, 70 }; 71 72 static int g_nAmount = sizeof(g_works)/sizeof(g_works[0]); 73 74 /**/////////////////////////////////////////////////////////////////////////// 75 // message loop 76 77 void PrintHelp() { 78 system("cls"); 79 printf("\nplease select a item from follow items"); 80 for (int i = 0; i < g_nAmount; ++i) { 81 printf("\n%-4d: %s", g_works[i]._nIndex, g_works[i]._chMsg); 82 } 83 printf("\nEnter your input: "); 84 } 85 bool CheckInput() { 86 if(cin.fail()) { 87 cin.clear(); 88 char c; 89 cin >> c; 90 return false; 91 } 92 return true; 93 } 94 95 bool Dispatch(int nSelect) { 96 if(!CheckInput()) 97 return false; 98 99 bool bProduced = false; 100 for (int i = 0; i < g_nAmount; ++i) { 101 if(nSelect == g_works[i]._nIndex) { 102 bProduced = true; 103 g_works[i]._fu(); 104 break; 105 } 106 } 107 return bProduced; 108 } 109 110 void ProcessMessage() { 111 int n; 112 do { 113 PrintHelp(); 114 cin>> n; 115 while (!Dispatch(n)) { 116 printf("\nEnter a valid item eg.(-1):"); 117 cin >> n; 118 } 119 printf("press enter to begin next."); 120 system("pause"); 121 }while(true); 122 } 123 124 /**///////////////////////////////////////////////////////////////////////////125 // Entry point. 126 int main(int argc, char* argv[]) { 127 ProcessMessage(); 128 return EXIT_SUCCESS; 129 } 130 131 /**///////////////////////////////////////////////////////////////////////////132 // implemention. 133 134 bool InputDriverPath(char *szPath, int nLength = MAX_PATH) { 135 if(!szPath) { 136 ERROR_MSG(("InputDriverPath`s argument is invalid")); 137 return false; 138 } 139 string strTemp; 140 do { 141 printf("Driver binary file:"); 142 cin >>strTemp; 143 if(PathFileExistsA(strTemp.c_str())) { 144 break; 145 } 146 printf("You input a invalid file path!\n"); 147 }while (1); 148 149 if(nLength < strTemp.length()) { 150 WARN_MSG(("The buffer for file path is too samll")); 151 } 152 strcpy_s(szPath, nLength, strTemp.c_str()); 153 return true; 154 } 155 156 bool InputDriverName(char *szName, int nLen = MAX_FNAME_LEN) { 157 if(!szName) { 158 ERROR_MSG(("InputDriverName`s argument is invalid")); 159 return false; 160 } 161 string strTemp; 162 do { 163 printf("Driver name:"); 164 cin >> strTemp; 165 if(strTemp.size() > 3) { 166 break; 167 } 168 printf("You input a invalid driver name!\n"); 169 }while (1); 170 171 if(nLen < strTemp.size()) { 172 ERROR_MSG(("The buffer for file path is too samll")); 173 return false; 174 } 175 strcpy_s(szName,nLen, strTemp.c_str()); 176 return true; 177 } 178 179 DWORD _InstallMyDriver(char *szName, char * szImagePath) { 180 // Open scm 181 if(!szName || !szImagePath) { 182 ERROR_MSG(("_InstallMyDriver`parameter is invalid.\n")); 183 return EXIT_FAILURE; 184 } 185 SC_HANDLE hMgr = 0; 186 SC_HANDLE hSev = 0; 187 hMgr = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS); 188 if(!hMgr) { 189 ERROR_MSG(("OpenSCManager() Failed: %d.\n", GetLastError())); 190 return EXIT_FAILURE; 191 } 192 193 // create service. 194 hSev = CreateServiceA(hMgr, 195 szName, 196 szName, 197 SERVICE_ALL_ACCESS, 198 SERVICE_KERNEL_DRIVER, 199 SERVICE_DEMAND_START, 200 SERVICE_ERROR_IGNORE, 201 szImagePath, 202 NULL, 203 NULL, 204 NULL, 205 NULL, 206 NULL); 207 DWORD dwError = EXIT_SUCCESS; 208 if(!hSev) { 209 dwError = GetLastError(); 210 if(dwError != ERROR_IO_PENDING && dwError != ERROR_SERVICE_EXISTS) { 211 ERROR_MSG(("CreateServiceA failed: %d.\n", dwError)); 212 goto clear; 213 } 214 hSev = OpenServiceA(hMgr, szName, SERVICE_ALL_ACCESS); 215 if(!hSev) { 216 ERROR_MSG(("OpenServiceA failed: %d", GetLastError())); 217 goto clear; 218 } 219 dwError = EXIT_SUCCESS; 220 } 221 clear: 222 if(hSev) { 223 CloseServiceHandle(hSev); 224 } 225 if(hMgr) { 226 CloseServiceHandle(hMgr); 227 } 228 return dwError; 229 } 230 void InstallMyDriver() 231  { 232 // Input file path and name. 233 char szImagePath[MAX_PATH] = {'\0'}; 234 if(!InputDriverPath(szImagePath)) { 235 return; 236 } 237 char szName[MAX_FNAME_LEN] = {'\0'}; 238 if(!InputDriverName(szName)) { 239 return; 240 } 241 if(_InstallMyDriver(szName, szImagePath) != EXIT_SUCCESS) { 242 ERROR_MSG(("Install driver failed.\n")); 243 return; 244 } 245 printf("Did you want to start the device?(0: yes):"); 246 int i; 247 cin >> i; 248 if(i == 0) { 249 _controlServiceImpl(szName, SCT_START); 250 } 251 } 252 253 DWORD _UnstallMyDriver(char *szName) { 254 SC_HANDLE hMgr = 0; 255 SC_HANDLE hSev = 0; 256 hMgr = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS); 257 if(!hMgr) { 258 ERROR_MSG(("OpenSCManager() Failed: %d.\n", GetLastError())); 259 return EXIT_FAILURE; 260 } 261 262 DWORD dwError = EXIT_SUCCESS; 263 hSev = OpenServiceA(hMgr, szName, SERVICE_ALL_ACCESS); 264 if(!hSev) { 265 ERROR_MSG(("OpenServiceA failed: %d", GetLastError())); 266 dwError = GetLastError(); 267 goto clear; 268 } 269 270 SERVICE_STATUS sta; 271 ControlService(hSev, SERVICE_CONTROL_STOP, &sta); 272 if(!DeleteService(hSev)) { 273 dwError = GetLastError(); 274 ERROR_MSG(("DeleteService failed:%d", GetLastError())); 275 goto clear; 276 } 277 printf("Uninstall driver successfully.\n"); 278 clear: 279 if(hSev) { 280 CloseServiceHandle(hSev); 281 } 282 if(hMgr) { 283 CloseServiceHandle(hMgr); 284 } 285 return dwError; 286 287 } 288 void UnstallMyDriver() 289  { 290 char szName[MAX_FNAME_LEN] = {'\0'}; 291 if(!InputDriverName(szName)) { 292 return; 293 } 294 _UnstallMyDriver(szName); 295 } 296 297 bool _IsSameState(SERVICE_COMMAND_TYPE t, DWORD dwState) { 298 if(dwState == SERVICE_RUNNING) { 299 if(t == SCT_START || t == SCT_CONTINUE) { 300 return true; 301 } 302 } 303 else if(dwState != SERVICE_RUNNING) { 304 if(t == SCT_STOP) { 305 return true; 306 } 307 } 308 else if(dwState == SERVICE_PAUSED) { 309 if(t == SCT_PAUSE) { 310 return true; 311 } 312 } 313 return false; 314 } 315 void _controlServiceImpl(char *szName, SERVICE_COMMAND_TYPE t) { 316 SC_HANDLE hMgr = 0; 317 SC_HANDLE hSev = 0; 318 hMgr = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS); 319 if(!hMgr) { 320 ERROR_MSG(("OpenSCManager() Failed: %d.\n", GetLastError())); 321 return ; 322 } 323 324 DWORD dwError = EXIT_SUCCESS; 325 hSev = OpenServiceA(hMgr, szName, SERVICE_ALL_ACCESS); 326 if(!hSev) { 327 ERROR_MSG(("OpenServiceA failed: %d", GetLastError())); 328 dwError = GetLastError(); 329 goto clear; 330 } 331 332 SERVICE_STATUS sta; 333 if(!QueryServiceStatus(hSev, &sta)) { 334 ERROR_MSG(("QueryServiceStatus failed: %d", GetLastError())); 335 goto clear; 336 } 337 338 if(!_IsSameState(t, sta.dwCurrentState)) { 339 BOOL bSuccessful = TRUE; 340 switch (t) 341 { 342 case SCT_START: 343 bSuccessful = StartService(hSev, 0, 0); 344 break; 345 case SCT_CONTINUE: 346 bSuccessful = ControlService(hSev, SERVICE_CONTROL_CONTINUE, &sta); 347 break; 348 case SCT_PAUSE: 349 bSuccessful = ControlService(hSev, SERVICE_CONTROL_PAUSE, &sta); 350 break; 351 default: 352 bSuccessful = ControlService(hSev, SERVICE_CONTROL_STOP, &sta); 353 break; 354 } 355 if(!bSuccessful) { 356 ERROR_MSG(("Control Service failed: %d", GetLastError())); 357 } 358 else { 359 printf("Successfully.\n"); 360 } 361 } 362 else { 363 printf("The service`s state is same.\n"); 364 } 365 clear: 366 if(hSev) { 367 CloseServiceHandle(hSev); 368 } 369 if(hMgr) { 370 CloseServiceHandle(hMgr); 371 } 372 return ; 373 } 374 375 void _controlService(SERVICE_COMMAND_TYPE t) { 376 char szName[MAX_FNAME_LEN] = {'\0'}; 377 if(!InputDriverName(szName)) { 378 return; 379 } 380 _controlServiceImpl(szName, t); 381 } 382 void StartMyDevice() 383  { 384 _controlService(SCT_START); 385 } 386 387 void PauseMyDevice() 388  { 389 _controlService(SCT_PAUSE); 390 } 391 392 void ResumeMyDevice() 393  { 394 _controlService(SCT_CONTINUE); 395 } 396 397 void StopMyDevice() 398  { 399 _controlService(SCT_STOP); 400 }
|