ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 卸载NT驱动程序 (源Windows驱动程序详解)

卸载NT驱动程序 (源Windows驱动程序详解)

原创 Linux操作系统 作者:tangtao94 时间:2011-11-18 22:53:15 0 删除 编辑
//卸载NT驱动程序  
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 ! \n", GetLastError() );  
        bRet = FALSE;
        goto BeforeLeave;
    }  
    else  
    {
        //带开SCM管理器失败成功
        printf( "OpenSCManager() ok ! \n" );  
    }
    //打开驱动所对应的服务
    hServiceDDK = OpenService( hServiceMgr, szSvrName, SERVICE_ALL_ACCESS );  

    if( hServiceDDK == NULL )  
    {
        //打开驱动所对应的服务失败
        printf( "OpenService() Faild %d ! \n", GetLastError() );  
        bRet = FALSE;
        goto BeforeLeave;
    }  
    else  
    {  
        printf( "OpenService() ok ! \n" );  
    }  
    //停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。  
    if( !ControlService( hServiceDDK, SERVICE_CONTROL_STOP , &SvrSta ) )  
    {  
        printf( "ControlService() Faild %d !\n", GetLastError() );  
    }  
    else  
    {
        //打开驱动所对应的失败
        printf( "ControlService() ok !\n" );  
    }  
    //动态卸载驱动程序。  
    if( !DeleteService( hServiceDDK ) )  
    {
        //卸载失败
        printf( "DeleteSrevice() Faild %d !\n", GetLastError() );  
    }  
    else  
    {  
        //卸载成功
        printf( "DelServer:eleteSrevice() ok !\n" );  
    }  
    bRet = TRUE;
BeforeLeave:
//离开前关闭打开的句柄
    if(hServiceDDK)
    {
        CloseServiceHandle(hServiceDDK);
    }
    if(hServiceMgr)
    {
        CloseServiceHandle(hServiceMgr);
    }
    return bRet;    
}

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/518171/viewspace-711612/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2010-07-06

  • 博文量
    33
  • 访问量
    242821