📄 dev.cpp
字号:
#include "stdafx.h"
#include <windows.h>
#include <winioctl.h>
#include "GPIOCTL.h"
#include "dev.h"
bool IsInitialized = false;
char szDriverPath[MAX_PATH];
bool dev::GetDriverPath()
{
PSTR pszSlash;
if (!GetModuleFileName(GetModuleHandle(NULL), szDriverPath, sizeof(szDriverPath)))
return false;
pszSlash = strrchr(szDriverPath, '\\');
if (pszSlash)
pszSlash[1] = 0;
else
return false;
strcat(szDriverPath, "genport.sys");
return true;
}
bool dev::StartDriver()
{
SC_HANDLE hSCManager;
SC_HANDLE hService;
bool bResult;
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCManager)
{
hService = OpenService(hSCManager, "AthDev", SERVICE_ALL_ACCESS);
CloseServiceHandle(hSCManager);
if (hService)
{
bResult = StartService(hService, 0, NULL) || GetLastError() == ERROR_SERVICE_ALREADY_RUNNING;
CloseServiceHandle(hService);
}
else
return false;
}
else
return false;
return bResult;
}
bool dev::Initialize()
{
bool bResult;
DWORD dwBytesReturned;
hDriver = CreateFile("\\\\.\\AthDev",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
// If the driver is not running, install it
if (hDriver == INVALID_HANDLE_VALUE)
{
GetDriverPath();
bResult = InstallDriver(szDriverPath, true);
if (!bResult)
return false;
bResult = StartDriver();
if (!bResult)
return false;
hDriver = CreateFile("\\\\.\\AthDev",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hDriver == INVALID_HANDLE_VALUE)
return false;
}
IsInitialized = true;
return true;
}
bool dev::StopDriver()
{
SC_HANDLE hSCManager;
SC_HANDLE hService;
SERVICE_STATUS ServiceStatus;
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCManager)
{
hService = OpenService(hSCManager, "AthDev", SERVICE_ALL_ACCESS);
CloseServiceHandle(hSCManager);
else
return false;
}
else
return false;
return true;
}
bool RemoveDriver()
{
SC_HANDLE hSCManager;
SC_HANDLE hService;
bool bResult;
StopDriver();
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCManager)
{
hService = OpenService(hSCManager, "AthDev", SERVICE_ALL_ACCESS);
CloseServiceHandle(hSCManager);
if (hService)
{
bResult = DeleteService(hService);
CloseServiceHandle(hService);
}
else
return false;
}
else
return false;
return bResult;
}
bool InstallDriver(PSTR pszDriverPath, bool IsDemandLoaded)
{
SC_HANDLE hSCManager;
SC_HANDLE hService;
// Remove any previous instance of the driver
RemoveDriver();
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hSCManager)
{
// Install the driver
hService = CreateService(hSCManager,
"AthDev",
"AthDev",
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
(IsDemandLoaded == true) ? SERVICE_DEMAND_START : SERVICE_SYSTEM_START,
SERVICE_ERROR_NORMAL,
pszWinIoDriverPath,
NULL,
NULL,
NULL,
NULL,
NULL);
CloseServiceHandle(hSCManager);
if (hService == NULL)
return false;
}
else
return false;
CloseServiceHandle(hService);
return true;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -