Это часть класса для загрузки драйвера:
| Код |
class DrvInstallException: public std::exception { public: DrvInstallException(unsigned int lastError, const char* message): std::exception(message) { this->lastError = lastError; } unsigned int GetErrorCode() const { return lastError; } private: unsigned int lastError; };
class DriverHelper { public: DriverHelper(const wchar_t* drivername, const wchar_t* driverpath); ~DriverHelper(); private: void Install(); void Start(); void Stop(); void Remove(); void Cleanup(); SC_HANDLE schSCManager; SC_HANDLE schService; const wchar_t* drvname; const wchar_t* drvpath; bool createdbyme; };
DriverHelper::DriverHelper(const wchar_t* drivername, const wchar_t* driverpath): drvname(drivername), drvpath(driverpath) { schSCManager = OpenSCManager(0,0,SC_MANAGER_ALL_ACCESS ); if (schSCManager == NULL) throw DrvInstallException(GetLastError(),"Failed to open SC manager."); createdbyme = true; Install(); Start(); }
void DriverHelper::Install() { schService = CreateService(schSCManager, drvname, drvname, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, drvpath, 0, 0, 0, 0, 0); if (schService == NULL) { unsigned int lerr = GetLastError(); if (lerr != ERROR_SERVICE_EXISTS) { Cleanup(); throw DrvInstallException(lerr, "Service starting failed"); } else { schService = OpenService(schSCManager,drvname,SERVICE_ALL_ACCESS); createdbyme = false; if (schService == NULL) throw DrvInstallException(lerr, "Can't open existing driver."); } } }
void DriverHelper::Start() { BOOL res = StartServiceW(schService, 0, 0); if (res != TRUE) { unsigned int lerr = GetLastError(); if (lerr != ERROR_SERVICE_ALREADY_RUNNING) throw DrvInstallException(lerr, "Starting driver failed"); } }
|
В коде :
| Код | DriverHelper drvHelper(L"mydriver", L"c:\\mydriver.sys");
|
|