驅動加載工具的實現
主要思路:
使用OpenSCManager函數打開服務控制管理器(SCM),獲得句柄.使用這個SCM句柄創建(或者打開)服務,服務運行加載驅動,服務停止卸載驅動
主要函數:
OpenSCManager //打開設備(服務)管理器
CreateService //創建服務(或者設備,根據參數不同而不同)
OpenService //打開設備或者服務.
StartService //啓動服務,啓動設備.
ControlService //控制設備或者服務的狀態.
CloseServiceHandle //關閉服務或者設備的句柄
DeleteService //卸載,刪除服務
主要代碼:
//安裝驅動
void CMFCDriveLoadDlg::OnBnClickedMfcbuttonInstall()
{
// TODO: 在此添加控件通知處理程序代碼
//不是驅動文件,直接返回,不處理
if (m_isDriverFile == FALSE)
{
return;
}
//1. 使用OpenSCManager函數打開SCM
m_hServiceMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (m_hServiceMgr == NULL)
{
MessageBox(_T("OpenSCManager Error"), _T("Error"));
CloseServiceHandle(m_hServiceMgr);
return;
}
//2.使用CreateService函數利用SCM句柄創建一個服務
m_hServiceDDK = CreateService(
m_hServiceMgr,//SMC句柄
_T("HadesService"),//驅動服務名稱(驅動程序的在註冊表中的名字)
_T("HadesService"),//驅動服務顯示名稱(註冊表驅動程序的DisplayName值)
SERVICE_ALL_ACCESS,//權限(所有訪問權限)
SERVICE_KERNEL_DRIVER,//服務類型(驅動程序)
SERVICE_DEMAND_START,//啓動方式(需要時啓動,註冊表驅動程序的Start值)
SERVICE_ERROR_IGNORE,//錯誤控制(忽略,註冊表驅動程序的ErrorControl值)
m_szPath_w,//服務的二進制文件路徑(驅動程序文件路徑, 註冊表驅動程序的ImagePath值)
NULL,//加載組命令
NULL,//TagId(指向一個加載順序的標籤值)
NULL,//依存關係
NULL,//服務啓動名
NULL);//密碼
if (m_hServiceDDK == NULL)
{
//如果創建錯誤,關閉句柄
CloseServiceHandle(m_hServiceDDK);
CloseServiceHandle(m_hServiceMgr);
MessageBox(_T("Install Drive Fail"), _T("Error"));
return;
}
CloseServiceHandle(m_hServiceDDK);
CloseServiceHandle(m_hServiceMgr);
GetDlgItem(IDC_STATIC_PROMPT)->SetWindowText(_T("Driver has been installed"));//設置控件文本內容
}
//加載驅動
void CMFCDriveLoadDlg::OnBnClickedMfcbuttonStart()
{
// TODO: 在此添加控件通知處理程序代碼
//不是驅動文件,直接返回,不處理
if (m_isDriverFile == FALSE)
{
return;
}
//使用OpenSCManager函數打開SCM
m_hServiceMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (m_hServiceMgr == NULL)
{
MessageBox(_T("OpenSCManager Error"), _T("Error"));
CloseServiceHandle(m_hServiceMgr);
return;
}
//打開服務獲得句柄
m_hServiceDDK = OpenService(m_hServiceMgr, _T("HadesService"), SERVICE_START);
//啓動剛剛創建的服務
BOOL bRet = StartService(m_hServiceDDK, NULL, NULL);
if (bRet == FALSE)
{
CloseServiceHandle(m_hServiceDDK);
CloseServiceHandle(m_hServiceMgr);
MessageBox(_T("Start Service Fail"), _T("Error"));
return;
}
CloseServiceHandle(m_hServiceDDK);
CloseServiceHandle(m_hServiceMgr);
GetDlgItem(IDC_STATIC_PROMPT)->SetWindowText(_T("The driver has started"));//設置控件文本內容
}
//停止驅動
void CMFCDriveLoadDlg::OnBnClickedMfcbuttonStop()
{
// TODO: 在此添加控件通知處理程序代碼
//不是驅動文件,直接返回,不處理
if (m_isDriverFile == FALSE)
{
return;
}
//使用OpenSCManager函數打開SCM
m_hServiceMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (m_hServiceMgr == NULL)
{
MessageBox(_T("OpenSCManager Error"), _T("Error"));
CloseServiceHandle(m_hServiceMgr);
return;
}
//打開服務獲得句柄
m_hServiceDDK = OpenService(m_hServiceMgr, _T("HadesService"), SERVICE_STOP);
//停止驅動服務
SERVICE_STATUS svcsta = { 0 };
BOOL bRet = ControlService(m_hServiceDDK, SERVICE_CONTROL_STOP, &svcsta);
if (bRet == FALSE)
{
CloseServiceHandle(m_hServiceDDK);
CloseServiceHandle(m_hServiceMgr);
MessageBox(_T("Stop Service Fail"), _T("Error"));
return;
}
CloseServiceHandle(m_hServiceDDK);
CloseServiceHandle(m_hServiceMgr);
GetDlgItem(IDC_STATIC_PROMPT)->SetWindowText(_T("The drive has stopped"));//設置控件文本內容
}
//卸載驅動
void CMFCDriveLoadDlg::OnBnClickedMfcbuttonUninstall()
{
// TODO: 在此添加控件通知處理程序代碼
//不是驅動文件,直接返回,不處理
if (m_isDriverFile == FALSE)
{
return;
}
//使用OpenSCManager函數打開SCM
m_hServiceMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (m_hServiceMgr == NULL)
{
MessageBox(_T("OpenSCManager Error"), _T("Error"));
CloseServiceHandle(m_hServiceMgr);
return;
}
//打開服務獲得句柄
m_hServiceDDK = OpenService(m_hServiceMgr, _T("HadesService"), SERVICE_STOP | DELETE);
//刪除驅動服務
BOOL bRet = DeleteService(m_hServiceDDK);
if (bRet == FALSE)
{
MessageBox(_T("UnInstall Service Fail"), _T("Error"));
CloseServiceHandle(m_hServiceDDK);
CloseServiceHandle(m_hServiceMgr);
return;
}
GetDlgItem(IDC_STATIC_PROMPT)->SetWindowText(_T("Driver has been uninstalled"));//設置控件文本內容
CloseServiceHandle(m_hServiceDDK);
CloseServiceHandle(m_hServiceMgr);
}