?? 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 + -