头文件
#pragma once #include <windows.h> #include <winsvc.h> #include <tchar.h> class CLoadNtDriver { public: CLoadNtDriver(); ~CLoadNtDriver(); BOOL pathIsFile(CString strPath); BOOL loadNTDriver(CString strDriverPath); BOOL unloadNTDriver(CString strSvrName); void showErrorInfo(UINT nErrCode, UINT nLine, LPCTSTR = _T(""), UINT = 0); CString m_strDvrName; wchar_t m_szServerName[40]; wchar_t m_szShowName[40]; };
源文件
#include "stdafx.h" #include "LoadNtDriver.h" #include <strsafe.h> CLoadNtDriver::CLoadNtDriver() { } CLoadNtDriver::~CLoadNtDriver() { } BOOL CLoadNtDriver::pathIsFile(CString strPath) { // 过滤路径是文件夹的情况 // Code by Lthis if (PathIsDirectory(strPath)) return FALSE; if (PathFileExists(strPath)) return TRUE; return FALSE; } void CLoadNtDriver::showErrorInfo(UINT nErrCode, UINT nLine, LPCTSTR lpFuncName, UINT nType){ LPTSTR lpMsgBuf = NULL; TCHAR szMessage[256] = { 0 }; TCHAR szCaption[32] = { 0 }; FormatMessage(0x1300, NULL, nErrCode, 0x400, (LPTSTR)&lpMsgBuf, 64, NULL); StringCchPrintf(szMessage, 256, _T("Error code:0x%X:%s\n"), nErrCode, lpMsgBuf); StringCchPrintf(szCaption, 32, _T("%s (Error Line:%d)"), lpFuncName, nLine); StringCchCat(szMessage, 256+32+1, szCaption); switch (nType) { case 0: OutputDebugString(szMessage); break; case 1: MessageBox(NULL, szMessage, szCaption, 0); break; default: break; } } //装载NT驱动程序 BOOL CLoadNtDriver::loadNTDriver(CString strDriverPath) { wchar_t szDriverImagePath[256]; CString strDriverImagePath; //得到完整的驱动路径 GetFullPathName(strDriverPath, 256, szDriverImagePath, NULL); strDriverImagePath = szDriverImagePath; if (strDriverImagePath.IsEmpty()){ MessageBoxW(0, L" CLoadDriver::installDriver: 请输入路径名", NULL, MB_OK | MB_ICONWARNING); return FALSE; } if (!pathIsFile(strDriverImagePath)){ strDriverImagePath += L"loadNTDriver:文件不存在"; MessageBoxW(0, strDriverImagePath, NULL, MB_OK | MB_ICONWARNING); return FALSE; } // 提取驱动名 int nPos; nPos = strDriverImagePath.ReverseFind(_T('\\')); m_strDvrName = strDriverImagePath.Right(nPos - 1); nPos = m_strDvrName.ReverseFind(_T('.')); m_strDvrName = m_strDvrName.Left(nPos); swprintf_s(m_szServerName, m_strDvrName + L"_ServerName"); swprintf_s(m_szShowName, m_strDvrName + L"_ShowName"); 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失败 showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver"); bRet = FALSE; goto BeforeLeave; } else{ //OpenSCManager成功 showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()"); } //创建驱动所对应的服务 hServiceDDK = CreateService(hServiceMgr, m_szServerName, //驱动程序的在注册表中的名字 m_szShowName, // 注册表驱动程序的 DisplayName 值 SERVICE_ALL_ACCESS, // 加载驱动程序的访问权限 SERVICE_KERNEL_DRIVER, // 表示加载的服务是驱动程序 SERVICE_DEMAND_START, // 注册表驱动程序的 Start 值 SERVICE_ERROR_IGNORE, // 注册表驱动程序的 ErrorControl 值 strDriverImagePath, // 注册表驱动程序的 ImagePath 值 NULL, NULL, NULL, NULL, NULL); DWORD dwRtn; //判断服务是否失败 if (hServiceDDK == NULL) { dwRtn = GetLastError(); if (dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS) { //由于其他原因创建服务失败 //printf("CrateService() Faild %d ! \n", dwRtn); showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()"); bRet = FALSE; goto BeforeLeave; } else { //服务创建失败,是由于服务已经创立过 //printf("CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \n"); showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()"); } // 驱动程序已经加载,只需要打开 hServiceDDK = OpenService(hServiceMgr, m_szServerName, SERVICE_ALL_ACCESS); if (hServiceDDK == NULL) { //如果打开服务也失败,则意味错误 dwRtn = GetLastError(); printf("OpenService() Faild %d ! \n", dwRtn); bRet = FALSE; goto BeforeLeave; } } //开启此项服务 bRet = StartService(hServiceDDK, NULL, NULL); if (!bRet) { DWORD dwRtn = GetLastError(); if (dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING) { //printf("StartService() Faild %d ! \n", dwRtn); showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()"); bRet = FALSE; goto BeforeLeave; } else { if (dwRtn == ERROR_IO_PENDING) { //设备被挂住 //printf("StartService() Faild ERROR_IO_PENDING ! \n"); showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()"); bRet = FALSE; goto BeforeLeave; } else { //服务已经开启 //printf("StartService() Faild ERROR_SERVICE_ALREADY_RUNNING ! \n"); showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()"); bRet = TRUE; goto BeforeLeave; } } } bRet = TRUE; //离开前关闭句柄 BeforeLeave: if (hServiceDDK){ CloseServiceHandle(hServiceDDK); } if (hServiceMgr){ CloseServiceHandle(hServiceMgr); } return bRet; } /**************************************************************** // FunctionName: unloadNTDriver // Function : 卸载NT驱动 // Parameter: strSvrName,驱动服务名,类中m_szServerName存储, 传递该值即可 // Author: 张帆 // Create: 2015-3-23 20:57:50 // Checked: Lthis 2015-3-23 ****************************************************************/ BOOL CLoadNtDriver::unloadNTDriver(CString strSvrName) { BOOL bRet = FALSE; SC_HANDLE hServiceMgr = NULL;//SCM管理器的句柄 SC_HANDLE hServiceDDK = NULL;//NT驱动程序的服务句柄 SERVICE_STATUS SvrSta; CString strError; //打开SCM管理器 hServiceMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hServiceMgr == NULL) { //打开SCM管理器失败 //"unloadNTDriver -> OpenSCManager() Faild ! \n"; showErrorInfo(GetLastError(), __LINE__, L"unloadNTDriver()"); bRet = FALSE; goto BeforeLeave; } //打开驱动所对应的服务 hServiceDDK = OpenService(hServiceMgr, strSvrName, SERVICE_ALL_ACCESS); if (hServiceDDK == NULL){ //打开驱动所对应的服务失败 //strError = L"unloadNTDriver -> OpenService() Faild ! !"; showErrorInfo(GetLastError(), __LINE__, L"unloadNTDriver()"); goto BeforeLeave; } //停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。 if (!ControlService(hServiceDDK, SERVICE_CONTROL_STOP, &SvrSta)) { //printf("ControlService() Faild %d !\n", GetLastError()); showErrorInfo(GetLastError(), __LINE__, L"unloadNTDriver()"); } //动态卸载驱动程序。 if (!DeleteService(hServiceDDK)) { //卸载失败 //printf("DeleteSrevice() Faild %d !\n", GetLastError()); showErrorInfo(GetLastError(), __LINE__, L"unloadNTDriver()"); } bRet = TRUE; BeforeLeave: //离开前关闭打开的句柄 if (hServiceDDK){ CloseServiceHandle(hServiceDDK); } if (hServiceMgr){ CloseServiceHandle(hServiceMgr); } return bRet; }