NT駆動クラスC++のロード
22707 ワード
ヘッダファイル
ソースファイル
#pragma once
#include <windows.h>
#include <winsvc.h>
#include <tchar.h>
class CLoadNtDriver
{
public:
CLoadNtDriver();
~CLoadNtDriver();
BOOL pathIsFile(CString strPath);
BOOL loadNTDriver(CString strDriverPath);
BOOL unloadNTDriver(CString strSvrName);
void showErrorInfo(UINT nErrCode, UINT nLine, LPCTSTR = _T(""), UINT = 0);
CString m_strDvrName;
wchar_t m_szServerName[40];
wchar_t m_szShowName[40];
};
ソースファイル
#include "stdafx.h"
#include "LoadNtDriver.h"
#include <strsafe.h>
CLoadNtDriver::CLoadNtDriver()
{
}
CLoadNtDriver::~CLoadNtDriver()
{
}
BOOL CLoadNtDriver::pathIsFile(CString strPath)
{
//
// Code by Lthis
if (PathIsDirectory(strPath))
return FALSE;
if (PathFileExists(strPath))
return TRUE;
return FALSE;
}
void CLoadNtDriver::showErrorInfo(UINT nErrCode, UINT nLine, LPCTSTR lpFuncName, UINT nType){
LPTSTR lpMsgBuf = NULL;
TCHAR szMessage[256] = { 0 };
TCHAR szCaption[32] = { 0 };
FormatMessage(0x1300, NULL, nErrCode, 0x400, (LPTSTR)&lpMsgBuf, 64, NULL);
StringCchPrintf(szMessage, 256, _T("Error code:0x%X:%s
"), nErrCode, lpMsgBuf);
StringCchPrintf(szCaption, 32, _T("%s (Error Line:%d)"), lpFuncName, nLine);
StringCchCat(szMessage, 256+32+1, szCaption);
switch (nType)
{
case 0:
OutputDebugString(szMessage);
break;
case 1:
MessageBox(NULL, szMessage, szCaption, 0);
break;
default:
break;
}
}
// NT
BOOL CLoadNtDriver::loadNTDriver(CString strDriverPath)
{
wchar_t szDriverImagePath[256];
CString strDriverImagePath;
//
GetFullPathName(strDriverPath, 256, szDriverImagePath, NULL);
strDriverImagePath = szDriverImagePath;
if (strDriverImagePath.IsEmpty()){
MessageBoxW(0, L" CLoadDriver::installDriver: ", NULL, MB_OK | MB_ICONWARNING);
return FALSE;
}
if (!pathIsFile(strDriverImagePath)){
strDriverImagePath += L"loadNTDriver: ";
MessageBoxW(0, strDriverImagePath, NULL, MB_OK | MB_ICONWARNING);
return FALSE;
}
//
int nPos;
nPos = strDriverImagePath.ReverseFind(_T('\\'));
m_strDvrName = strDriverImagePath.Right(nPos - 1);
nPos = m_strDvrName.ReverseFind(_T('.'));
m_strDvrName = m_strDvrName.Left(nPos);
swprintf_s(m_szServerName, m_strDvrName + L"_ServerName");
swprintf_s(m_szShowName, m_strDvrName + L"_ShowName");
BOOL bRet = FALSE;
SC_HANDLE hServiceMgr = NULL; // SCM
SC_HANDLE hServiceDDK = NULL; // NT
//
hServiceMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hServiceMgr == NULL){
//OpenSCManager
showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver");
bRet = FALSE;
goto BeforeLeave;
}
else{
//OpenSCManager
showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()");
}
//
hServiceDDK = CreateService(hServiceMgr,
m_szServerName, //
m_szShowName, // DisplayName
SERVICE_ALL_ACCESS, //
SERVICE_KERNEL_DRIVER, //
SERVICE_DEMAND_START, // Start
SERVICE_ERROR_IGNORE, // ErrorControl
strDriverImagePath, // ImagePath
NULL,
NULL,
NULL,
NULL,
NULL);
DWORD dwRtn;
//
if (hServiceDDK == NULL)
{
dwRtn = GetLastError();
if (dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS)
{
//
//printf("CrateService() Faild %d !
", dwRtn);
showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()");
bRet = FALSE;
goto BeforeLeave;
}
else
{
// ,
//printf("CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS!
");
showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()");
}
// ,
hServiceDDK = OpenService(hServiceMgr, m_szServerName, SERVICE_ALL_ACCESS);
if (hServiceDDK == NULL)
{
// ,
dwRtn = GetLastError();
printf("OpenService() Faild %d !
", dwRtn);
bRet = FALSE;
goto BeforeLeave;
}
}
//
bRet = StartService(hServiceDDK, NULL, NULL);
if (!bRet)
{
DWORD dwRtn = GetLastError();
if (dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING)
{
//printf("StartService() Faild %d !
", dwRtn);
showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()");
bRet = FALSE;
goto BeforeLeave;
}
else
{
if (dwRtn == ERROR_IO_PENDING)
{
//
//printf("StartService() Faild ERROR_IO_PENDING !
");
showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()");
bRet = FALSE;
goto BeforeLeave;
}
else
{
//
//printf("StartService() Faild ERROR_SERVICE_ALREADY_RUNNING !
");
showErrorInfo(GetLastError(), __LINE__, L"loadNTDriver()");
bRet = TRUE;
goto BeforeLeave;
}
}
}
bRet = TRUE;
//
BeforeLeave:
if (hServiceDDK){
CloseServiceHandle(hServiceDDK);
}
if (hServiceMgr){
CloseServiceHandle(hServiceMgr);
}
return bRet;
}
/****************************************************************
// FunctionName: unloadNTDriver
// Function : NT
// Parameter: strSvrName, , m_szServerName ,
// Author:
// Create: 2015-3-23 20:57:50
// Checked: Lthis 2015-3-23
****************************************************************/
BOOL CLoadNtDriver::unloadNTDriver(CString strSvrName)
{
BOOL bRet = FALSE;
SC_HANDLE hServiceMgr = NULL;//SCM
SC_HANDLE hServiceDDK = NULL;//NT
SERVICE_STATUS SvrSta;
CString strError;
// SCM
hServiceMgr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (hServiceMgr == NULL)
{
// SCM
//"unloadNTDriver -> OpenSCManager() Faild !
";
showErrorInfo(GetLastError(), __LINE__, L"unloadNTDriver()");
bRet = FALSE;
goto BeforeLeave;
}
//
hServiceDDK = OpenService(hServiceMgr, strSvrName, SERVICE_ALL_ACCESS);
if (hServiceDDK == NULL){
//
//strError = L"unloadNTDriver -> OpenService() Faild ! !";
showErrorInfo(GetLastError(), __LINE__, L"unloadNTDriver()");
goto BeforeLeave;
}
// , , , 。
if (!ControlService(hServiceDDK, SERVICE_CONTROL_STOP, &SvrSta))
{
//printf("ControlService() Faild %d !
", GetLastError());
showErrorInfo(GetLastError(), __LINE__, L"unloadNTDriver()");
}
// 。
if (!DeleteService(hServiceDDK))
{
//
//printf("DeleteSrevice() Faild %d !
", GetLastError());
showErrorInfo(GetLastError(), __LINE__, L"unloadNTDriver()");
}
bRet = TRUE;
BeforeLeave:
//
if (hServiceDDK){
CloseServiceHandle(hServiceDDK);
}
if (hServiceMgr){
CloseServiceHandle(hServiceMgr);
}
return bRet;
}