1. NtClose is a generic routine that operates on any type of object.
2. Closing an open object handle causes that handle to become invalid. The system also decrements the handle count for the object and checks whether the object can be deleted. The system does not actually delete the object until all of the object's handles are closed and no referenced pointers remain.
3. A driver must close every handle that it opens as soon as the handle is no longer required. Kernel handles, which are those that are opened by a system thread or by specifying the OBJ_KERNEL_HANDLE flag, can be closed only when the previous processor mode is KernelMode. This requirement applies both to system threads and to dispatch routines for IRPs that were issued from kernel mode. (For more information about the previous processor mode, see ExGetPreviousMode.) For example, a handle that NtCreateKey returns to a DriverEntry routine cannot subsequently be closed by the same driver's dispatch routines. A DriverEntry routine runs in a system process, whereas dispatch routines usually run either in the context of the thread issuing the current I/O request, or, for lower-level drivers, in an arbitrary thread context.
4. A nonkernel handle can be closed only if one of two conditions is met: The previous processor mode is KernelMode, or the calling thread has sufficient permission to close the handle. An example of the latter occurs when the calling thread is the one that created the handle.
5. Callers of NtClose should not assume that this routine automatically waits for all I/O to complete prior to returning.
1. NtClose是一个在任何类型的对象上运行的通用例程。
2. 关闭打开的对象句柄会导致该句柄无效。系统还会减少对象的句柄计数,并检查是否可以删除该对象。在关闭所有对象的句柄并且不保留引用的指针之前,系统实际上不会删除该对象。
3. 一旦不再需要句柄,驱动程序必须关闭它打开的每个句柄。内核句柄是由系统线程打开或通过指定OBJ_KERNEL_HANDLE标志的句柄,只有在先前的处理器模式为KernelMode时才能关闭。此要求适用于系统线程和分配从内核模式发出的IRP的例程。 (有关先前处理器模式的更多信息,请参阅ExGetPreviousMode。)例如,NtCreateKey返回到DriverEntry例程的句柄随后不能由相同的驱动程序的调度例程关闭。 DriverEntry例程在系统进程中运行,而调度例程通常在发出当前I / O请求的线程的上下文中运行,或者对于较低级别的驱动程序在任意线程上下文中运行。
4. 只有满足以下两个条件之一时,才能关闭非内核句柄:先前的处理器模式是KernelMode,或者调用线程有足够的权限来关闭句柄。当调用线程是创建句柄的线程时,会发生后者的示例。
5. NtClose的调用者不应该假设此例程在返回之前自动等待所有I / O完成。