Windows内核学习笔记-- 编写程序手动加载驱动程序

本文介绍了一个用于Windows NT内核的驱动程序如何被加载和卸载的过程。包括使用SCM管理器创建、启动、停止及删除服务的步骤,并演示了通过API函数实现这一流程的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. #include <windows.h>    
  2. #include <winsvc.h>    
  3. #include <conio.h>    
  4. #include <stdio.h>  
  5. #define DRIVER_NAME "HelloDriver"  
  6. #define DRIVER_PATH "..//MyDriver//HelloDriver.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. }   
  190. void TestDriver()  
  191. {  
  192.     //测试驱动程序    
  193.     HANDLE hDevice = CreateFile("////.//HelloDDK",    
  194.         GENERIC_WRITE | GENERIC_READ,    
  195.         0,    
  196.         NULL,    
  197.         OPEN_EXISTING,    
  198.         0,    
  199.         NULL);    
  200.     if( hDevice != INVALID_HANDLE_VALUE )    
  201.     {  
  202.         printf( "Create Device ok ! /n" );    
  203.     }  
  204.     else    
  205.     {  
  206.         printf( "Create Device faild %d ! /n", GetLastError() );    
  207.     }  
  208.     CloseHandle( hDevice );  
  209. }   
  210. int main(int argc, char* argv[])    
  211. {  
  212.     //加载驱动  
  213.     BOOL bRet = LoadNTDriver(DRIVER_NAME,DRIVER_PATH);  
  214.     if (!bRet)  
  215.     {  
  216.         printf("LoadNTDriver error/n");  
  217.         return 0;  
  218.     }  
  219.     //加载成功  
  220.     printf( "press any to create device!/n" );    
  221.     getch();    
  222.     TestDriver();  
  223.     //这时候你可以通过注册表,或其他查看符号连接的软件验证。    
  224.     printf( "press any to unload the driver!/n" );    
  225.     getch();    
  226.     //卸载驱动  
  227.     UnloadNTDriver(DRIVER_NAME);  
  228.     if (!bRet)  
  229.     {  
  230.         printf("UnloadNTDriver error/n");  
  231.         return 0;  
  232.     }  
  233.     return 0;    
  234. }    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值