实现自己的驱动工具
用C++写了一个通用的类,之后用MFC写的界面调用的。主要就是掉函数安装。
C++类
CInStallDriver.h
#include <WinSvc.h>
#include <string>
using namespace std;
class CCInStallDriver
{
public:
CCInStallDriver(void);
~CCInStallDriver(void);
public:
//获取路径和文件名,并返回
BOOL GetDriverPathAndFileName(wstring &strPath, wstring &strFileName);
//安装
BOOL InstallDriver(int &iError);
//启动
BOOL StartDriver(int &iError);
//停止
BOOL StopDriver(int &iError);
//卸载
BOOL UnLoadDriver(int &iError);
public:
wstring m_wstrPath;
wstring m_wstrFileName;
SC_HANDLE m_scMageger;
};
CInStallDriver.cpp
#include "CInStallDriver.h"
#include <windows.h>
#include <string>
using namespace std;
CCInStallDriver::CCInStallDriver(void)
{
m_scMageger = NULL;
}
CCInStallDriver::~CCInStallDriver(void)
{
if (m_scMageger != NULL)
{
CloseServiceHandle(m_scMageger);
m_scMageger == NULL
}
}
//选择一个文件
BOOL CCInStallDriver::GetDriverPathAndFileName(wstring &strPath, wstring &strFileName)
{
// TODO: 在此添加控件通知处理程序代码
wchar_t szFileName[MAX_PATH] = { 0 };
OPENFILENAME openFileName = { 0 };
openFileName.lStructSize = sizeof(OPENFILENAME);
openFileName.nMaxFile = MAX_PATH; //这个必须设置,不设置的话不会出现打开文件对话框
openFileName.lpstrFilter = L"所有文件(*.*)\0*.*\0\0";
openFileName.lpstrFile = szFileName;
openFileName.nFilterIndex = 1;
openFileName.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (::GetOpenFileName(&openFileName))
{
strPath = szFileName;
if (strPath.empty())
{
return FALSE;
}
m_wstrPath = strPath;
int iVal = strPath.rfind('\\');
if (iVal >= 0)
{
strFileName = strPath.substr(iVal + 1);
m_wstrFileName = strFileName;
}
else
{
return FALSE;
}
}
m_scMageger = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (m_scMageger == NULL)
{
return FALSE;
}
return TRUE;
}
//安装
BOOL CCInStallDriver::InstallDriver(int &iError)
{
if (m_wstrFileName.empty() || m_wstrPath.empty() || m_scMageger == NULL)
{
return FALSE;
}
SC_HANDLE serviceHandle = CreateServiceW(m_scMageger, m_wstrFileName.c_str(), m_wstrFileName.c_str(), SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, m_wstrPath.c_str(), NULL, NULL, NULL, NULL ,NULL);
if (serviceHandle == NULL)
{
iError = GetLastError();
return FALSE;
}
CloseServiceHandle(serviceHandle);
return TRUE;
}
//启动
BOOL CCInStallDriver::StartDriver(int &iError)
{
if (m_wstrFileName.empty() || m_wstrPath.empty() || m_scMageger == NULL)
{
return FALSE;
}
SC_HANDLE serviceHandle = OpenService(m_scMageger, m_wstrFileName.c_str(), SERVICE_ALL_ACCESS);
if (serviceHandle == NULL)
{
iError = GetLastError();
return FALSE;
}
int result = StartService(serviceHandle, 0, NULL);
if (result == 0)
{
iError = GetLastError();
}
CloseServiceHandle(serviceHandle);
return TRUE;
}
//停止
BOOL CCInStallDriver::StopDriver(int &iError)
{
if (m_wstrFileName.empty() || m_wstrPath.empty() || m_scMageger == NULL)
{
return FALSE;
}
SC_HANDLE serviceHandle = OpenService(m_scMageger, m_wstrFileName.c_str(), SERVICE_ALL_ACCESS);
if (serviceHandle == NULL)
{
iError = GetLastError();
return FALSE;
}
SERVICE_STATUS error = { 0 };
int result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, &error);
if (result == 0)
{
iError = GetLastError();
return FALSE;
}
CloseServiceHandle(serviceHandle);
return TRUE;
}
//卸载
BOOL CCInStallDriver::UnLoadDriver(int &iError)
{
if (m_wstrFileName.empty() || m_wstrPath.empty() || m_scMageger == NULL)
{
return FALSE;
}
SC_HANDLE serviceHandle = OpenService(m_scMageger, m_wstrFileName.c_str(), SERVICE_ALL_ACCESS);
if (serviceHandle == NULL)
{
iError = GetLastError();
return FALSE;
}
if (!DeleteService(serviceHandle))
{
iError = GetLastError();
return FALSE;
}
CloseServiceHandle(serviceHandle);
CloseServiceHandle(m_scMageger);
m_scMageger = NULL;
return TRUE;
}
MFC
MFC 就几个按钮的实现。
//获取路径
void CInStallDriverDlg::OnBnClickedButtonPath()
{
// TODO: 在此添加控件通知处理程序代码
wstring wstrPath;
wstring wstrFileName;
if (!m_CInStallDriver.GetDriverPathAndFileName(wstrPath, wstrFileName))
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"获取文件路径失败。");
return;
}
GetDlgItem(IDC_EDIT_PATH)->SetWindowText(wstrPath.c_str());
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"获取文件路径成功。");
}
void CInStallDriverDlg::OnBnClickedButtonInstall()
{
// TODO: 在此添加控件通知处理程序代码
int iError = 0;
if (!m_CInStallDriver.InstallDriver(iError))
{
if (iError == ERROR_SERVICE_EXISTS)
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"服务已经存在不需要创建了。");
}
else
{
CString strError;
strError.Format(L"服务创建失败: %d", iError);
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(strError);
}
}
else
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"服务创建成功。");
}
}
void CInStallDriverDlg::OnBnClickedButtonStart()
{
// TODO: 在此添加控件通知处理程序代码
int iError = 0;
if (!m_CInStallDriver.StartDriver(iError))
{
if (iError == ERROR_SERVICE_DOES_NOT_EXIST)
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"服务不存在,请重新安装。");
}
else if (iError == ERROR_SERVICE_ALREADY_RUNNING)
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"已经运行。");
}
else
{
CString strError;
strError.Format(L"运行失败: %d", iError);
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(strError);
}
}
else
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"运行成功。");
}
}
void CInStallDriverDlg::OnBnClickedButtonStop()
{
// TODO: 在此添加控件通知处理程序代码
int iError = 0;
if (!m_CInStallDriver.StopDriver(iError))
{
if (iError == ERROR_SERVICE_DOES_NOT_EXIST)
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"服务不存在,请重新安装。");
}
else if (iError == ERROR_SERVICE_NOT_ACTIVE)
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"没有在运行。");
}
else
{
CString strError;
strError.Format(L"停止失败: %d", iError);
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(strError);
}
}
else
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"已停止。");
}
}
void CInStallDriverDlg::OnBnClickedButtonUnload()
{
// TODO: 在此添加控件通知处理程序代码
int iError = 0;
if (!m_CInStallDriver.UnLoadDriver(iError))
{
if (iError == ERROR_SERVICE_DOES_NOT_EXIST)
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"服务不存在,请重新安装。");
}
else
{
CString strError;
strError.Format(L"卸载失败: %d", iError);
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(strError);
}
}
else
{
GetDlgItem(IDC_STATIC_STATE)->SetWindowText(L"卸载成功。");
}
}