<< Hide left pane
Leave feedback
Paging Files Sample
This sample demonstrates how to obtain information about all
paging files in the system using the undocumented
ZwQuerySystemInformation function. This function, which
prototype is shown below, provides access to a wide variety of
system information.
NTSTATUS ZwQuerySystemInformation(
IN ULONG SystemInformationClass,
IN OUT PVOID SystemInformation,
IN ULONG SystemInformationLength,
OUT PULONG ReturnLength OPTIONAL
);
The first parameter, SystemInformationClass, specifies
the type of information to retrieve. To obtain information about
paging files, this parameter should be set to 18.
The second parameter, SystemInformation, should point
to a buffer, into which the system will store requested information.
For paging files, the information is returned as an array of
SYSTEM_PAGEFILE_INFORMATION structures, one structure for
each paging file in the system.
typedef struct _SYSTEM_PAGEFILE_INFORMATION {
ULONG NextEntryOffset;
ULONG CurrentSize;
ULONG TotalUsed;
ULONG PeakUsed;
UNICODE_STRING FileName;
} SYSTEM_PAGEFILE_INFORMATION, * PSYSTEM_PAGEFILE_INFORMATION;
All sizes in this structure are expressed in pages. To determine
corresponding sizes in bytes, these values should be multiplied on
the processor's page size, which can be obtained with GetSystemInfo
function.
Below is a part of the sample program source code, which obtains and
displays information about all paging files in the system.
int _tmain(
int argc,
_TCHAR * argv[]
)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
ULONG cbBuffer = 0x1000;
LPVOID pBuffer = NULL;
NTSTATUS Status;
do
{
pBuffer = malloc(cbBuffer);
if (pBuffer == NULL)
{
_tprintf(_T("Not enough memory\n"));
return 1;
}
Status = ZwQuerySystemInformation(
SystemPagefileInformation,
pBuffer, cbBuffer, NULL);
if (Status == STATUS_INFO_LENGTH_MISMATCH)
{
free(pBuffer);
cbBuffer *= 2;
}
else if (!NT_SUCCESS(Status))
{
_tprintf(_T("ZwQuerySystemInformation failed with")
_T("status 0x%08X\n"), Status);
free(pBuffer);
return 1;
}
}
while (Status == STATUS_INFO_LENGTH_MISMATCH);
PSYSTEM_PAGEFILE_INFORMATION pInfo =
(PSYSTEM_PAGEFILE_INFORMATION)pBuffer;
_tprintf(_T(" Current Size Total Used Peak Used File Name\n"));
_tprintf(_T("--------------------------------------------------\n"));
for (;;)
{
_tprintf(_T(" %8d K %8d K %8d K %ls\n"),
pInfo->CurrentSize * si.dwPageSize / 1024,
pInfo->TotalUsed * si.dwPageSize / 1024,
pInfo->PeakUsed * si.dwPageSize / 1024,
pInfo->FileName.Buffer);
if (pInfo->NextEntryOffset == 0)
break;
pInfo = (PSYSTEM_PAGEFILE_INFORMATION)((PBYTE)pInfo +
pInfo->NextEntryOffset);
}
free(pBuffer);
return 0;
}
Note: the same information can be obtained using Windows
Management Instrumentation (WMI), which is fully documented in the
Platform SDK. WMI exposes paging files information through
Win32_PageFileUsage and Win32_PageFileSettings classes.
It is strongly recommended to use WMI to obtain system information
rather than undocumented functions.
References
- Gary Nebbett, Windows NT/2000 Native API Reference.
New Riders Publishing, 2000.