• 加载windows驱动代码


    #include <WINDOWS.H>
    #include <winsvc.h>
    #include <conio.h>
    #include <stdio.h>

    #pragma comment(lib, "Advapi32.lib")

    BOOL LoadNTDriver(char* lpszDriverName, char* lpszDriverPath)
    {
     char szDriverImagePath[MAX_PATH];
     GetFullPathNameA(lpszDriverPath, MAX_PATH, szDriverImagePath, NULL);

     BOOL bRet = FALSE;

     SC_HANDLE hSvcMgr = NULL;
     SC_HANDLE hSvcDDK = NULL;

     hSvcMgr = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
     if (hSvcMgr == NULL)
     {
      printf("OpenSCManagerA failed : %d ", GetLastError());
      bRet = FALSE;
      goto BeforeLeave;
     }
     else
     {
      printf("OpenSCManagerA ok... ");
     }

     hSvcDDK = CreateServiceA(hSvcMgr,
      lpszDriverName,
      lpszDriverName,
      SERVICE_ALL_ACCESS,
      SERVICE_KERNEL_DRIVER,
      SERVICE_DEMAND_START,
      SERVICE_ERROR_IGNORE,
      szDriverImagePath,
      NULL, NULL, NULL, NULL, NULL);

     DWORD dwRet;
     if (hSvcDDK == NULL)
     {
      dwRet = GetLastError();
      if (dwRet != ERROR_IO_PENDING && dwRet != ERROR_SERVICE_EXISTS)
      {
       printf("CreateServiceA failed : %d ", dwRet);
       bRet = FALSE;
       goto BeforeLeave;
      }
      else
      {
       printf("CreateServiceA failed, %s is pending or has been existed... ", lpszDriverName);
      }

      hSvcDDK = OpenServiceA(hSvcDDK, lpszDriverName, SERVICE_ALL_ACCESS);
      if (hSvcDDK == NULL)
      {
       dwRet = GetLastError();
       printf("OpenServiceA failed : %d ", dwRet);
       bRet = FALSE;
       goto BeforeLeave;
      }
      else
      {
       printf("OpenServiceA ok! ");
      }
     }
     else
     {
      printf("CreateServiceA ok... ");
     }

     bRet = StartServiceA(hSvcDDK, NULL, NULL);
     if (!bRet)
     {
      DWORD dwRet = GetLastError();
      if (dwRet != ERROR_IO_PENDING && dwRet != ERROR_SERVICE_EXISTS)
      {
       printf("StartServiceA failed : %d ", dwRet);
       bRet = FALSE;
       goto BeforeLeave;
      }
      else
      {
       if (dwRet == ERROR_IO_PENDING)
       {
        printf("StartServiceA failed ERROR_IO_PENDING ");
        bRet = FALSE;
        goto BeforeLeave;
       }
       else
       {
        printf("StartServiceA failed ERROR_SERVICE_EXISTS ");
        bRet = TRUE;
        goto BeforeLeave;
       }
      }
     }
     else
     {
      printf("StartServiceA ok... ");
     }
     bRet = TRUE;
    BeforeLeave:
     if (hSvcDDK)
     {
      CloseServiceHandle(hSvcDDK);
     }
     if (hSvcMgr)
     {
      CloseServiceHandle(hSvcMgr);
     }
     return bRet;
    }

    BOOL UnloadDriver(char* lpszSvcName)
    {
     BOOL bRet = FALSE;
     SC_HANDLE hSvcMgr = NULL;
     SC_HANDLE hSvcDDK = NULL;
     SERVICE_STATUS SvcStatus;

     hSvcMgr = OpenSCManagerA(NULL, NULL, SC_MANAGER_ALL_ACCESS);
     if (hSvcMgr == NULL)
     {
      printf("OpenSCManagerA failed : %d ", GetLastError());
      bRet = FALSE;
      goto BeforeLeave;
     }
     else
     {
      printf("OpenSCManagerA ok... ");
     }

     hSvcDDK = OpenServiceA(hSvcMgr, lpszSvcName, SERVICE_ALL_ACCESS);
     if (hSvcDDK == NULL)
     {
      printf("OpenServiceA %s failed : %d ", lpszSvcName,GetLastError());
      bRet = FALSE;
      goto BeforeLeave;
     }
     else
     {
      printf("OpenServiceA %s ok... ", lpszSvcName);
     }
     if (!ControlService(hSvcDDK, SERVICE_CONTROL_STOP, &SvcStatus))
     {
      printf("ControlService failed : %d ", GetLastError());
     }
     else
     {
      printf("ControlService ok... ");
     }
     if (!DeleteService(hSvcDDK))
     {
      printf("DeleteService failed : %d ", GetLastError());
     }
     else
     {
      printf("DeleteService ok... ");
     }
     bRet = TRUE;
    BeforeLeave:
     if (hSvcDDK)
     {
      CloseServiceHandle(hSvcDDK);
     }
     if (hSvcMgr)
     {
      CloseServiceHandle(hSvcMgr);
     }
     return bRet;
    }

    int main(int argc, char** argv)
    {
     if (argc < 3)
     {
      printf("Usage : test_dev.exe load|unload ddk_svc_name sys_file_full_path ");
      return 0;
     }
     if (strcmp(argv[1], "load") == 0)
     {
      BOOL bRet = FALSE;
      bRet = LoadNTDriver(argv[2], argv[3]);
      if (!bRet)
      {
       printf("Load %s failed ", argv[2]);
       return 1;
      }
      else
      {
       printf("Load %s ok ", argv[2]);
       return 0;
      }
     }
     else if (strcmp(argv[1], "unload") == 0)
     {
      BOOL bRet = FALSE;
      bRet = UnloadDriver(argv[2]);
      if (!bRet)
      {
       printf("Unload %s failed... ", argv[2]);
       return 1;
      }
      else
      {
       printf("Unload %s ok... ", argv[2]);
       return 0;
      }
     }
    }

  • 相关阅读:
    闭包
    内置函数
    595926265989859
    C错题集锦
    C中改变指针的指向
    /dev/zero
    define的高级用法
    (转)Linux ./configure --prefix命令
    (转)linux下tty,控制台,虚拟终端,串口,console(控制台终端)详解
    内核驱动模块的Makefile模板
  • 原文地址:https://www.cnblogs.com/debug-me/p/6376441.html
Copyright © 2020-2023  润新知