NT 드라이브 의 로드 와 마 운 트 해제

//    
#include   
#include   
#include   
#include 


#define DRIVER_NAME "HelloDDK"
#define DRIVER_PATH "..\\MyDriver\\MyDriver_Check\\HelloDDK.sys"


//  NT    
BOOL LoadNTDriver(char* lpszDriverName,char* lpszDriverPath)
{
char szDriverImagePath[256];
//         
GetFullPathName(lpszDriverPath, 256, szDriverImagePath, NULL);


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  
printf( "OpenSCManager() Faild %d ! 
", GetLastError() ); bRet = FALSE; goto BeforeLeave; } else { OpenSCManager printf( "OpenSCManager() ok !
" );   } // hServiceDDK = CreateService( hServiceMgr, lpszDriverName, //   lpszDriverName, // DisplayName   SERVICE_ALL_ACCESS, //   SERVICE_KERNEL_DRIVER,//   SERVICE_DEMAND_START, // Start   SERVICE_ERROR_IGNORE, // ErrorControl   szDriverImagePath, // 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 );   bRet = FALSE; goto BeforeLeave; }   else   { // , printf( "CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS!
" );   } // ,   hServiceDDK = OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );   if( hServiceDDK == NULL )   { // , dwRtn = GetLastError();   printf( "OpenService() Faild %d !
", dwRtn );   bRet = FALSE; goto BeforeLeave; }   else  { printf( "OpenService() ok !
" ); } }   else   { printf( "CrateService() ok !
" ); } // 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 );   bRet = FALSE; goto BeforeLeave; }   else   {   if( dwRtn == ERROR_IO_PENDING )   {   // printf( "StartService() Faild ERROR_IO_PENDING !
"); bRet = FALSE; goto BeforeLeave; }   else   {   // printf( "StartService() Faild ERROR_SERVICE_ALREADY_RUNNING !
"); bRet = TRUE; goto BeforeLeave; }   }   } bRet = TRUE; // BeforeLeave: if(hServiceDDK) { CloseServiceHandle(hServiceDDK); } if(hServiceMgr) { CloseServiceHandle(hServiceMgr); } return bRet; }

 
    


//        
BOOL UnloadNTDriver( char * szSvrName )  
{
BOOL bRet = FALSE;
SC_HANDLE hServiceMgr=NULL;//SCM      
SC_HANDLE hServiceDDK=NULL;//NT         
SERVICE_STATUS SvrSta;
//  SCM   
hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );  
if( hServiceMgr == NULL )  
{
//  SCM     
printf( "OpenSCManager() Faild %d ! 
", GetLastError() );   bRet = FALSE; goto BeforeLeave; }   else   { // SCM printf( "OpenSCManager() ok !
" );   } // hServiceDDK = OpenService( hServiceMgr, szSvrName, SERVICE_ALL_ACCESS );   if( hServiceDDK == NULL )   { // printf( "OpenService() Faild %d !
", GetLastError() );   bRet = FALSE; goto BeforeLeave; }   else   {   printf( "OpenService() ok !
" );   }   // , , , 。   if( !ControlService( hServiceDDK, SERVICE_CONTROL_STOP , &SvrSta ) )   {   printf( "ControlService() Faild %d !
", GetLastError() );   }   else   { // printf( "ControlService() ok !
" );   }   // 。   if( !DeleteService( hServiceDDK ) )   { // printf( "DeleteSrevice() Faild %d !
", GetLastError() );   }   else   {   // printf( "DelServer:eleteSrevice() ok !
" );   }   bRet = TRUE; BeforeLeave: // if(hServiceDDK) { CloseServiceHandle(hServiceDDK); } if(hServiceMgr) { CloseServiceHandle(hServiceMgr); } return bRet; } 
#include 
#include 
#include  
#include 
#include 

//       

void 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; } } showErrorInfo(GetLastError(), __LINE__, L"xxxx",1);

좋은 웹페이지 즐겨찾기