1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
BOOL LoadNTDriver(char* lpszDreverName,char * lpszDriverPath) { char szDriverImagePath[256]; GetFullPathName(lpszDriverPath,256,szDriverImagePath,NULL); BOOL bRet = FALSE; SC_HANDLE hServiceMgr = NULL; SC_HANDLE hServiceDDK = NULL; hServiceMgr = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS); if (hServiceMgr == NULL) { printf("OpenSCManger() Faild %d\n",GetLastError()); goto BeforeLeave; } else { printf("OpenSCManger() OK\n"); } hServiceDDK = CreateService(hServiceMgr,lpszDreverName,lpszDreverName,SC_MANAGER_ALL_ACCESS,SERVICE_KERNEL_DRIVER,SERVICE_DEMAND_START ,SERVICE_ERROR_IGNORE,szDriverImagePath,NULL,NULL,NULL,NULL,NULL);
DWORD dwRtn; if (hServiceDDK == NULL) { dwRtn = GetLastError(); if (dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS) { printf("CreateService失败,错误号:%d\n",dwRtn); goto BeforeLeave; } else { printf("服务之前已经创建过了,正在打开之前的服务!\n"); hServiceDDK = OpenService(hServiceMgr,lpszDreverName,SERVICE_ALL_ACCESS); if (hServiceDDK == NULL) { printf("打开服务出错 错误码:%d",GetLastError()); goto BeforeLeave; } else { printf("打开服务成功"); } } }else { printf("CreateService OK\n"); } bRet = StartService(hServiceDDK,NULL,NULL); if (!bRet) { DWORD dwRtn = GetLastError(); if (dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING) { printf("StartService faild %d",dwRtn); bRet = FALSE; goto BeforeLeave; } else { if (dwRtn == ERROR_IO_PENDING) { printf("设备已经被挂起"); bRet = FALSE; goto BeforeLeave; } else { printf("服务之前已经启动了! ERROR_SERVICE_ALREADY_RUNNING"); bRet = TRUE; goto BeforeLeave; } } } bRet = TRUE; BeforeLeave: if (hServiceMgr) CloseServiceHandle(hServiceMgr); if(hServiceDDK) CloseServiceHandle(hServiceDDK); return bRet; }
|