阅读:1377回复:7
结束线程的问题
按照walter的方法,结束线程时发送事件,
我是这么运用的,在DriverEntry中startthread,在DriverUnload中stopthread,但是问题是DriverUnload的参数是没有设备扩展的,这么操作那个事件? walter的例子: typedef struct _DEVICE_EXTENSION { KEVENT evKill; PKTHREAD thread; }; NTSTATUS StartThread(PDEVICE_EXTENSION pdx) { NTSTATUS status; HANDLE hthread; OBJECT_ATTRIBUTES oa; InitializeObjectAttributes(&oa, NULL, OBJ_KERNEL_HANDLE, NULL, NULL); status = PsCreateSystemThread(&hthread, THREAD_ALL_ACCESS, &oa, NULL, NULL, (PKSTART_ROUTINE) ThreadProc, pdx); if (!NT_SUCCESS(status)) return status; ObReferenceObjectByHandle(hthread, THREAD_ALL_ACCESS, NULL, KernelMode, (PVOID*) &pdx->thread, NULL); ZwClose(hthread); 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); } 我的运用 NTSTATUS DriverEntry( IN PDRIVER_OBJECT DriverObject, IN PUNICODE_STRING RegistryPath ) { PDEVICE_OBJECT DeviceObject = NULL; PDEVICE_EXTENSION DeviceExtension; .... ntStatus = IoCreateDevice( DriverObject, sizeof(DEVICE_EXTENSION), // DeviceExtensionSize &ntDeviceName, // DeviceName FILE_DEVICE, // DeviceType 0, // DeviceCharacteristics TRUE, // Exclusive &DeviceObject // [OUT] ); DeviceExtension = (PDEVICE_EXTENSION)DeviceObject->DeviceExtension; ..... Startthread(DeviceExtension); } DriverUnload(IN PDRIVER_OBJECT DriverObject) { //只有 DriverObject怎么操作那个DeviceExtension? //是不是就是那个(DriverObject->DeviceObject)->DeviceExtension? } [编辑 - 11/27/04 by pwpwpw123] [编辑 - 11/27/04 by pwpwpw123] [编辑 - 11/27/04 by pwpwpw123] |
|
沙发#
发布于:2004-11-27 19:20
StopThread(DeviceObject->DeviceExtension)
|
|
|
板凳#
发布于:2004-11-27 20:20
在unload的时候通常设备对象都已经删除了
所以(DriverObject->DeviceObject)->DeviceExtension应该不行。 为什么一定要在unload的时候停止线程呢,其它的地方不行吗 |
|
地板#
发布于:2004-11-27 22:18
其他地方肯定可以阿
|
|
地下室#
发布于:2004-11-29 04:02
全局变量不能用么.....
|
|
5楼#
发布于:2004-11-29 11:03
VOID
Unload( IN PDRIVER_OBJECT DriverObject ) { StopThread(DriverObject->DeviceObject)->DeviceExtension) IoDeleteSymbolicLink(...) IoDeleteDevice( DriverObject->DeviceObject); ... } |
|
|
6楼#
发布于:2004-11-30 21:12
在这个 IRP_MN_STOP_DEVICE irp时stopthread
|
|
7楼#
发布于:2004-12-01 05:51
Could you try to StartThread in AddDevice and StopThread when IRP_MN_STOP_DEVICE as ananda recommented? StartThread in DriverEntry might cause re-entry problem if there are multiple devices installed. Certainly, if you want it to be global, being used by multiple device instances, your design is the only way.
|
|