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