驱动学习笔记二:加载驱动

来源:互联网 发布:360网络联盟 编辑:程序博客网 时间:2024/04/30 13:40

下面的代码是使用的SCM加载的驱动,非是什么猥琐方法...思想委琐的人可以去蹲墙角去了。驱动名和驱动路径之前定义过了...如果想写个加载工具,是可以参考下InstDrv工具源代码的。方法都一样的,呵呵

  1. #include <windows.h>  
  2. #include <winsvc.h>  
  3. #include <conio.h>  
  4. #include <stdio.h>
  5. #define DRIVER_NAME "HelloDDK"
  6. #define DRIVER_PATH "..//MyDriver//MyDriver_Check//HelloDDK.sys"
  7. //装载NT驱动程序
  8. BOOL LoadNTDriver(char* lpszDriverName,char* lpszDriverPath)
  9. {
  10.     char szDriverImagePath[256];
  11.     //得到完整的驱动路径
  12.     GetFullPathName(lpszDriverPath, 256, szDriverImagePath, NULL);
  13.     BOOL bRet = FALSE;
  14.     SC_HANDLE hServiceMgr=NULL;//SCM管理器的句柄
  15.     SC_HANDLE hServiceDDK=NULL;//NT驱动程序的服务句柄
  16.     //打开服务控制管理器
  17.     hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
  18.     if( hServiceMgr == NULL )  
  19.     {
  20.         //OpenSCManager失败
  21.         printf( "OpenSCManager() Faild %d ! /n", GetLastError() );
  22.         bRet = FALSE;
  23.         goto BeforeLeave;
  24.     }
  25.     else
  26.     {
  27.         ////OpenSCManager成功
  28.         printf( "OpenSCManager() ok ! /n" );  
  29.     }
  30.     //创建驱动所对应的服务
  31.     hServiceDDK = CreateService( hServiceMgr,
  32.         lpszDriverName, //驱动程序的在注册表中的名字  
  33.         lpszDriverName, // 注册表驱动程序的 DisplayName 值  
  34.         SERVICE_ALL_ACCESS, // 加载驱动程序的访问权限  
  35.         SERVICE_KERNEL_DRIVER,// 表示加载的服务是驱动程序  
  36.         SERVICE_DEMAND_START, // 注册表驱动程序的 Start 值  
  37.         SERVICE_ERROR_IGNORE, // 注册表驱动程序的 ErrorControl 值  
  38.         szDriverImagePath, // 注册表驱动程序的 ImagePath 值  
  39.         NULL,  
  40.         NULL,  
  41.         NULL,  
  42.         NULL,  
  43.         NULL);  
  44.     DWORD dwRtn;
  45.     //判断服务是否失败
  46.     if( hServiceDDK == NULL )  
  47.     {  
  48.         dwRtn = GetLastError();
  49.         if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS )  
  50.         {  
  51.             //由于其他原因创建服务失败
  52.             printf( "CrateService() Faild %d ! /n", dwRtn );  
  53.             bRet = FALSE;
  54.             goto BeforeLeave;
  55.         }  
  56.         else  
  57.         {
  58.             //服务创建失败,是由于服务已经创立过
  59.             printf( "CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! /n" );  
  60.         }
  61.         // 驱动程序已经加载,只需要打开  
  62.         hServiceDDK = OpenService( hServiceMgr, lpszDriverName, SERVICE_ALL_ACCESS );  
  63.         if( hServiceDDK == NULL )  
  64.         {
  65.             //如果打开服务也失败,则意味错误
  66.             dwRtn = GetLastError();  
  67.             printf( "OpenService() Faild %d ! /n", dwRtn );  
  68.             bRet = FALSE;
  69.             goto BeforeLeave;
  70.         }  
  71.         else 
  72.         {
  73.             printf( "OpenService() ok ! /n" );
  74.         }
  75.     }  
  76.     else  
  77.     {
  78.         printf( "CrateService() ok ! /n" );
  79.     }
  80.     //开启此项服务
  81.     bRet= StartService( hServiceDDK, NULL, NULL );  
  82.     if( !bRet )  
  83.     {  
  84.         DWORD dwRtn = GetLastError();  
  85.         if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING )  
  86.         {  
  87.             printf( "StartService() Faild %d ! /n", dwRtn );  
  88.             bRet = FALSE;
  89.             goto BeforeLeave;
  90.         }  
  91.         else  
  92.         {  
  93.             if( dwRtn == ERROR_IO_PENDING )  
  94.             {  
  95.                 //设备被挂住
  96.                 printf( "StartService() Faild ERROR_IO_PENDING ! /n");
  97.                 bRet = FALSE;
  98.                 goto BeforeLeave;
  99.             }  
  100.             else  
  101.             {  
  102.                 //服务已经开启
  103.                 printf( "StartService() Faild ERROR_SERVICE_ALREADY_RUNNING ! /n");
  104.                 bRet = TRUE;
  105.                 goto BeforeLeave;
  106.             }  
  107.         }  
  108.     }
  109.     bRet = TRUE;
  110. //离开前关闭句柄
  111. BeforeLeave:
  112.     if(hServiceDDK)
  113.     {
  114.         CloseServiceHandle(hServiceDDK);
  115.     }
  116.     if(hServiceMgr)
  117.     {
  118.         CloseServiceHandle(hServiceMgr);
  119.     }
  120.     return bRet;
  121. }
  122. //卸载驱动程序  
  123. BOOL UnloadNTDriver( char * szSvrName )  
  124. {
  125.     BOOL bRet = FALSE;
  126.     SC_HANDLE hServiceMgr=NULL;//SCM管理器的句柄
  127.     SC_HANDLE hServiceDDK=NULL;//NT驱动程序的服务句柄
  128.     SERVICE_STATUS SvrSta;
  129.     //打开SCM管理器
  130.     hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );  
  131.     if( hServiceMgr == NULL )  
  132.     {
  133.         //带开SCM管理器失败
  134.         printf( "OpenSCManager() Faild %d ! /n", GetLastError() );  
  135.         bRet = FALSE;
  136.         goto BeforeLeave;
  137.     }  
  138.     else  
  139.     {
  140.         //带开SCM管理器失败成功
  141.         printf( "OpenSCManager() ok ! /n" );  
  142.     }
  143.     //打开驱动所对应的服务
  144.     hServiceDDK = OpenService( hServiceMgr, szSvrName, SERVICE_ALL_ACCESS );  
  145.     if( hServiceDDK == NULL )  
  146.     {
  147.         //打开驱动所对应的服务失败
  148.         printf( "OpenService() Faild %d ! /n", GetLastError() );  
  149.         bRet = FALSE;
  150.         goto BeforeLeave;
  151.     }  
  152.     else  
  153.     {  
  154.         printf( "OpenService() ok ! /n" );  
  155.     }  
  156.     //停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。  
  157.     if( !ControlService( hServiceDDK, SERVICE_CONTROL_STOP , &SvrSta ) )  
  158.     {  
  159.         printf( "ControlService() Faild %d !/n", GetLastError() );  
  160.     }  
  161.     else  
  162.     {
  163.         //打开驱动所对应的失败
  164.         printf( "ControlService() ok !/n" );  
  165.     }  
  166.     //动态卸载驱动程序。  
  167.     if( !DeleteService( hServiceDDK ) )  
  168.     {
  169.         //卸载失败
  170.         printf( "DeleteSrevice() Faild %d !/n", GetLastError() );  
  171.     }  
  172.     else  
  173.     {  
  174.         //卸载成功
  175.         printf( "DelServer:eleteSrevice() ok !/n" );  
  176.     }  
  177.     bRet = TRUE;
  178. BeforeLeave:
  179. //离开前关闭打开的句柄
  180.     if(hServiceDDK)
  181.     {
  182.         CloseServiceHandle(hServiceDDK);
  183.     }
  184.     if(hServiceMgr)
  185.     {
  186.         CloseServiceHandle(hServiceMgr);
  187.     }
  188.     return bRet;    
  189. void TestDriver()
  190. {
  191.     //测试驱动程序  
  192.     HANDLE hDevice = CreateFile("////.//HelloDDK",  
  193.         GENERIC_WRITE | GENERIC_READ,  
  194.         0,  
  195.         NULL,  
  196.         OPEN_EXISTING,  
  197.         0,  
  198.         NULL);  
  199.     if( hDevice != INVALID_HANDLE_VALUE )  
  200.     {
  201.         printf( "Create Device ok ! /n" );  
  202.     }
  203.     else  
  204.     {
  205.         printf( "Create Device faild %d ! /n", GetLastError() );  
  206.     }
  207.     CloseHandle( hDevice );
  208. int main(int argc, char* argv[])  
  209. {
  210.     //加载驱动
  211.     BOOL bRet = LoadNTDriver(DRIVER_NAME,DRIVER_PATH);
  212.     if (!bRet)
  213.     {
  214.         printf("LoadNTDriver error/n");
  215.         return 0;
  216.     }
  217.     //加载成功
  218.     printf( "press any to create device!/n" );  
  219.     getch();  
  220.     TestDriver();
  221.     //这时候你可以通过注册表,或其他查看符号连接的软件验证。  
  222.     printf( "press any to unload the driver!/n" );  
  223.     getch();  
  224.     //卸载驱动
  225.     UnloadNTDriver(DRIVER_NAME);
  226.     if (!bRet)
  227.     {
  228.         printf("UnloadNTDriver error/n");
  229.         return 0;
  230.     }
  231.     return 0;  
  232. }  
原创粉丝点击