⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loaddrv.cpp

📁 以filemon开源代码为模板
💻 CPP
字号:
#include "StdAfx.h"
#include "LoadDrv.h"

CLoadDrv::CLoadDrv(LPCTSTR driveName,LPCTSTR sysPath)
{
	m_sysPath = new TCHAR[MAX_PATH];
	lstrcpy(m_sysPath,sysPath);
	m_strDriverName = new TCHAR[MAX_PATH];
	lstrcpy(m_strDriverName,driveName);
}

CLoadDrv::~CLoadDrv(void)
{
	delete [] m_strDriverName;
	delete [] m_sysPath;
}


/****************************************************************************
*
*    FUNCTION: InstallDriver( IN SC_HANDLE, IN LPCTSTR, IN LPCTSTR)
*
*    PURPOSE: Creates a driver service.
*
****************************************************************************/
BOOL CLoadDrv::InstallDriver()
{
	SC_HANDLE  schService;

	//
	// NOTE: This creates an entry for a standalone driver. If this
	//       is modified for use with a driver that requires a Tag,
	//       Group, and/or Dependencies, it may be necessary to
	//       query the registry for existing driver information
	//       (in order to determine a unique Tag, etc.).
	//

	schService = CreateService( m_schSCManager,          // SCManager database
		m_strDriverName,           // name of service
		m_strDriverName,           // name to display
		SERVICE_ALL_ACCESS,    // desired access
		SERVICE_KERNEL_DRIVER, // service type
		SERVICE_DEMAND_START,  // start type
		SERVICE_ERROR_NORMAL,  // error control type
		m_sysPath,            // service's binary
		NULL,                  // no load ordering group
		NULL,                  // no tag identifier
		NULL,                  // no dependencies
		NULL,                  // LocalSystem account
		NULL                   // no password
		);
	if ( schService == NULL )
		return FALSE;

	CloseServiceHandle( schService );

	return TRUE;
}


/****************************************************************************
*
*    FUNCTION: StartDriver( IN SC_HANDLE, IN LPCTSTR)
*
*    PURPOSE: Starts the driver service.
*
****************************************************************************/
BOOL CLoadDrv::StartDriver()
{
	SC_HANDLE  schService;
	BOOL       ret;

	schService = OpenService( m_schSCManager,
		m_strDriverName,
		SERVICE_ALL_ACCESS
		);
	if ( schService == NULL )
		return FALSE;

	ret = StartService( schService, 0, NULL )
		|| GetLastError() == ERROR_SERVICE_ALREADY_RUNNING 
		|| GetLastError() == ERROR_SERVICE_DISABLED;

	CloseServiceHandle( schService );

	return ret;
}



/****************************************************************************
*
*    FUNCTION: OpenDevice( IN LPCTSTR, HANDLE *)
*
*    PURPOSE: Opens the device and returns a handle if desired.
*
****************************************************************************/
BOOL CLoadDrv::OpenDevice()
{
	TCHAR    completeDeviceName[64];
	HANDLE   hDevice;

	//
	// Create a \\.\XXX device name that CreateFile can use
	//
	// NOTE: We're making an assumption here that the driver
	//       has created a symbolic link using it's own name
	//       (i.e. if the driver has the name "XXX" we assume
	//       that it used IoCreateSymbolicLink to create a
	//       symbolic link "\DosDevices\XXX". Usually, there
	//       is this understanding between related apps/drivers.
	//
	//       An application might also peruse the DEVICEMAP
	//       section of the registry, or use the QueryDosDevice
	//       API to enumerate the existing symbolic links in the
	//       system.
	//

	if( (GetVersion() & 0xFF) >= 5 ) {

		//
		// We reference the global name so that the application can
		// be executed in Terminal Services sessions on Win2K
		//
		wsprintf( completeDeviceName, TEXT("\\\\.\\Global\\%s"), m_strDriverName );

	} else {

		wsprintf( completeDeviceName, TEXT("\\\\.\\%s"), m_strDriverName );
	}
	hDevice = CreateFile( completeDeviceName,
		GENERIC_READ | GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL,
		NULL
		);
	if ( hDevice == ((HANDLE)-1) )
		return FALSE;

	// If user wants handle, give it to them.  Otherwise, just close it.
	m_sysHandle = hDevice;
	//if ( lphDevice )
	//	*lphDevice = hDevice;
	//else
	//	CloseHandle( hDevice );

	return TRUE;
}



/****************************************************************************
*
*    FUNCTION: StopDriver( IN SC_HANDLE, IN LPCTSTR)
*
*    PURPOSE: Has the configuration manager stop the driver (unload it)
*
****************************************************************************/
BOOL CLoadDrv::StopDriver()
{
	SC_HANDLE       schService;
	BOOL            ret;
	SERVICE_STATUS  serviceStatus;

	schService = OpenService(m_schSCManager, m_strDriverName, SERVICE_ALL_ACCESS );
	if ( schService == NULL )
		return FALSE;

	ret = ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus );

	CloseServiceHandle( schService );

	return ret;
}


/****************************************************************************
*
*    FUNCTION: RemoveDriver( IN SC_HANDLE, IN LPCTSTR)
*
*    PURPOSE: Deletes the driver service.
*
****************************************************************************/
BOOL CLoadDrv::RemoveDriver()
{
	SC_HANDLE  schService;
	BOOL       ret;

	schService = OpenService( m_schSCManager,
		m_strDriverName,
		SERVICE_ALL_ACCESS
		);

	if ( schService == NULL )
		return FALSE;

	ret = DeleteService( schService );

	CloseServiceHandle( schService );

	return ret;
}


/****************************************************************************
*
*    FUNCTION: UnloadDeviceDriver( const TCHAR *)
*
*    PURPOSE: Stops the driver and has the configuration manager unload it.
*
****************************************************************************/
BOOL CLoadDrv::UnloadDeviceDriver()
{

	m_schSCManager = OpenSCManager(	NULL,                 // machine (NULL == local)
		NULL,                 // database (NULL == default)
		SC_MANAGER_ALL_ACCESS // access required
		);

	StopDriver();
	RemoveDriver();

	CloseServiceHandle(m_schSCManager);

	 m_schSCManager = NULL;

	return TRUE;
}



/****************************************************************************
*
*    FUNCTION: LoadDeviceDriver( const TCHAR, const TCHAR, HANDLE *)
*
*    PURPOSE: Registers a driver with the system configuration manager 
*	 and then loads it.
*
****************************************************************************/
BOOL CLoadDrv::LoadDeviceDriver(PDWORD Error )
{
	BOOL		okay;

	m_schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );

	// Remove previous instance
	RemoveDriver();

	// Ignore success of installation: it may already be installed.
	InstallDriver();

	// Ignore success of start: it may already be started.
	StartDriver();

	// Do make sure we can open it.
	okay = OpenDevice();
	*Error = GetLastError();
	CloseServiceHandle( m_schSCManager );

	return okay;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -