#include<ntddk.h>
#include<ntstrsafe.h>
//提供一个Unload函数只是为了让这个程序能动态卸载
VOID DriverUnload(PDRIVER_OBJECT driver) {
DbgPrint("first:Our driver is unloading...\r\n");
}
struct ThreadStruct
{
HANDLE ThreadHandle[20];//线程句柄
UNICODE_STRING str;
NTSTATUS ThreadStatus;
UINT32 i;
KEVENT kevent;//同步事件结构
}thread_struct;
#define ts thread_struct
VOID ThreadProcess(IN PVOID context) {
PUINT32 pint32 = (PUINT32)context;
NTSTATUS threadStopStatus=NULL;
KeSetEvent(&thread_struct.kevent, 0, TRUE);//设置事件
DbgPrint("线程已启动:%d\r\n",(*pint32));
PsTerminateSystemThread(STATUS_SUCCESS);
}
VOID ThreadOpen() {
KeInitializeEvent(&thread_struct.kevent, SynchronizationEvent, TRUE);//初始化
thread_struct.ThreadStatus = PsCreateSystemThread(&ts.ThreadHandle[ts.i], 0, NULL, NULL, NULL, ThreadProcess, (PVOID)&ts.i);
if (ts.ThreadStatus == STATUS_SUCCESS) {
DbgPrint("成功线程:%d\r\n",ts.i);
}
else {
DbgPrint("失败线程");
}
KeWaitForSingleObject(&thread_struct.kevent, Executive, KernelMode, 0, 0);//阻塞
}
NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path) {
#if DBG
__asm int 3
#endif // DBG
ts.i = 0;
while (1)
{
if (ts.i < 10) {
ThreadOpen();
++ts.i;
}
else {
break;
}
}
ts.i = 0;
while (1)
{
if (ts.i < 10) {
ZwClose(&ts.ThreadHandle[ts.i]);
++ts.i;
}
else {
break;
}
}
driver->DriverUnload = DriverUnload;
return STATUS_SUCCESS;
}
//DbgPrint("当前KernelModule中断级别:%d.\r\n",kil);
//设置一个卸载函数,便于这个函数退出
Win内核使用线程同步事件例子
最新推荐文章于 2022-03-17 16:17:10 发布