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

📄 scmanagerutility.cpp

📁 Shadow SDT的应用
💻 CPP
字号:
// SCManagerUtility.cpp: implementation of the CSCManagerUtility class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SCManagerUtility.h"

#define LAN			LANG_CHINESE
#define SUB_LAN		SUBLANG_CHINESE_SIMPLIFIED

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CSCManagerUtility::CSCManagerUtility()
{

}

CSCManagerUtility::~CSCManagerUtility()
{

}

BOOL CSCManagerUtility::InstallDriver( IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName, IN LPCTSTR ServiceExe )
{
    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( SchSCManager,          // SCManager database
		DriverName,           // name of service
		DriverName,           // name to display
		SERVICE_ALL_ACCESS,    // desired access
		SERVICE_KERNEL_DRIVER, // service type
		SERVICE_DEMAND_START,  // start type
		SERVICE_ERROR_NORMAL,  // error control type
		ServiceExe,            // 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;
	}
	else
	{
		CloseServiceHandle( schService );
	}
    return TRUE;
}

BOOL CSCManagerUtility::StartDriver( IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName )
{
    SC_HANDLE  schService;

    schService = OpenService( SchSCManager, DriverName, SERVICE_ALL_ACCESS );
    if ( schService == NULL )
	{
		return FALSE;
	}
	
	BOOL	bResult = FALSE;
	if( StartService( schService, 0, NULL ))
	{
		bResult = TRUE;
	}
	CloseServiceHandle( schService );
	return bResult;
}

BOOL CSCManagerUtility::StopDriver( IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName )
{
    SC_HANDLE       schService;
    BOOL            ret = FALSE;
    SERVICE_STATUS  serviceStatus;

    schService = OpenService( SchSCManager, DriverName, SERVICE_ALL_ACCESS );
    if ( schService == NULL )
	{
		return FALSE;
	}

	/*
	SERVICE_STATUS sStatus;
	memset(&sStatus, 0, sizeof(sStatus));
	if(QueryServiceStatus(schService, &sStatus))
	{
		if(sStatus.dwCurrentState == SERVICE_STOPPED)
		{
			ret = TRUE;
		}
		else
		{
			ret = ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus );
			CloseServiceHandle( schService );
		}
	}
	*/
	ret = ControlService( schService, SERVICE_CONTROL_STOP, &serviceStatus );
	CloseServiceHandle( schService );
	return ret;
}

BOOL CSCManagerUtility::RemoveDriver( IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName )
{
    SC_HANDLE  schService;
    BOOL       ret;
	
    schService = OpenService( SchSCManager, DriverName, SERVICE_ALL_ACCESS );
	if ( schService == NULL )
    {
		return FALSE;
	}
	ret = DeleteService( schService );
    CloseServiceHandle( schService );
    return ret;
}

BOOL CSCManagerUtility::OpenDevice( IN LPCTSTR DriverName, HANDLE * lphDevice )
{
    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"), DriverName );
		
	} else {
		
		wsprintf( completeDeviceName, TEXT("\\\\.\\%s"), DriverName );
	}
	
    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.
	if ( lphDevice )
		*lphDevice = hDevice;
	else
		CloseHandle( hDevice );
	
    return TRUE;
}

/****************************************************************************
*
*    FUNCTION: UnloadDeviceDriver( const TCHAR *)
*
*    PURPOSE: Stops the driver and has the configuration manager unload it.
*
****************************************************************************/
BOOL CSCManagerUtility::UnloadDeviceDriver( const TCHAR * Name )
{
	SC_HANDLE	schSCManager;
	schSCManager = OpenSCManager(	NULL,                 // machine (NULL == local)
		NULL,                 // database (NULL == default)
		SC_MANAGER_ALL_ACCESS // access required
		);
	
	StopDriver( schSCManager, Name );
	RemoveDriver( schSCManager, Name );
	
	CloseServiceHandle( schSCManager );
	
	return TRUE;
}

/****************************************************************************
*
*    FUNCTION: LoadDeviceDriver( const TCHAR, const TCHAR, HANDLE *)
*
*    PURPOSE: Registers a driver with the system configuration manager 
*	 and then loads it.
*
****************************************************************************/
BOOL CSCManagerUtility::LoadDeviceDriver( const TCHAR * Name, const TCHAR * Path, 
							   HANDLE * lphDevice, PDWORD Error )
{
	SC_HANDLE	schSCManager;
	BOOL		okay;
	DWORD		dwLastError;
	
	schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
	// Stop
	StopDriver(schSCManager, Name);
	/*
	if(!StopDriver(schSCManager, Name))
	{
		dwLastError = GetLastError();
		if(dwLastError != ERROR_SERVICE_NOT_ACTIVE)
		{
			*Error = dwLastError;
			return FALSE;
		}
	}
	*/

	// Remove old instances
	RemoveDriver( schSCManager, Name);
	/*
	if(!RemoveDriver( schSCManager, Name))
	{
		dwLastError = GetLastError();
		if(dwLastError != ERROR_SERVICE_MARKED_FOR_DELETE)
		{
			*Error = dwLastError;
			return FALSE;
		}
	}
	*/
	
	// Ignore success of installation: it may already be installed.
	if(!InstallDriver( schSCManager, Name, Path ))
	{
		dwLastError = GetLastError();
		if(dwLastError != ERROR_SERVICE_EXISTS)
		{
			*Error = dwLastError;
			return FALSE;
		}
	}
	
	// Ignore success of start: it may already be started.
	if(!StartDriver( schSCManager, Name ))
	{
		dwLastError = GetLastError();
		if(dwLastError != ERROR_SERVICE_ALREADY_RUNNING)
		{
			*Error = dwLastError;
			return FALSE;
		}
	}
	
	// Do make sure we can open it.
	okay = OpenDevice( Name, lphDevice );
	*Error = GetLastError();
	CloseServiceHandle( schSCManager );
	
	return okay;
}

BOOL CSCManagerUtility::ShowErrorMessage(LPTSTR& szError)
{
	// Get the error code
	DWORD dwError = GetLastError();
	
	//	  GetDlgItemText()
	HLOCAL hlocal = NULL;   // Buffer that gets the error message string
	
	// Get the error code's textual description
	BOOL fOk = FormatMessage(
		FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, 
		NULL, dwError, MAKELANGID( LAN,SUB_LAN ),
		(PTSTR) &hlocal, 0, NULL);
	
	if (!fOk) {
		// Is it a network-related error?
		HMODULE hDll = LoadLibraryEx(TEXT("netmsg.dll"), NULL, 
			DONT_RESOLVE_DLL_REFERENCES);
		
		if (hDll != NULL) {
			FormatMessage(
				FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_FROM_SYSTEM,
				hDll, dwError, MAKELANGID(LAN, SUB_LAN),
				(PTSTR) &hlocal, 0, NULL);
			FreeLibrary(hDll);
		}
	}
	
	if (hlocal != NULL) 
	{
		LPCTSTR szResult = (LPCTSTR) LocalLock(hlocal);
		szError = new TCHAR[lstrlen(szResult) + sizeof(TCHAR)];
		lstrcpy(szError, szResult);
		LocalFree(hlocal);
	}
	else 
	{
		return FALSE;
	}
	return TRUE;
}

⌨️ 快捷键说明

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