注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

熊猫正正的博客

熊猫正正的天空

 
 
 

日志

 
 

加载和卸载驱动  

2012-03-04 18:51:42|  分类: window驱动学习 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
void CLoadSysDlg::OnButtonLoadsys() 
{
// TODO: Add your control notification handler code here
CFileDialog sysFile(TRUE,NULL,NULL,OFN_HIDEREADONLY,"驱动文件*.sys|*.sys|所有文件|*.*|");
if (IDOK == sysFile.DoModal())
{
m_syspathname = sysFile.GetPathName();

DriverName = sysFile.GetFileName();

// MessageBox(DriverName,"Caption",MB_OK);
// MessageBox(sysFile.GetFileName().GetBuffer(256),"Caption",MB_OK);
UpdateData(FALSE);
//LoadNtDriver(char* lpszDriverName,char* lpszDriverPath);
LoadNtDriver(sysFile.GetFileName().GetBuffer(256),sysFile.GetPathName().GetBuffer(256));
}
}

void CLoadSysDlg::OnButtonUnloadsys() 
{
// TODO: Add your control notification handler code here
UnLoadSys(DriverName.GetBuffer(256));
}

BOOL LoadNtDriver(char* lpszDriverName,char* lpszDriverPath)
{

BOOL bRet = FALSE;
SC_HANDLE hServiceSCR,hServiceDDK;

hServiceSCR = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (hServiceSCR == NULL)
{
TRACE("OpenSCManager 调用失败");
goto BEXIT;
}

hServiceDDK = CreateService(hServiceSCR,
lpszDriverName,
lpszDriverName,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START,
SERVICE_ERROR_IGNORE,
lpszDriverPath,
NULL,
NULL,
NULL,
NULL,
NULL);

if (hServiceDDK == NULL)
{
TRACE("CreateService失败!");
hServiceDDK = OpenService(hServiceSCR,lpszDriverName,SERVICE_ALL_ACCESS);
{
TRACE("OpenService失败");
goto BEXIT;
}
}

StartService(hServiceDDK,NULL,NULL);

bRet = TRUE;
BEXIT:

//CloseServiceHandle
CloseServiceHandle(hServiceDDK);
CloseServiceHandle(hServiceSCR);

return bRet;
}


BOOL UnLoadSys(char* szSvrName)
{
BOOL bRet = FALSE;
SC_HANDLE hSCM = NULL;
SC_HANDLE hService = NULL;
SERVICE_STATUS SvrSta;

hSCM = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if (hSCM == NULL)
{
TRACE("OpenSCManager() Failed\n",GetLastError());
bRet = FALSE;
goto BeforeLeave;
}
else
{
TRACE("OpenSCManager() OK\n");
}

hService = OpenService(hSCM,szSvrName,SERVICE_ALL_ACCESS);

if (hService == NULL)
{
TRACE("OpenService() Failed\n",GetLastError());
bRet = TRUE;
goto BeforeLeave;
}
else
{
TRACE("OpenService() Ok\n");

}


if( !ControlService( hService, SERVICE_CONTROL_STOP , &SvrSta ) )  
{  
TRACE( "用ControlService() 停止驱动程序失败 错误号:%d !\n", GetLastError() );  
}  
else  
{
//停止驱动程序成功
TRACE( "用ControlService() 停止驱动程序成功 !\n" );  
}  


if (!DeleteService(hService))
{
TRACE("卸载失败:DeleteService()错误号:%d\n",GetLastError());
}
else
{
TRACE("卸载成功\n");
}


bRet = TRUE;
BeforeLeave:
if (hService > 0)
{
CloseServiceHandle(hService);
}
if (hSCM > 0)
{
CloseServiceHandle(hSCM);
}

return bRet;
}
  评论这张
 
阅读(60)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017