内核中线程的创建与销毁

用PsCreateSystemThread来在内核中创建线程。读书笔记而已,高手飘过好 了~~~~~

先用KmdManager加载驱动,然后在DebugView中查看。。。。

SysThread.c部分代码

C代码   收藏代码
  1. NTSTATUS  
  2. DriverEntry(  
  3. IN PDRIVER_OBJECT pDriverObject,  
  4. IN PUNICODE_STRING regPath  
  5. )  
  6. {  
  7. PDEVICE_OBJECT pDeviceObject = NULL;  
  8. NTSTATUS ntStatus;  
  9. UNICODE_STRING uniNtNameString, uniWin32NameString;  
  10.   
  11.   
  12. RtlInitUnicodeString( &uniNtNameString, NT_DEVICE_NAME );  
  13. ntStatus = IoCreateDevice (  
  14.             pDriverObject,  
  15.                            sizeof(SYSTHREAD_DEVICE_EXTENSION), // DeviceExtensionSize  
  16.                            &uniNtNameString,  
  17.                            FILE_DEVICE_UNKNOWN,         //  
  18.                            0,              // No standard device characteristics  
  19.                            FALSE,             // not exclusive device  
  20.                            &pDeviceObject  
  21.                            );  
  22. if( !NT_SUCCESS(ntStatus) ) {  
  23.          return ntStatus;  
  24. }  
  25.   
  26. // 派遣函数  
  27. pDriverObject->MajorFunction[IRP_MJ_CREATE] = SysThreadOpen;  
  28. pDriverObject->MajorFunction[IRP_MJ_CLOSE] = SysThreadClose;  
  29. pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = SysThreadDeviceIoControl;  
  30. pDriverObject->DriverUnload = SysThreadUnload;  
  31.   
  32. pDeviceObject->Flags |= DO_BUFFERED_IO;  
  33.   
  34.   
  35. RtlInitUnicodeString( &uniWin32NameString, DOS_DEVICE_NAME );  
  36. ntStatus = IoCreateSymbolicLink( &uniWin32NameString, &uniNtNameString );  
  37. if (!NT_SUCCESS(ntStatus)){  
  38.          IoDeleteDevice( pDriverObject->DeviceObject );  
  39. }  
  40.   
  41. return ntStatus;  
  42. }  
  43. ///  
  44. ///  
  45.   
  46. void  
  47. SysThreadUnload(  
  48. IN PDRIVER_OBJECT pDriverObject  
  49. )  
  50. {  
  51. PDEVICE_OBJECT pDeviceObject;  
  52. UNICODE_STRING uniWin32NameString;  
  53.   
  54. pDeviceObject = pDriverObject->DeviceObject;  
  55.   
  56. RtlInitUnicodeString( &uniWin32NameString, DOS_DEVICE_NAME );  
  57. IoDeleteSymbolicLink( &uniWin32NameString );  
  58. IoDeleteDevice( pDriverObject->DeviceObject );  
  59. }  
  60. ///  
  61. ///  
  62.   
  63. NTSTATUS  
  64. SysThreadOpen(  
  65. IN PDEVICE_OBJECT pDeviceObject,  
  66. IN PIRP pIrp  
  67. )  
  68. {  
  69. KdPrint((" SysThreadOpen() was Called.... \n"));  
  70. pIrp->IoStatus.Status = STATUS_SUCCESS;  
  71. pIrp->IoStatus.Information = 0;  
  72. IoCompleteRequest( pIrp, IO_NO_INCREMENT );  
  73. return STATUS_SUCCESS;  
  74. }  
  75.   
  76. ///  
  77. ///  
  78.   
  79. NTSTATUS  
  80. SysThreadClose(  
  81. IN PDEVICE_OBJECT pDeviceObject,  
  82. IN PIRP pIrp  
  83. )  
  84. {  
  85. KdPrint((" SysThreadClose() was Called.... \n"));  
  86. pIrp->IoStatus.Status = STATUS_SUCCESS;  
  87. pIrp->IoStatus.Information = 0;  
  88. IoCompleteRequest( pIrp, IO_NO_INCREMENT );  
  89. return STATUS_SUCCESS;  
  90. }  
  91. ///  
  92. ///  
  93.   
  94. NTSTATUS  
  95. SysThreadDeviceIoControl(  
  96. IN PDEVICE_OBJECT pDeviceObject,  
  97. IN PIRP pIrp  
  98. )  
  99. {  
  100. NTSTATUS ntStatus = STATUS_SUCCESS;  
  101. PIO_STACK_LOCATION pIrpStack;  
  102. PSYSTHREAD_DEVICE_EXTENSION pdx;  
  103. ULONG dwControlCode;  
  104.   
  105. pdx = (PSYSTHREAD_DEVICE_EXTENSION) pDeviceObject->DeviceExtension;  
  106. pIrpStack = IoGetCurrentIrpStackLocation( pIrp );  
  107. dwControlCode = pIrpStack->Parameters.DeviceIoControl.IoControlCode;  
  108.   
  109. switch(dwControlCode)  
  110. {  
  111.          case IOCTL_SYSTHREAD_START:  
  112.           StartThread(pdx);          //线程开始  
  113.           break;  
  114.   
  115.          case IOCTL_SYSTHREAD_STOP:  
  116.           StopThread(pdx);          //线程结束  
  117.           break;  
  118.   
  119.          default:  
  120.          break;  
  121. }  
  122.   
  123. pIrp->IoStatus.Status = STATUS_SUCCESS;  
  124. pIrp->IoStatus.Information = 0;  
  125. IoCompleteRequest( pIrp, IO_NO_INCREMENT );  
  126.   
  127. return ntStatus;  
  128. }  
  129.   
  130. ///  
  131. ///  
  132. NTSTATUS StartThread(PSYSTHREAD_DEVICE_EXTENSION pdx)  
  133. {  
  134. NTSTATUS status;  
  135. HANDLE hthread;  
  136.            //初始化event对象  
  137. KeInitializeEvent(&pdx->evKill,  
  138.              SynchronizationEvent, // auto reset  
  139.              FALSE                   // initial state : FALSE ==> non-signaled  
  140.              );  
  141.            //创建ThreadProc  
  142. status = PsCreateSystemThread(&hthread,  
  143.                                          THREAD_ALL_ACCESS,  
  144.                  NULL,  
  145.                  NULL,  
  146.                  NULL,  
  147.                  (PKSTART_ROUTINE) ThreadProc,  
  148.                  pdx  
  149.                 );  
  150. if( !NT_SUCCESS(status))  
  151. {  
  152.                KdPrint(("Fail Start ThreadProc()!\n"));  
  153.                return status;  
  154. }  
  155. ObReferenceObjectByHandle(         hthread,  
  156.                THREAD_ALL_ACCESS,  
  157.                NULL,  
  158.                KernelMode,  
  159.                (PVOID *) &pdx->thread,  
  160.                NULL  
  161.                );  
  162.   
  163. ZwClose(hthread);  
  164. return STATUS_SUCCESS;  
  165.   
  166. }  
  167. ///  
  168. ///  
  169.   
  170. VOID StopThread(PSYSTHREAD_DEVICE_EXTENSION pdx)  
  171. {  
  172. KeSetEvent(&pdx->evKill, 0, FALSE); //通过KeSetEvent事件结束线程  
  173.            KeWaitForSingleObject(pdx->thread, Executive, KernelMode, FALSE, NULL);  
  174. ObDereferenceObject(pdx->thread);  
  175. }  
  176. ///  
  177. ///  
  178. VOID ThreadProc(PSYSTHREAD_DEVICE_EXTENSION pdx)  
  179. {  
  180. NTSTATUS status;  
  181. int cnt = 0;  
  182.   
  183. LARGE_INTEGER timeout;  
  184. timeout.QuadPart = -1 * 10000000; // 1 second  
  185.            //通过设置超时,每隔一秒打印一句话  
  186. while(1)  
  187. {  
  188.          status = KeWaitForSingleObject(&pdx->evKill, Executive, KernelMode, FALSE, &timeout);  
  189.          if( status == STATUS_TIMEOUT )  
  190.           KdPrint(("^_^ ThreadProc()运行了%d秒!\n", cnt++));  
  191.          else  
  192.           break;  
  193. }  
  194.            KdPrint(("^_^ ThreadProc()停止!\n"));  
  195. PsTerminateSystemThread(STATUS_SUCCESS);  
  196. }  
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值