动态加载驱动 DDK

转自:http://laokaddk.blog.51cto.com/368606/411887

 

以服务的方式,动态加载驱动

以后就是两个实现这两个功能的函数.
//=================================================================
//函数名:StopDriver
//功能:  暂停驱动
//参数:
//  IN strDriverName  驱动名
//  IN strServiceName 服务名
//返回值:
//  TRUE  成功
//  FALSE 失败
//=================================================================
BOOL StopDriver(IN LPCSTR strDriverName,
    IN LPCSTR strServiceName)
{
 SC_HANDLE hMangerHandle = NULL;
 SC_HANDLE hCreateServiceHandle = NULL;
 SC_HANDLE hOpenServiceHandle = NULL;
 SERVICE_STATUS sStatus ;
 DWORD dwErrCode = 0;
 BOOL bRet = TRUE;


 do 
 {
  //1.打开服务控制管理器
  hMangerHandle = ::OpenSCManager(NULL,
   NULL,
   SC_MANAGER_ALL_ACCESS);
  if(!hMangerHandle)
  {
   bRet = FALSE;
   break;
  }

 

  //2. 打开service
  hOpenServiceHandle = OpenService(hMangerHandle, strServiceName,
   SERVICE_START | DELETE | SERVICE_STOP);
  if (!hOpenServiceHandle)
  {
   dwErrCode = ::GetLastError();
   bRet = FALSE;
   break;
  }

  //4.启动服务程序
  bRet = ControlService(hOpenServiceHandle, SERVICE_CONTROL_STOP, &sStatus);
  if (!bRet)
  {
   bRet = FALSE;
   break;
  }

  //删除服务
  bRet = DeleteService(hOpenServiceHandle);

 

 } while (0);


 if (hOpenServiceHandle)
 {
  CloseServiceHandle(hOpenServiceHandle);
  hOpenServiceHandle = NULL;
 }
 if (hCreateServiceHandle)
 {
  CloseServiceHandle(hCreateServiceHandle);
  hCreateServiceHandle = NULL;
 }
 if (hMangerHandle)
 {
  CloseServiceHandle(hMangerHandle);
  hMangerHandle = NULL;
 }


 return bRet;
}

//加载驱动
//=================================================================
//函数名:StartDriver
//功能:  以服务的方式启动驱动
//参数:
//  IN strDriverName  驱动名
//  IN strServiceName 服务名
//  IN strDriverFilePath  文件路径
//返回值:
//  TRUE  成功
//  FALSE 失败
//=================================================================

BOOL StartDriver(IN LPCSTR strDriverName,
     IN LPCSTR strServiceName,
     IN LPCSTR strDriverFilePath)
{
 SC_HANDLE hMangerHandle = NULL;
 SC_HANDLE hCreateServiceHandle = NULL;
 SC_HANDLE hOpenServiceHandle = NULL;
 SERVICE_STATUS sStatus ;
 DWORD dwErrCode = 0;
 BOOL bRet = TRUE;


 do 
 {
  //1.打开服务控制管理器
  hMangerHandle = ::OpenSCManager(NULL,
          NULL,
          SC_MANAGER_ALL_ACCESS);
  if(!hMangerHandle)
  {
   bRet = FALSE;
   break;
  }

  //2. 创建service
  hCreateServiceHandle = CreateService(hMangerHandle,
            strServiceName,
            strDriverName,
            SERVICE_ALL_ACCESS,
            SERVICE_KERNEL_DRIVER,//SERVICE_FILE_SYSTEM_DRIVER
            SERVICE_DEMAND_START,
            SERVICE_ERROR_IGNORE,
            strDriverFilePath,
            NULL,
            NULL,
            NULL,
            NULL,
            NULL);
  if (!hCreateServiceHandle)
  {
   dwErrCode = ::GetLastError();
   if (ERROR_SERVICE_EXISTS != dwErrCode)
   {
    bRet = FALSE;
    break;
   }

  }


  //3. 打开service
  hOpenServiceHandle = OpenService(hMangerHandle, strServiceName,
   SERVICE_START | DELETE | SERVICE_STOP);
  if (!hOpenServiceHandle)
  {
   dwErrCode = ::GetLastError();
   bRet = FALSE;
   break;
  }

  //4.启动服务程序
  bRet = StartService(hOpenServiceHandle,NULL, NULL);
  if (FALSE == bRet)
  {
   dwErrCode = ::GetLastError();
   bRet = FALSE;
   break;
  }

 } while (0);


 if (hOpenServiceHandle)
 {
  CloseServiceHandle(hOpenServiceHandle);
  hOpenServiceHandle = NULL;
 }
 if (hCreateServiceHandle)
 {
  CloseServiceHandle(hCreateServiceHandle);
  hCreateServiceHandle = NULL;
 }
 if (hMangerHandle)
 {
  CloseServiceHandle(hMangerHandle);
  hMangerHandle = NULL;
 }

 

 return bRet;
}

你可能感兴趣的:(Kernel)