📄 tdriver.cpp
字号:
//if you use this code in a mfc program:
//add the header stdafx.h or disable precompiled header
//Unless you do it, when compiling vc will say: Unexpected end
//of file while looking for precompiled header
#include "stdafx.h"
#include "TDriver.h"
//Constructor. Initialize variables.
TDriver::TDriver(void)
{
driverHandle = NULL;
removable = TRUE;
driverName = NULL;
driverPath = NULL;
driverDosName = NULL;
initialized = FALSE;
loaded = FALSE;
started = FALSE;
}
//Destructor. Free resources and unload the driver.
TDriver::~TDriver(void)
{
if (removable){
if(driverHandle != NULL)
{
CloseHandle(driverHandle);
driverHandle = NULL;
}
UnloadDriver();
}
}
//If removable = TRUE, the driver isnt unload when exit
void TDriver::SetRemovable(BOOL value)
{
removable = value;
}
//is driver initialized?
BOOL TDriver::IsInitialized(void)
{
return initialized;
}
//is driver loaded?
BOOL TDriver::IsLoaded(void)
{
return loaded;
}
//is driver started?
BOOL TDriver::IsStarted(void)
{
return started;
}
//Init the driver class variables
DWORD TDriver::InitDriver(LPCTSTR path)
{
//if already initialized, first unload
if(initialized)
{
if(UnloadDriver() != DRV_SUCCESS)
return DRV_ERROR_ALREADY_INITIALIZED;
}
//if yes, i analized the path to extract driver name
driverPath = (LPTSTR)malloc(strlen(path) + 1);
if(driverPath == NULL)
return DRV_ERROR_MEMORY;
strcpy(driverPath, path);
//first i search the last backslash
LPTSTR sPos1 = strrchr(driverPath, (int)'\\');
//if null, the string havent any backslash
if (sPos1 == NULL)
sPos1 = driverPath;
//now, i search .sys
LPTSTR sPos2 = strrchr(sPos1, (int)'.');
if (sPos2 == NULL || sPos1 > sPos2)
{
free(driverPath);
driverPath = NULL;
return DRV_ERROR_INVALID_PATH_OR_FILE;
}
//extract the driver name
driverName = (LPTSTR) malloc (sPos2 - sPos1);
if(driverName == NULL)
{
free(driverPath);
driverPath = NULL;
return DRV_ERROR_MEMORY;
}
memcpy(driverName, sPos1 + 1, sPos2 - sPos1 - 1);
driverName[sPos2 - sPos1 - 1] = 0;
//driverDosName = \\.\driverName
driverDosName = (LPTSTR) malloc (strlen(driverName) + 5);
if(driverDosName == NULL)
{
free(driverPath);
driverPath = NULL;
free(driverName);
driverName = NULL;
return DRV_ERROR_MEMORY;
}
sprintf(driverDosName, "\\\\.\\%s", driverName);
initialized = TRUE;
return DRV_SUCCESS;
}
//Init the driver class variables
DWORD TDriver::InitDriver(LPCTSTR name, LPCTSTR path, LPCTSTR dosName)
{
//if already initialized, first unload
if(initialized)
{
if(UnloadDriver() != DRV_SUCCESS)
return DRV_ERROR_ALREADY_INITIALIZED;
}
LPTSTR dirBuffer;
//if the user introduced path, first i will ckeck it
if (path != NULL)
{
//if yes, copy in auxiliar buffer and continue
DWORD len = (DWORD)(strlen(name) + strlen(path) + 1);
dirBuffer = (LPTSTR) malloc (len);
if(dirBuffer == NULL)
return DRV_ERROR_MEMORY;
strcpy(dirBuffer, path);
}
else
{
//if the user dont introduced path, i search in curren directory
LPTSTR pathBuffer;
DWORD len = GetCurrentDirectory(0, NULL);
pathBuffer = (LPTSTR) malloc (len);
if(pathBuffer == NULL)
return DRV_ERROR_MEMORY;
if (GetCurrentDirectory(len, pathBuffer) != 0)
{
len = (DWORD)(strlen(pathBuffer) + strlen(name) + 6);
dirBuffer = (LPTSTR) malloc (len);
if(dirBuffer == NULL)
{
free(pathBuffer);
return DRV_ERROR_MEMORY;
}
//complete de total path, currentdirectory\driverName.sys
sprintf(dirBuffer, "%s\\%s.sys", pathBuffer, name);
//exists this file?
if(GetFileAttributes(dirBuffer) == 0xFFFFFFFF)
{
free(pathBuffer);
free(dirBuffer);
//if no, i search in \system32\drivers\
LPCTSTR sysDriver = "\\system32\\Drivers\\";
LPTSTR sysPath;
//i have to get the windows directory
DWORD len = GetWindowsDirectory(NULL, 0);
sysPath = (LPTSTR) malloc (len + strlen(sysDriver));
if(sysPath == NULL)
return DRV_ERROR_MEMORY;
if (GetWindowsDirectory(sysPath, len) == 0)
{
free(sysPath);
return DRV_ERROR_UNKNOWN;
}
//complete the path and check it
strcat(sysPath, sysDriver);
len = (DWORD)(strlen(sysPath) + strlen(name) + 5);
dirBuffer = (LPTSTR) malloc (len);
if(dirBuffer == NULL)
return DRV_ERROR_MEMORY;
sprintf(dirBuffer, "%s%s.sys", sysPath, name);
free(sysPath);
//if the file neither exist, i dont know where is it -> i dont initialize
if(GetFileAttributes(dirBuffer) == 0xFFFFFFFF)
{
free(dirBuffer);
return DRV_ERROR_INVALID_PATH_OR_FILE;
}
}
}
else
{
free(pathBuffer);
return DRV_ERROR_UNKNOWN;
}
}
//Write driver's variables with obtained data
driverPath = dirBuffer;
driverName = (LPTSTR)malloc(strlen(name) + 1);
if(driverName == NULL)
{
free(driverPath);
driverPath = NULL;
return DRV_ERROR_MEMORY;
}
strcpy(driverName, name);
LPCTSTR auxBuffer;
if(dosName != NULL)
auxBuffer = dosName;
else
auxBuffer = name;
//dosName=\\.\driverName
if(auxBuffer[0] != '\\' && auxBuffer[1] != '\\')
{
driverDosName = (LPTSTR) malloc (strlen(auxBuffer) + 5);
if(driverDosName == NULL)
{
free(driverPath);
driverPath = NULL;
free(driverName);
driverName = NULL;
return DRV_ERROR_MEMORY;
}
sprintf(driverDosName, "\\\\.\\%s", auxBuffer);
}
else
{
driverDosName = (LPTSTR) malloc (strlen(auxBuffer));
if(driverDosName == NULL)
{
free(driverPath);
driverPath = NULL;
free(driverName);
driverName = NULL;
return DRV_ERROR_MEMORY;
}
strcpy(driverDosName, auxBuffer);
}
//set the state to initialized
initialized = TRUE;
return DRV_SUCCESS;
}
DWORD TDriver::ReLoadDriver(LPCTSTR name, LPCTSTR path, LPCTSTR dosName, BOOL start)
{
//first initialized it
DWORD retCode = InitDriver(name, path, dosName);
//then load
if(retCode == DRV_SUCCESS)
retCode = LoadDriver(start,FALSE);
return retCode;
}
//Function to Load the driver.
DWORD TDriver::LoadDriver(LPCTSTR name, LPCTSTR path, LPCTSTR dosName, BOOL start)
{
//first initialized it
DWORD retCode = InitDriver(name, path, dosName);
//then load
if(retCode == DRV_SUCCESS)
retCode = LoadDriver(start,TRUE);
return retCode;
}
//Function to load the driver
DWORD TDriver::LoadDriver(LPCTSTR path, BOOL start)
{
//first initialized it
DWORD retCode = InitDriver(path);
//then load
if(retCode == DRV_SUCCESS)
retCode = LoadDriver(start,TRUE);
return retCode;
}
//Function to Load the driver
DWORD TDriver::LoadDriver(BOOL start, BOOL isNUW)
{
//if the driver is already started, i havent to do nothing
if(loaded)
return DRV_SUCCESS;
if(!initialized)
return DRV_ERROR_NO_INITIALIZED;
//Open Service manager to create the new "service"
SC_HANDLE SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
DWORD retCode = DRV_SUCCESS;
if (SCManager == NULL)
return DRV_ERROR_SCM;
SC_HANDLE SCService = NULL;
if (isNUW)
{
//Create the driver "service"
SCService = CreateService(SCManager, // SCManager database
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -