📄 installsys.cpp
字号:
// InstallSys.cpp: implementation of the CInstallSys class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "RegIsaIntExe.h"
#include "InstallSys.h"
#include<winioctl.h>
#include <winsvc.h>/*加入该头文件用来支持服务函数*/
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CInstallSys::CInstallSys()
{
char b;
DWORD nByteRead;
m_bConnectInt=FALSE;
m_bInstall=FALSE;
hDevice=NULL;
InterruptEvent=CreateEvent(0,FALSE,FALSE,NULL);
BOOL bResult;
bResult=StartDriver("system32\\drivers\\RegIsaInt.sys", "RegIsaInt");
if(bResult==FALSE)
{
StopDriver("RegIsaInt");
bResult=StartDriver("system32\\drivers\\RegIsaInt.sys", "RegIsaInt");
}
hDevice=OpenDevice("\\\\.\\RegIsaInt");
if(bResult&&hDevice!=INVALID_HANDLE_VALUE)
{
m_bInstall=TRUE;
if(!DeviceIoControl(hDevice,(DWORD)PassDownEvent,&InterruptEvent,sizeof(InterruptEvent),&b,sizeof(CHAR),&nByteRead,NULL))
{
AfxMessageBox("failed to pass down event handle!");
m_bInstall=FALSE;
}
}
else
{
m_bInstall=FALSE;
AfxMessageBox("can not open device,you must restart your machine!");
}
}
CInstallSys::~CInstallSys()
{
if(m_bConnectInt==TRUE)
{
/*断开中断连接*/
DWORD nByteRead;
if(!DeviceIoControl(hDevice,(DWORD)UnRegisterIsaInt,0,0,0,0,&nByteRead,NULL))
{
AfxMessageBox("Failed to disconnect interrupt!");
}
}
CloseHandle(InterruptEvent);
CloseHandle(hDevice);
StopDriver("RegIsaInt");
}
HANDLE CInstallSys::OpenDevice(LPCTSTR lpszDevicePath)
{
HANDLE hLocalDevice;
// 打开设备
hLocalDevice= ::CreateFile(lpszDevicePath, // 设备路径
GENERIC_READ | GENERIC_WRITE, // 读写方式
FILE_SHARE_READ | FILE_SHARE_WRITE, // 共享方式
NULL, // 默认的安全描述符
OPEN_EXISTING, // 创建方式
0, // 不需设置文件属性
NULL); // 不需参照模板文件
return hLocalDevice;
}
BOOL CInstallSys::StartDriver(LPCTSTR lpszDriverPath, LPCTSTR lpszServiceName)
{
SC_HANDLE hSCManager; // 服务控制管理器句柄
SC_HANDLE hService; // 服务句柄
// DWORD dwLastError;
BOOL bResult = FALSE; // 返回值
// 打开服务控制管理器
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(hSCManager)
{
// 创建服务
hService = CreateService(hSCManager,
lpszServiceName,
lpszServiceName,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_NORMAL,
lpszDriverPath,
NULL,
NULL,
NULL,
NULL,
NULL);
if(hService == NULL)
{
if(::GetLastError() == ERROR_SERVICE_EXISTS)
{
//AfxMessageBox("this service has existed");
hService = ::OpenService(hSCManager, lpszServiceName, SERVICE_ALL_ACCESS);
if(!hService)AfxMessageBox("can not open the exist service");
}
else AfxMessageBox("can not create the service");
}
if(hService)
{
// 启动服务
bResult = StartService(hService, 0, NULL);
/* LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
LocalFree( lpMsgBuf );*/
// 关闭服务句柄
CloseServiceHandle(hService);
}
// 关闭服务控制管理器句柄
CloseServiceHandle(hSCManager);
}
return bResult;
}
BOOL CInstallSys::StopDriver(LPCTSTR lpszServiceName)
{
SC_HANDLE hSCManager; // 服务控制管理器句柄
SC_HANDLE hService; // 服务句柄
BOOL bResult; // 返回值
SERVICE_STATUS ServiceStatus;
bResult = FALSE;
// 打开服务控制管理器
hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(hSCManager)
{
// 打开服务
hService = OpenService(hSCManager, lpszServiceName, SERVICE_ALL_ACCESS);
if(hService)
{
// 停止服务
bResult = ControlService(hService, SERVICE_CONTROL_STOP, &ServiceStatus);
// 删除服务
bResult = bResult && DeleteService(hService);
// 关闭服务句柄
CloseServiceHandle(hService);
}
// 关闭服务控制管理器句柄
CloseServiceHandle(hSCManager);
}
return bResult;
}
BOOL CInstallSys::ConnectInterrupt(unsigned short m_IrqNum, BOOL m_bTrigManner)
{
IntPara m_IntPara;
m_IntPara.m_IrqNum=m_IrqNum;
if(m_bTrigManner==TRUE)
m_IntPara.m_TrigManner=1;
else
m_IntPara.m_TrigManner=0;
if(m_bInstall==TRUE)
{
int b=2;
DWORD nByteRead;
if(!DeviceIoControl(hDevice, (DWORD)RegisterIsaInt,
&m_IntPara,sizeof(m_IntPara),&b,sizeof(int),
&nByteRead, NULL))
{
m_bConnectInt=FALSE;
return FALSE;
}
else
{
if(b==1)
{
m_bConnectInt=TRUE;
return TRUE;
}
else
{
m_bConnectInt=FALSE;
return FALSE;
}
}
}
else
{
return FALSE;
}
}
BOOL CInstallSys::DisConnectInt(unsigned short m_IrqNum, BOOL m_bTrigManner)
{
if(m_bInstall==TRUE)/*成功安装了驱动*/
{
DWORD nByteRead;
if(!DeviceIoControl(hDevice,(DWORD)UnRegisterIsaInt,0,0,0,0,&nByteRead,NULL))
{
AfxMessageBox("Failed to disconnect interrupt!");
m_bConnectInt=TRUE;
return FALSE;
}
else
{
m_bConnectInt=FALSE;
return TRUE;
}
}
else
return FALSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -