内核事件通知KeWaitForSingleObject

  1. from:   http://blog.csdn.net/apxar/article/details/10865635

  2. /*  
  3. 使用事件同步,等待线程中的函数DbgPrint结束之后,外面生成线程的函数再返回。     
  4.   
  5. */  
  6.   
  7.   
  8.   
  9.   
  10. #include <ntddk.h>  
  11.   
  12.   
  13. static KEVENT event;  
  14.   
  15.   
  16. VOID MyThread()//线程调用的函数  
  17. {  
  18.   
  19.   
  20.   
  21.   
  22. DbgPrint("Create Thread");  
  23.   
  24.   
  25. KeSetEvent(&event,0,TRUE);//事件能获取有信号状态  
  26.   
  27.   
  28. PsTerminateSystemThread(STATUS_SUCCESS);  
  29.   
  30.   
  31. }  
  32.   
  33.   
  34.   
  35.   
  36.   
  37.   
  38. void DriverUnload( PDRIVER_OBJECT DriverObject)  
  39. {  
  40.   
  41.   
  42.   
  43.   
  44.   
  45.   
  46. }  
  47. void  fun()  
  48. {     
  49.   
  50.   
  51.   
  52.   
  53.     HANDLE threadHandle=NULL;  
  54.     NTSTATUS status;  
  55.   
  56.   
  57.     //KeInitializeEvent() 事件初始化  
  58.     //KeWaitForSingleObject()  
  59.     //KeSetEvent()  
  60.   
  61.   
  62.   
  63.   
  64.     KeInitializeEvent(  
  65.         &event,  
  66.         SynchronizationEvent,//SynchronizationEvent为同步事件  
  67.         FALSE//  当是TRUE 时初始化事件是有信号状态.,当是FALSE时初始化事件是没信号状态,如果此处为TRUE,则为有信号状态,KeWaitForSingleObject会直接通过,此时需要调用KeResetEvent来设置为无信号  
  68.         );  
  69.   
  70.   
  71.       //KeResetEvent(&event);//指定的事件对象设置为无信号状态。  
  72.   
  73.   
  74.   
  75.   
  76.   
  77.   
  78.     status=PsCreateSystemThread(  //创建线程  
  79.         &threadHandle,  
  80.         THREAD_ALL_ACCESS,  
  81.         NULL,  
  82.         NULL,  
  83.         NULL,  
  84.         MyThread,//调用的函数  
  85.         NULL  //PVOID StartContext 传递给函数的参数  
  86.         );  
  87.   
  88.   
  89.      if(!NT_SUCCESS(status))  
  90.           
  91.          return STATUS_UNSUCCESSFUL;  
  92.   
  93.   
  94.   
  95.   
  96.   
  97.   
  98.     //等待信号  
  99.     KeWaitForSingleObject (   
  100.         &event,//可以为 时间  信号,线程,时钟,互斥对象  
  101.         Executive,//等待  
  102.         KernelMode ,  
  103.         FALSE,  
  104.         0  
  105.         );  
  106.       
  107.     DbgPrint("Create Thread has return");  
  108.     ZwClose(threadHandle);  
  109. }  
  110.   
  111.   
  112.   
  113.   
  114. NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,PUNICODE_STRING RegistryPath)  
  115. {    
  116.   
  117.   
  118.   
  119.   
  120.     DriverObject->DriverUnload = DriverUnload;  
  121.      
  122.     fun();  
  123.       
  124.     return STATUS_SUCCESS;  
  125. }  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值