驱动开发之 创建线程函数PsCreateSystemThread

PsCreateSystemThread 创建一个执行在内核模式的系统线程。

注意:创建线程必须用函数PsTerminateSystemThread强制线程结束。否则该线程是无法自动退出的。

函数原型:

[cpp] view plain copy

print?

  1. NTSTATUS PsCreateSystemThread(
  2. _Out_      PHANDLE ThreadHandle,
  3. _In_       ULONG DesiredAccess,
  4. _In_opt_   POBJECT_ATTRIBUTES ObjectAttributes,
  5. _In_opt_   HANDLE ProcessHandle,
  6. _Out_opt_  PCLIENT_ID ClientId,
  7. _In_       PKSTART_ROUTINE StartRoutine,
  8. _In_opt_   PVOID StartContext
  9. );

[cpp] view plain copy

print?

  1. NTSTATUS PsCreateSystemThread(
  2. _Out_      PHANDLE ThreadHandle,
  3. _In_       ULONG DesiredAccess,
  4. _In_opt_   POBJECT_ATTRIBUTES ObjectAttributes,
  5. _In_opt_   HANDLE ProcessHandle,
  6. _Out_opt_  PCLIENT_ID ClientId,
  7. _In_       PKSTART_ROUTINE StartRoutine,
  8. _In_opt_   PVOID StartContext
  9. );
NTSTATUS PsCreateSystemThread(
  _Out_      PHANDLE ThreadHandle,
  _In_       ULONG DesiredAccess,
  _In_opt_   POBJECT_ATTRIBUTES ObjectAttributes,
  _In_opt_   HANDLE ProcessHandle,
  _Out_opt_  PCLIENT_ID ClientId,
  _In_       PKSTART_ROUTINE StartRoutine,
  _In_opt_   PVOID StartContext
);

参数说明

ThreadHandle [out]

返回的handle。当此handle不再使用时,调用ZwClose关闭。对于windows vista以及以后版本,这个handle是一个内核句柄。

DesiredAccess [in]

ACCESS_MASK值,创建的权限。一般取THREAD_ALL_ACCESS。

ObjectAttributes [in,
optional]

线程属性,一般设为null。

ProcessHandle [in,
optional]

线程所在地址空间的进程的handle。对于驱动线程,通常设为NULL。也可以设为NtCurrentProcess()指定为当前进程。

ClientId [out,
optional]

对于驱动线程设为null。

StartRoutine [in]

函数指针,创建的系统线程的入口指针。此函数接受一个参数,就是StartContext。

StartContext [in,
optional]

线程执行时传给StartRoutine的参数。

返回值

PsCreateSystemThread成功返回 STATUS_SUCCESS。

例子1:

[cpp] view plain copy

print?

  1. NTSTATUS lstatus;
  2. lstatus = PsCreateSystemThread( &hThread,
  3. 0,
  4. NULL, //或者THREAD_ALL_ACCESS
  5. NtCurrentProcess(),
  6. NULL,
  7. (PKSTART_ROUTINE)ThreadProc,
  8. NULL );
  9. if (!NT_SUCCESS(lstatus))
  10. {
  11. ;
  12. }

[cpp] view plain copy

print?

  1. NTSTATUS lstatus;
  2. lstatus = PsCreateSystemThread( &hThread,
  3. 0,
  4. NULL, //或者THREAD_ALL_ACCESS
  5. NtCurrentProcess(),
  6. NULL,
  7. (PKSTART_ROUTINE)ThreadProc,
  8. NULL );
  9. if (!NT_SUCCESS(lstatus))
  10. {
  11. ;
  12. }
NTSTATUS lstatus;
lstatus = PsCreateSystemThread( &hThread,
                0,
		NULL, //或者THREAD_ALL_ACCESS
		NtCurrentProcess(),
		NULL,
		(PKSTART_ROUTINE)ThreadProc,
		NULL );
if (!NT_SUCCESS(lstatus))
{
	;
}

[cpp] view plain copy

print?

  1. NTSTATUS
  2. ThreadProc()
  3. {
  4. DbgPrint("CreateThread Successfully");
  5. //创建线程必须用函数PsTerminateSystemThread强制线程结束。否则该线程是无法自动退出的。
  6. PsTerminateSystemThread(STATUS_SUCCESS);
  7. }

[cpp] view plain copy

print?

  1. NTSTATUS
  2. ThreadProc()
  3. {
  4. DbgPrint("CreateThread Successfully");
  5. //创建线程必须用函数PsTerminateSystemThread强制线程结束。否则该线程是无法自动退出的。
  6. PsTerminateSystemThread(STATUS_SUCCESS);
  7. }
NTSTATUS
	ThreadProc()
{
	DbgPrint("CreateThread Successfully");
	//创建线程必须用函数PsTerminateSystemThread强制线程结束。否则该线程是无法自动退出的。
	PsTerminateSystemThread(STATUS_SUCCESS);
}

2.提供一种方式来通知线程终止并等待终止发生。

[cpp] view plain copy

print?

  1. typedef struct _DEVICE_EXTENSION {
  2. ...
  3. KEVENT evKill;//在设备扩展中声明一个KEVENT对象
  4. PKTHREAD thread;
  5. };
  6. NTSTATUS StartThread(PDEVICE_EXTENSION pdx)
  7. {
  8. NTSTATUS status;
  9. HANDLE hthread;
  10. KeInitializeEvent(&pdx->evKill, NotificationEvent, FALSE);
  11. status = PsCreateSystemThread(&hthread,   //创建新线程
  12. THREAD_ALL_ACCESS,
  13. NULL,
  14. NULL,
  15. NULL,
  16. (PKSTART_ROUTINE) ThreadProc,
  17. pdx);
  18. if (!NT_SUCCESS(status))
  19. return status;
  20. ObReferenceObjectByHandle(hthread,        //为了等待线程终止,你需要KTHREAD对象的地址来代替从PsCreateSystemThread获得的线程句柄。
  21. THREAD_ALL_ACCESS,  //调用ObReferenceObjectByHandle获得这个地址。
  22. NULL,
  23. KernelMode,
  24. (PVOID*) &pdx->thread,
  25. NULL);
  26. ZwClose(hthread);         //现在可以关闭线程句柄了,因为已经得到thread
  27. return STATUS_SUCCESS;
  28. }
  29. VOID StopThread(PDEVICE_EXTENSION pdx)
  30. {
  31. KeSetEvent(&pdx->evKill, 0, FALSE);
  32. KeWaitForSingleObject(pdx->thread, Executive, KernelMode, FALSE, NULL);
  33. ObDereferenceObject(pdx->thread);
  34. }
  35. VOID ThreadProc(PDEVICE_EXTENSION pdx)
  36. {
  37. ...
  38. KeWaitForXxx(<at least pdx->evKill>);
  39. ...
  40. PsTerminateSystemThread(STATUS_SUCCESS);
  41. }

[cpp] view plain copy

print?

  1. typedef struct _DEVICE_EXTENSION {
  2. ...
  3. KEVENT evKill;//在设备扩展中声明一个KEVENT对象
  4. PKTHREAD thread;
  5. };
  6. NTSTATUS StartThread(PDEVICE_EXTENSION pdx)
  7. {
  8. NTSTATUS status;
  9. HANDLE hthread;
  10. KeInitializeEvent(&pdx->evKill, NotificationEvent, FALSE);
  11. status = PsCreateSystemThread(&hthread,   //创建新线程
  12. THREAD_ALL_ACCESS,
  13. NULL,
  14. NULL,
  15. NULL,
  16. (PKSTART_ROUTINE) ThreadProc,
  17. pdx);
  18. if (!NT_SUCCESS(status))
  19. return status;
  20. ObReferenceObjectByHandle(hthread,        //为了等待线程终止,你需要KTHREAD对象的地址来代替从PsCreateSystemThread获得的线程句柄。
  21. THREAD_ALL_ACCESS,  //调用ObReferenceObjectByHandle获得这个地址。
  22. NULL,
  23. KernelMode,
  24. (PVOID*) &pdx->thread,
  25. NULL);
  26. ZwClose(hthread);         //现在可以关闭线程句柄了,因为已经得到thread
  27. return STATUS_SUCCESS;
  28. }
  29. VOID StopThread(PDEVICE_EXTENSION pdx)
  30. {
  31. KeSetEvent(&pdx->evKill, 0, FALSE);
  32. KeWaitForSingleObject(pdx->thread, Executive, KernelMode, FALSE, NULL);
  33. ObDereferenceObject(pdx->thread);
  34. }
  35. VOID ThreadProc(PDEVICE_EXTENSION pdx)
  36. {
  37. ...
  38. KeWaitForXxx(<at least pdx->evKill>);
  39. ...
  40. PsTerminateSystemThread(STATUS_SUCCESS);
  41. }
typedef struct _DEVICE_EXTENSION {
  ...
  KEVENT evKill;//在设备扩展中声明一个KEVENT对象
  PKTHREAD thread;
};

NTSTATUS StartThread(PDEVICE_EXTENSION pdx)
{
  NTSTATUS status;
  HANDLE hthread;
  KeInitializeEvent(&pdx->evKill, NotificationEvent, FALSE);
  status = PsCreateSystemThread(&hthread,	//创建新线程
				THREAD_ALL_ACCESS,
				NULL,
				NULL,
				NULL,
				(PKSTART_ROUTINE) ThreadProc,
				pdx);
  if (!NT_SUCCESS(status))
    return status;
  ObReferenceObjectByHandle(hthread,		//为了等待线程终止,你需要KTHREAD对象的地址来代替从PsCreateSystemThread获得的线程句柄。
			    THREAD_ALL_ACCESS,  //调用ObReferenceObjectByHandle获得这个地址。
			    NULL,
			    KernelMode,
			    (PVOID*) &pdx->thread,
			    NULL);
  ZwClose(hthread);			//现在可以关闭线程句柄了,因为已经得到thread
  return STATUS_SUCCESS;
}

VOID StopThread(PDEVICE_EXTENSION pdx)
{
  KeSetEvent(&pdx->evKill, 0, FALSE);
  KeWaitForSingleObject(pdx->thread, Executive, KernelMode, FALSE, NULL);
  ObDereferenceObject(pdx->thread);
}

VOID ThreadProc(PDEVICE_EXTENSION pdx)
{
  ...
  KeWaitForXxx(<at least pdx->evKill>);
  ...
  PsTerminateSystemThread(STATUS_SUCCESS);
}

jpg改rar

时间: 2025-01-06 11:46:01

驱动开发之 创建线程函数PsCreateSystemThread的相关文章

[笔记]linux下和windows下的 创建线程函数

linux下和windows下的 创建线程函数 1 #ifdef __GNUC__ 2 //Linux 3 #include <pthread.h> 4 #define CreateThreadEx(tid,threadFun,args) pthread_create(tid, 0, threadFun, args) 5 #define CloseHandle(ph) 6 7 int pthread_create( 8 //指向线程标识符的指针. 9 pthread_t *restrict t

创建线程函数的方法

1.线程函数 在启动一个线程之前,必须为线程编写一个全局的线程函数,这个线程函数接受一个32位的LPVOID作为参数,返回一个UINT,线程函数的结构为: UINT ThreadFunction(LPVOID pParam) { //线程处理代码 return0; } 在线程处理代码部分通常包括一个死循环,该循环中先等待某事情的发生,再处理相关的工作: while(1) { WaitForSingleObject(-,-);//或WaitForMultipleObjects(-) //Do so

Windows驱动开发-手动创建IRP

手动创建IRP有以下几个步骤: 1,先得到设备的指针,一种方法是用IoGetDeviceObjectPointer内核函数得到设备对象指针,另外一种方法是用zwCreateFile内核函数先得到设备句柄,然后调用ObReferenceObjectByHandle内核方法通过设备句柄得到设备对象指针: 2,手动创建IRP,有4个内核函数可以选择,IoBuildSychronousFsdRequest,IoBuildAsychronousFsdRequest,IoBuildDeviceControl

Windows驱动开发-内核常用内存函数

搞内存常用函数 C语言 内核 malloc ExAllocatePool memset RtlFillMemory memcpy RtlMoveMemory free ExFreePool 原文地址:https://www.cnblogs.com/a-s-m/p/12330596.html

linux线程笔记1之创建线程

1 线程与进程的对比 这里有一个笔记详细的阐述 http://blog.csdn.net/laviolette/article/details/51506953 2 创建线程函数 int pthread_create(pthread_t *thread, const pthread_attr_t *attr,                          void *(*start_routine) (void *), void *arg); 参数意义: typedef unsigned l

iOS开发多线程篇—创建线程

iOS开发多线程篇—创建线程 一.创建和启动线程简单说明 一个NSThread对象就代表一条线程 创建.启动线程 (1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; [thread start]; // 线程一启动,就会在线程thread中执行self的run方法 主线程相关用法 + (NSThread *)mainThread; // 获得主线程 -

Windows 驱动开发基础(九)内核函数

Windows 驱动开发基础系列,转载请标明出处:http://blog.csdn.net/ikerpeng/article/details/38849861 这里主要介绍3类Windows的内核函数:字符串处理函数,文件操作函数, 注册表读写函数.(这些函数都是运行时函数,所以都有Rtl字样) 1 字符串处理函数 首先驱动程序中,常用的字符串包括4种:CHAR (打印的时候注意小写%s), WCHAR(打印的时候注意大写%S), ANSI_STRING, UNICODE_STRING.后面两种

【转】线程池体系介绍及从阿里Java开发手册学习线程池的正确创建方法

jdk1.7中java.util.concurrent.Executor线程池体系介绍 java.util.concurrent.Executor : 负责线程的使用与调度的根接口  |–ExecutorService:Executor的子接口,线程池的主要接口  |–ThreadPoolExecutor:ExecutorService的实现类  |–ScheduledExecutorService:ExecutorService的子接口,负责线程的调度  |–ScheduledThreadPo

【C++】【MFC】创建新的线程函数

DWORD WINAPI MyThreadProc (LPVOID lpParam){ somestruct* pN = (somestruct*)lpParam; // 将参数转为你的类型 ... return 0;} 创建命令以及各个参数说明:HANDLE hThread = CreateThread( NULL, // 没有安全描述符 0, // 默认线程栈的大小 MyThreadProc, // 线程函数指针 (LPVOID)&n, // 传递参数 NULL, // 没有附加属性 NUL