Driver.h
#pragma once
#include "Alastor.h"
namespace Alastor
{
class ALASTOR_API Driver {
public:
Driver();
~Driver();
bool load(
std::string path,
std::string deviceName = "",
std::string symbolName = "",
unsigned long startOptions = SERVICE_DEMAND_START
);
bool unload();
int getControlCode(int function, int deviceType = FILE_DEVICE_UNKNOWN, int method = METHOD_BUFFERED, int access = FILE_ANY_ACCESS);
bool control(int code, void* input, int inputLength, void* output, int outputLength);
std::string lasterror();
private:
std::string m_path;
std::string m_deviceName;
std::string m_symbolName;
std::string m_serviceName;
HANDLE m_deviceHandle;
int m_error;
};
}
Driver.cpp
#include "Driver.h"
#include <Windows.h>
/*
Author:River
*/
namespace Alastor
{
Driver::Driver()
{
m_deviceHandle = NULL;
m_error = 0;
}
Driver::~Driver()
{
}
bool Driver::load(
std::string path,
std::string deviceName,
std::string symbolName,
unsigned long startOptions
)
{
SC_HANDLE hSCManager = OpenSCManagerA(NULL, NULL, startOptions);
if (!hSCManager)
{
m_error = GetLastError();
return false;
}
SC_HANDLE hService = CreateServiceA(hSCManager, m_serviceName.c_str(), m_serviceName.c_str(), SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, startOptions, SERVICE_ERROR_IGNORE, m_path.c_str(), 0, 0, 0, 0, 0);
if (hService == NULL)
{
if (ERROR_SERVICE_EXISTS == GetLastError())
{
hService = OpenServiceA(hSCManager, m_serviceName.c_str(), SERVICE_ALL_ACCESS);
if (hService == NULL)
{
CloseServiceHandle(hSCManager);
m_error = GetLastError();
return false;
}
}
else
{
if (ERROR_SERVICE_MARKED_FOR_DELETE == GetLastError() || ERROR_DUPLICATE_SERVICE_NAME == GetLastError())
{
hService = OpenServiceA(hSCManager, m_serviceName.c_str(), SERVICE_ALL_ACCESS);
if (hService == NULL)
{
CloseServiceHandle(hSCManager);
m_error = GetLastError();
return false;
}
SERVICE_STATUS status;
ControlService(hService, SERVICE_CONTROL_STOP,&status);
DeleteService(hService);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCManager == NULL)
{
m_error = GetLastError();
return false;
}
hService = CreateServiceA(hSCManager, m_serviceName.c_str(), m_serviceName.c_str(), SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, startOptions, SERVICE_ERROR_IGNORE, m_path.c_str(), 0, 0, 0, 0, 0);
if (hService == NULL)
{
CloseServiceHandle(hSCManager);
m_error = GetLastError();
return false;
}
}
else
{
m_path = "";
m_deviceHandle = NULL;
CloseServiceHandle(hSCManager);
m_error = GetLastError();
return false;
}
}
}
if (StartServiceA(hService, NULL, NULL) == 0)
{
if (ERROR_SERVICE_ALREADY_RUNNING != GetLastError())
{
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
m_path = "";
m_error = GetLastError();
return false;
}
}
m_deviceHandle = CreateFileA(("\\\\.\" + m_symbolName).c_str(),GENERIC_READ|GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
if (m_deviceHandle == NULL)
{
SERVICE_STATUS status;
ControlService(hService, SERVICE_CONTROL_STOP, &status);
DeleteService(hService);
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
m_path = "";
m_error = GetLastError();
return false;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
return true;
}
bool Driver::unload()
{
if (m_deviceHandle)
{
CloseHandle(m_deviceHandle);
}
SC_HANDLE hSCManager = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCManager == NULL)
{
m_path = "";
m_deviceHandle = NULL;
m_error = GetLastError();
return false;
}
SC_HANDLE hService = OpenServiceA(hSCManager, m_serviceName.c_str(), SERVICE_ALL_ACCESS);
if (hService == NULL)
{
CloseServiceHandle(hSCManager);
m_path = "";
m_deviceHandle = NULL;
m_error = GetLastError();
return false;
}
SERVICE_STATUS status;
if (ControlService(hService, SERVICE_CONTROL_STOP, &status) == FALSE)
{
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
m_path = "";
m_deviceHandle = NULL;
m_error = GetLastError();
return false;
}
if (DeleteService(hService) == FALSE)
{
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
m_path = "";
m_deviceHandle = NULL;
m_error = GetLastError();
return false;
}
CloseServiceHandle(hService);
CloseServiceHandle(hSCManager);
m_path = "";
m_deviceHandle = NULL;
return false;
}
int Driver::getControlCode(int function, int deviceType, int method, int access)
{
return (deviceType << 2) + (access << 14) + (function << 2) + method;
}
bool Driver::control(int code, void* input, int inputLength, void* output, int outputLength)
{
OVERLAPPED overlapped;
return DeviceIoControl(m_deviceHandle, code, input, inputLength, output, outputLength, NULL, &overlapped)==1;
}
std::string Driver::lasterror()
{
return std::string();
}
}