国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

應用層 加載驅動 和 卸載驅動代碼

2019-11-14 11:59:46
字體:
來源:轉載
供稿:網友
BOOL LoadDriver(char* lpszDriverName,char* lpszDriverPath){    char szDriverImagePath[256] = {0};    //得到完整的驅動路徑    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 ! /n", GetLastError() );        bRet = FALSE;        goto BeforeLeave;    }    else    {        //OpenSCManager成功        printf( "OpenSCManager() ok ! /n" );      }    //創建驅動所對應的服務    hServiceDDK = CreateService( hServiceMgr,        lpszDriverName, //驅動程序的在注冊表中的名字          lpszDriverName, // 注冊表驅動程序的 DisplayName 值          SERVICE_ALL_ACCESS, // 加載驅動程序的訪問權限          SERVICE_KERNEL_DRIVER,// 表示加載的服務是驅動程序          SERVICE_DEMAND_START, // 注冊表驅動程序的 Start 值          SERVICE_ERROR_IGNORE, // 注冊表驅動程序的 ErrorControl 值          szDriverImagePath, // 注冊表驅動程序的 ImagePath 值          NULL,  //GroupOrder HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/GroupOrderList        NULL,          NULL,          NULL,          NULL);      DWord dwRtn;    //判斷服務是否失敗    if( hServiceDDK == NULL )      {          dwRtn = GetLastError();        if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS )          {              //由于其他原因創建服務失敗            printf( "CrateService() Faild %d ! /n", dwRtn );              bRet = FALSE;            goto BeforeLeave;        }          else          {            //服務創建失敗,是由于服務已經創立過            printf( "CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! /n" );          }        // 驅動程序已經加載,只需要打開          hServiceDDK = OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );          if( hServiceDDK == NULL )          {            //如果打開服務也失敗,則意味錯誤            dwRtn = GetLastError();              printf( "OpenService() Faild %d ! /n", dwRtn );              bRet = FALSE;            goto BeforeLeave;        }          else         {            printf( "OpenService() ok ! /n" );        }    }      else      {        printf( "CrateService() ok ! /n" );    }    //開啟此項服務    bRet= StartService( hServiceDDK, NULL, NULL );      if( !bRet )      {          DWORD dwRtn = GetLastError();          if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING )          {              printf( "StartService() Faild %d ! /n", dwRtn );              bRet = FALSE;            goto BeforeLeave;        }          else          {              if( dwRtn == ERROR_IO_PENDING )              {                  //設備被掛住                printf( "StartService() Faild ERROR_IO_PENDING ! /n");                bRet = FALSE;                goto BeforeLeave;            }              else              {                  //服務已經開啟                printf( "StartService() Faild ERROR_SERVICE_ALREADY_RUNNING ! /n");                bRet = TRUE;                goto BeforeLeave;            }          }      }    bRet = TRUE;//離開前關閉句柄BeforeLeave:    if(hServiceDDK)    {        CloseServiceHandle(hServiceDDK);    }    if(hServiceMgr)    {        CloseServiceHandle(hServiceMgr);    }    return bRet;}//卸載驅動程序BOOL UnloadDriver( 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:deleteSrevice() ok !/n" );      }      bRet = TRUE;BeforeLeave://離開前關閉打開的句柄    if(hServiceDDK)    {        CloseServiceHandle(hServiceDDK);    }    if(hServiceMgr)    {        CloseServiceHandle(hServiceMgr);    }    return bRet;    }
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 岑溪市| 饶阳县| 科技| 武清区| 延吉市| 嘉祥县| 繁昌县| 千阳县| 师宗县| 七台河市| 融水| 固安县| 龙南县| 余姚市| 太湖县| 漠河县| 搜索| 鹤岗市| 韩城市| 武城县| 潜山县| 汕尾市| 琼中| 尚义县| 怀宁县| 蓬莱市| 南郑县| 军事| 武胜县| 穆棱市| 馆陶县| 招远市| 房产| 饶河县| 南华县| 宜阳县| 宕昌县| 德安县| 阜新| 阜新| 大宁县|