loaddriverdlg.cpp
来自「灰狐驱动学习笔记系列 参考 windows驱动开发详解和 楚狂人windows驱」· C++ 代码 · 共 335 行
CPP
335 行
// LoadDriverDlg.cpp : implementation file
//
#include "stdafx.h"
#include "LoadDriver.h"
#include "LoadDriverDlg.h"
#include "Winsvc.h"
#pragma comment(lib,"Advapi32.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLoadDriverDlg dialog
CLoadDriverDlg::CLoadDriverDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLoadDriverDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLoadDriverDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CLoadDriverDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLoadDriverDlg)
DDX_Control(pDX, IDOK, m_btnOk);
DDX_Control(pDX, IDC_EDIT_STATUS, m_edtStatus);
DDX_Control(pDX, IDC_EDIT_FILEPATH, m_edtFilePath);
DDX_Control(pDX, IDC_BUTTON_OPEN, m_btnOpen);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLoadDriverDlg, CDialog)
//{{AFX_MSG_MAP(CLoadDriverDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLoadDriverDlg message handlers
BOOL CLoadDriverDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CLoadDriverDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLoadDriverDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//////////////////////////////////////////////////////////////////////////
//
// 浏览文件 对话框显示
//
//////////////////////////////////////////////////////////////////////////
void CLoadDriverDlg::OnButtonOpen()
{
// 初始化
m_edtFilePath.SetWindowText("");
m_edtStatus.SetWindowText("");
// 浏览文件对话框
CFileDialog dlgFile(TRUE);
if (dlgFile.DoModal())
{
if (dlgFile.GetFileExt() != "sys")
{
m_edtStatus.SetWindowText("对不起,您必须选择一个驱动(sys)文件!");
return;
}
m_edtFilePath.SetWindowText( dlgFile.GetPathName() );
m_edtStatus.SetWindowText( dlgFile.GetFileName() );
}
}
//////////////////////////////////////////////////////////////////////////
//
// 加载/卸载 驱动
//
//////////////////////////////////////////////////////////////////////////
void CLoadDriverDlg::OnOK()
{
char szTitle[10];
char szFileName[MAX_PATH];
char szFilePath[MAX_PATH];
m_btnOk.GetWindowText(szTitle, sizeof(szTitle));
m_edtFilePath.GetWindowText(szFilePath, sizeof(szFilePath));
m_edtStatus.GetWindowText(szFileName, sizeof(szFileName));
if (strcmp(szTitle, "加载") == 0)
{
if (LoadDriver(szFileName, szFilePath))
{
::MessageBox(NULL, "加载驱动SYS成功!", "提示", MB_OK);
m_btnOk.SetWindowText("卸载");
}
}
else
{
if (UnloadDriver(szFileName))
{
::MessageBox(NULL, "卸载驱动服务成功!", "提示", MB_OK);
m_btnOk.SetWindowText("加载");
}
}
}
void CLoadDriverDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
//////////////////////////////////////////////////////////////////////////
//
// 加载驱动 实现函数
//
//////////////////////////////////////////////////////////////////////////
BOOL CLoadDriverDlg::LoadDriver(LPCTSTR lpszDriverName, LPCTSTR lpszDriverPath)
{
char szDriverFilePath[MAX_PATH];
GetFullPathName(lpszDriverPath, MAX_PATH, szDriverFilePath, NULL);
BOOL bRet = TRUE;
DWORD dwReturn;
char szErrorMsg[MAX_PATH];
SC_HANDLE hServiceMgr = NULL;
SC_HANDLE hServiceSys = NULL;
// 打开服务管理器
hServiceMgr = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!hServiceMgr)
{
sprintf(szErrorMsg, "OpenSCManager() Error!");
::MessageBox(NULL, szErrorMsg, "Error", MB_OK);
bRet = FALSE;
goto END;
}
// 创建驱动服务
hServiceSys = ::CreateService(hServiceMgr,
lpszDriverName, // 驱动服务名
lpszDriverName, // 驱动服务显示名
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER, // 内核驱动
SERVICE_DEMAND_START, // 手动启动
SERVICE_ERROR_IGNORE, // 忽略错误
szDriverFilePath, // 服务文件路径
NULL,
NULL,
NULL,
NULL,
NULL);
if (!hServiceSys)
{
dwReturn = ::GetLastError();
if ((dwReturn != ERROR_IO_PENDING) && (dwReturn != ERROR_SERVICE_EXISTS))
{
// 服务创建失败,未知错误
sprintf(szErrorMsg, "CreateService() Error : %d!", dwReturn);
::MessageBox(NULL, szErrorMsg, "Error", MB_OK);
bRet = FALSE;
goto END;
}
// 服务已经存在,只需打开
hServiceSys = ::OpenService(hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS);
if (!hServiceSys)
{
sprintf(szErrorMsg, "OpenService() Error!");
::MessageBox(NULL, szErrorMsg, "Error", MB_OK);
bRet = FALSE;
goto END;
}
}
// 启动服务
bRet = ::StartService(hServiceSys, NULL, NULL);
if (!bRet)
{
dwReturn = ::GetLastError();
if ((dwReturn != ERROR_IO_PENDING) && (dwReturn != ERROR_SERVICE_ALREADY_RUNNING))
{
sprintf(szErrorMsg, "StartService() Error! : %d", dwReturn);
::MessageBox(NULL, szErrorMsg, "Error", MB_OK);
bRet = FALSE;
goto END;
}
else
{
if (dwReturn == ERROR_IO_PENDING)
{
// 设备被挂起
sprintf(szErrorMsg, "StartService() Error! : ERROR_IO_PENDING");
::MessageBox(NULL, szErrorMsg, "Error", MB_OK);
bRet = FALSE;
goto END;
}
else
{
// 设备已经运行
bRet = TRUE;
goto END;
}
}
}
END:
if (hServiceSys)
{
::CloseHandle(hServiceSys);
}
if (hServiceMgr)
{
::CloseHandle(hServiceMgr);
}
return bRet;
}
//////////////////////////////////////////////////////////////////////////
//
// 卸载驱动 实现函数
//
//////////////////////////////////////////////////////////////////////////
BOOL CLoadDriverDlg::UnloadDriver(LPCTSTR lpszSvrName)
{
BOOL bRet = TRUE;
char szErrorMsg[MAX_PATH];
SERVICE_STATUS SrvStatus;
SC_HANDLE hServiceMgr = NULL;
SC_HANDLE hServiceSys = NULL;
// 打开服务管理器
hServiceMgr = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (!hServiceMgr)
{
sprintf(szErrorMsg, "OpenSCManager() Error!");
::MessageBox(NULL, szErrorMsg, "Error", MB_OK);
bRet = FALSE;
goto END;
}
// 打开驱动服务
hServiceSys = ::OpenService(hServiceMgr, lpszSvrName, SERVICE_ALL_ACCESS);
if (!hServiceSys)
{
sprintf(szErrorMsg, "OpenService() Error!");
::MessageBox(NULL, szErrorMsg, "Error", MB_OK);
bRet = FALSE;
goto END;
}
// 停止驱动服务
if (!ControlService(hServiceSys, SERVICE_CONTROL_STOP, &SrvStatus))
{
sprintf(szErrorMsg, "ControlService() Error!");
::MessageBox(NULL, szErrorMsg, "Error", MB_OK);
bRet = FALSE;
}
// 卸载驱动服务
if (!DeleteService(hServiceSys))
{
sprintf(szErrorMsg, "DeleteService() Error!");
::MessageBox(NULL, szErrorMsg, "Error", MB_OK);
bRet = FALSE;
}
END:
if (hServiceSys)
{
::CloseHandle(hServiceSys);
}
if (hServiceMgr)
{
::CloseHandle(hServiceMgr);
}
return bRet;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?