1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
/* 在线程中休眠 by exchen 2009-10-05 */ #include <ntddk.h> static KEVENT s_event; #define DELAY_ONE_MICROSECOND (-10) #define DELAY_ONE_MILLISECOND (DELAY_ONE_MICROSECOND*1000) //自定义休眠函数 VOID MySleep(LONG msec) { LARGE_INTEGER My_int; My_int.QuadPart=DELAY_ONE_MILLISECOND; My_int.QuadPart*=msec; KeDelayExecutionThread(KernelMode,0,&My_int); } VOID MyThreadProc(IN PVOID pContext) { PUNICODE_STRING usStr = (PUNICODE_STRING)pContext; DbgPrint("%wZ\n",usStr); MySleep(1000*5); //在线程中休眠5秒 KeSetEvent(&s_event,0,TRUE); PsTerminateSystemThread(0); } VOID DriverUnload(IN PDRIVER_OBJECT DriverObject) { DbgPrint("驱动已经被停止了\n"); } NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath) { HANDLE hMyThread; UNICODE_STRING usStr = RTL_CONSTANT_STRING(L"Your String"); KeInitializeEvent(&s_event,SynchronizationEvent,FALSE); PsCreateSystemThread(&hMyThread, 0, NULL, NULL, NULL, MyThreadProc, (PVOID)&usStr); KdPrint(("Wait Object Begin\n")); KeWaitForSingleObject(&s_event,Executive,KernelMode,0,0); KdPrint(("Wait Object End\n")); ZwClose(hMyThread); DriverObject->DriverUnload = DriverUnload; return STATUS_SUCCESS; } |
转载请注明:exchen's blog » 在线程中休眠