不想說什麼,固定模式:spa
//裝載NT驅動程序 BOOL LoadNTDriver(TCHAR* lpDriverName, TCHAR* lpDriverPathName) { BOOL bRet = FALSE; SC_HANDLE hServiceMgr = NULL;//SCM管理器的句柄 SC_HANDLE hServiceDDK = NULL;//NT驅動程序的服務句柄 //打開服務控制管理器 hServiceMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hServiceMgr == NULL) { //OpenSCManager失敗 TRACE("OpenSCManager() Faild %d ! \n", GetLastError()); bRet = FALSE; goto BExit; } else { //OpenSCManager成功 TRACE("OpenSCManager() ok ! \n"); } //建立驅動所對應的服務 hServiceDDK = CreateService(hServiceMgr, lpDriverName, //驅動程序的在註冊表中的名字 lpDriverName, // 註冊表驅動程序的 DisplayName 值 SERVICE_ALL_ACCESS, // 加載驅動程序的訪問權限 SERVICE_KERNEL_DRIVER,// 表示加載的服務是驅動程序 SERVICE_DEMAND_START, // 註冊表驅動程序的 Start 值 SERVICE_ERROR_IGNORE, // 註冊表驅動程序的 ErrorControl 值 lpDriverPathName, // 註冊表驅動程序的 ImagePath 值 NULL, NULL, NULL, NULL, NULL); DWORD dwRtn = 0; //判斷服務是否失敗 if (hServiceDDK == NULL) { dwRtn = GetLastError(); if (dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS) { //因爲其餘緣由建立服務失敗 TRACE("CrateService() 失敗 %d ! \n", dwRtn); bRet = FALSE; goto BExit; } else { //服務建立失敗,是因爲服務已經創立過 TRACE("CrateService() 服務建立失敗,是因爲服務已經創立過 ERROR is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \n"); } // 驅動程序已經加載,只須要打開 hServiceDDK = OpenService(hServiceMgr, lpDriverName, SERVICE_ALL_ACCESS); if (hServiceDDK == NULL) { //若是打開服務也失敗,則意味錯誤 dwRtn = GetLastError(); TRACE("OpenService() 失敗 %d ! \n", dwRtn); bRet = FALSE; goto BExit; } else { TRACE("OpenService() 成功 ! \n"); } } else { TRACE("CrateService() 成功 ! \n"); } //開啓此項服務 bRet = StartService(hServiceDDK, NULL, NULL); if (!bRet) //開啓服務不成功 { dwRtn = GetLastError(); TRACE("StartService() 失敗 服務可能已經開啓%d ! \n", dwRtn); } bRet = TRUE; //離開前關閉句柄 BExit: if (hServiceDDK) { CloseServiceHandle(hServiceDDK); } if (hServiceMgr) { CloseServiceHandle(hServiceMgr); } return bRet; }
//卸載驅動程序 BOOL UnLoadSys(TCHAR * szSvrName) { //必定義所用到的變量 BOOL bRet = FALSE; SC_HANDLE hSCM = NULL;//SCM管理器的句柄,用來存放OpenSCManager的返回值 SC_HANDLE hService = NULL;//NT驅動程序的服務句柄,用來存放OpenService的返回值 SERVICE_STATUS SvrSta; //二打開SCM管理器 hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCM == NULL) { //帶開SCM管理器失敗 TRACE("OpenSCManager() Faild %d ! \n", GetLastError()); bRet = FALSE; goto BeforeLeave; } else { //打開SCM管理器成功 TRACE("OpenSCManager() ok ! \n"); } //三打開驅動所對應的服務 hService = OpenService(hSCM, szSvrName, SERVICE_ALL_ACCESS); if (hService == NULL) { //打開驅動所對應的服務失敗 退出 TRACE("OpenService() Faild %d ! \n", GetLastError()); bRet = FALSE; goto BeforeLeave; } else { TRACE("OpenService() ok ! \n"); //打開驅動所對應的服務 成功 } //四中止驅動程序,若是中止失敗,只有從新啓動才能,再動態加載。 if (!ControlService(hService, SERVICE_CONTROL_STOP, &SvrSta)) { TRACE("用ControlService() 中止驅動程序失敗 錯誤號:%d !\n", GetLastError()); } else { //中止驅動程序成功 TRACE("用ControlService() 中止驅動程序成功 !\n"); } //五動態卸載驅動服務。 if (!DeleteService(hService)) //TRUE//FALSE { //卸載失敗 TRACE("卸載失敗:DeleteSrevice()錯誤號:%d !\n", GetLastError()); } else { //卸載成功 TRACE("卸載成功 !\n"); } bRet = TRUE; //六 離開前關閉打開的句柄 BeforeLeave: if (hService>0) { CloseServiceHandle(hService); } if (hSCM>0) { CloseServiceHandle(hSCM); } return bRet; }
BOOL bRet = TRUE; bRet = LoadNTDriver(_T("eyou_driver"), _T("D:\\xxx\\x64\\PGame.sys")); if (bRet == TRUE){ AfxMessageBox(_T("安裝成功")); } else { AfxMessageBox(_T("安裝失敗")); } UnLoadSys(_T("eyou_driver"));