vc編程實現sys文件的安裝

  • 2019 年 11 月 21 日
  • 筆記

#include <windows.h>  #include <winsvc.h>  #include <conio.h>  #include <stdio.h>    #define DRIVER_NAME "123467"  #define DRIVER_PATH "..\HelloDDK.sys"    //裝載NT驅動程式  BOOL LoadNTDriver(char* lpszDriverName,char* lpszDriverPath)  {    /************************ 載入NT驅動的程式碼*******************************     ① 調用OpenSCManager,打開SCM管理器.如果返回NULL,則返回失敗,否則繼續     ② 調用CreateService,創建服務,創建成功則轉步驟 ⑥        ③ 用GetLastError的得到錯誤返回值     ④ 返回值為ERROR_IO_PENDING,說明服務已經創建過,用OpenService打開此服務.     ⑤ 返回值為其他值, 創建武服務失敗,返回失敗.     ⑥ 調用StartService開啟服務     ⑦ 成功返回  ************************************************************************/    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 ! 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,     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); // SCM句柄  }  return bRet;  }    // 卸載驅動程式  BOOL UnloadNTDriver( char * szSvrName )  {  /************************* 卸載NT驅動的程式碼******************************     ① 調用OpenSCManager,打開SCM管理器,如果返回NULL,則返回失敗,否則繼續.     ② 調用OpenService.如果返回NULL,則返回失敗,否則繼續     ③ 調用DeleteService卸載此項服務.     ④ 成功返回.  ************************************************************************/    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); // SCM 句柄  }  return bRet;  }    void TestDriver()  {  // 測試驅動程式  HANDLE hDevice = CreateFile("\\.\HelloDDK",     GENERIC_WRITE | GENERIC_READ,     0,     NULL,     OPEN_EXISTING,     0,     NULL);  if( hDevice != INVALID_HANDLE_VALUE )  {     MessageBox(NULL,"SUCESSFULLY....ComeOn...","Yes",0);     printf( "Create Device ok ! n" );  }  else  {     printf( "Create Device faild %d ! n", GetLastError() );     MessageBox(NULL,"Faild...Fuckking...","No",0);  }  CloseHandle( hDevice );  }    int main(int argc, char* argv[])  {    UnloadNTDriver(DRIVER_NAME);  // 載入驅動  BOOL bRet = LoadNTDriver(DRIVER_NAME,DRIVER_PATH);  if (!bRet)  {     printf("LoadNTDriver errorn");     return 0;  }  // 載入成功    printf( "press any to create device!n" );  getch();    TestDriver();    // 這時候你可以通過註冊表,或其他查看符號連接的軟體驗證。  printf( "press any to unload the driver!n" );  getch();    // 卸載驅動  UnloadNTDriver(DRIVER_NAME);  // if (!bRet)  // {  //   printf("UnloadNTDriver errorn");  //   return 0;  // }  system("pause");  return 0;  }