内核事件通知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. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章