未公开的函数 NtQuerySystemInformation 下载本文

内容发布更新时间 : 2024/12/29 14:03:20星期一 下面是文章的全部内容请认真阅读。

闲了没事,用SoftIce跟踪 TaskMgr 居然看出点门道来,将这些见解和大家共享,文章可能很长,也可能要花很长的时间,反正爱看就看,不爱看就别看。 幸亏微软提供了相应的 .PDB 和 .DBG文件,使跟踪变的比较容易。本文所有提到的技术都属于微软,我只不过是读懂了然后用Delphi改写,自己没有什么技术,不得用于商业用途,否则老盖找你打官司,可别来找我。

使用未公开的函数 NtQuerySystemInformation 点滴(自己动手写任务管理器)

(如何自己写一个任务管理器,从显示到功能和Windows TaskMgr完全一样,包括从注册表中读取Windows TaskMgr的所有启动参数(170个字节),并增加一些TaskMgr没有的功能) gzgzlxg 2005年4月12日

该文章为近一个月研究Windows 2000和Windows 2003的任务管理器的体会,研究还没有最后结束,但文章必须先写,否则许多东西不写下来就忘了。该文章将分若干篇来写,这是第一段。

NtQuerySystemInformation 是所谓 Undocuments 函数,主要用来获取系统各类信息。Windows 2000的任务管理器 TaskMgr 主要就是使用该函数来获取各类信息,如CPU使用率,内核使用率,句柄总数,线程总数,进程总数...等等在任务管理器中的几乎所有信息都是来自该函数。(当然也可以用别的函数完成类似的工作,如PDH,ToolHelp,或读取注册表等方法,各种方法中,应该以使用NtQuerySystemInformation 最好,这就是 TaskMgr 为什么使用该函数的原因。) 在MSDN知识库中是这样描写该函数的:

[NtQuerySystemInformation is available for use in Windows 2000 and Windows XP. It may be altered or unavailable in subsequent versions. Applications should use the alternate functions listed in this topic.] 但幸运的是,至少在Windows 2003 的任务管理器中仍然是使用该函数来获取系统各类信息的。

下面将具体讲述该函数在Delphi中的使用 1. 函数NtQuerySystemInformation

NtQuerySystemInformation函数隶属Ntdll.dll,函数的调用非常复杂,有许多入口参数,MSDN知识库中基本都是一带而过,没有具体的说明,这里所写的都是自己具体使用的感受,和网站上一些少的可怜的资料,而这些可怜的资料也都是别人自己的体会,所以难免有错误,因此在具体使用中,如有任何问题,概不负责。

1.1函数的调用格式:

function NtQuerySystemInformation(

SystemInformationClass: TSystemInformationClass; { SystemInformationClass [in] One of the values enumerated in SYSTEM_INFORMATION_CLASS, indicating the kind of system information to be retrieved.}

pSystemInformation: PVOID; { SystemInformation

[in, out] Points to a buffer where the requested information is/ to be returned. The size and structure of this information varies depending on the value of the SystemInformationClass parameter:} uSystemInformationLength: ULONG; { SystemInformationLength

[in] Size of the buffer pointed to by the SystemInformation parameter, in bytes.}

puReturnLength: PULONG { ReturnLength

[out, optional] Optional pointer to a location where the function writes the actual size of the information requested. If that size is less than or equal to the SystemInformationLength parameter, the function copies the information into the SystemInformation buffer; otherwise, it returns an NTSTATUS error code and returns in ReturnLength the size of buffer required to receive the requested information. } ): NTSTATUS; stdcall; {Return Values

Returns an NTSTATUS success or error code. The forms and significance of NTSTATUS error codes are listed in the Ntstatus.h header file available in the Windows Device Driver Kit (DDK), and are described in the DDK documentation under Kernel-Mode Driver Architecture / Design Guide / Driver Programming Techniques / Logging Errors.}

{uses a NtQuerySystemInformation call to obtain information about the Cache's settings and NtSetSystemInformation to set new sizing information. The working-set information for a process serves as guidelines for NT's Memory Manager egarding how many pages of/ physical memory should be assigned to the application. Because they are guidelines, conditions can result such that the Memory Manager grows a working-set to a size greater than the maximum, or shrinks it to less than the minimum. However, the settings are factors that will affect the overall allocation, and hence responsiveness, of an application. In the case of CacheSet the application is the file system Cache.}

1.2 参数说明:

NtQuerySystemInformation的调用参数非常多,我这里只列出在TaskMgr中调用的部分。

1.2.1 TSystemInformationClass TSystemInformationClass有许多类,这里列出的是能够找到的,可能还有一些。 PSystemInformationClass = ^TSystemInformationClass; _SYSTEM_INFORMATION_CLASS = (

SystemBasicInformation, SystemProcessorInformation, SystemPerformanceInformation, SystemTimeOfDayInformation, SystemPathInformation, SystemProcessInformation, SystemCallCountInformation,

SystemConfigurationInformation,

SystemProcessorPerformanceInformation, SystemGlobalFlag,

SystemCallTimeInformation, SystemModuleInformation, SystemLockInformation,

SystemStackTraceInformation, SystemPagedPoolInformation, SystemNonPagedPoolInformation, SystemHandleInformation, SystemObjectInformation, SystemPageFileInformation, SystemVdmInstemulInformation, SystemVdmBopInformation, SystemFileCacheInformation, SystemPoolTagInformation, SystemInterruptInformation, SystemDpcBehaviorInformation, SystemFullMemoryInformation, SystemLoadGdiDriverInformation, SystemUnloadGdiDriverInformation, SystemTimeAdjustmentInformation, SystemSummaryMemoryInformation, SystemNextEventIdInformation, SystemEventIdsInformation, SystemCrashDumpInformation, SystemExceptionInformation,

SystemCrashDumpStateInformation, SystemKernelDebuggerInformation, SystemContextSwitchInformation, SystemRegistryQuotaInformation,

SystemExtendServiceTableInformation, SystemPrioritySeperation,

SystemPlugPlayBusInformation, SystemDockInformation, SystemPowerInformation,

SystemProcessorSpeedInformation,