代码如下:
#include <windows.h>
#include <stdio.h>
#include "psapi.h"
#pragma comment(lib, "Psapi")
void PrintMemoryInfo( DWORD processID )
{
HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;
// Print the process identifier.
printf( "\nProcess ID: %u\n", processID );
// Print information about the memory usage of the process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID );
if (NULL == hProcess)
return;
//get process name
const int nBufSize = 512;
TCHAR chBuf[nBufSize];
HMODULE hMod;
DWORD cbNeeded;
if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
ZeroMemory(chBuf,nBufSize);
if (GetModuleBaseName(hProcess,hMod,chBuf,nBufSize))
{
printf("\t进程名字:%S\n",chBuf);
}
}
if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
{
printf( "\t缺页中断次数: %d\n", pmc.PageFaultCount ); //缺页中断次数
printf( "\t使用内存高峰: 0x%08X\n",
pmc.PeakWorkingSetSize ); //使用内存高峰
printf( "\t当前使用内存: 0x%08X\n", pmc.WorkingSetSize ); // 当前使用内存
printf( "\t使用页面缓存池高峰: 0x%08X\n",
pmc.QuotaPeakPagedPoolUsage ); //使用页面缓存池高峰
printf( "\t使用页面缓存池: 0x%08X\n",
pmc.QuotaPagedPoolUsage ); //使用页面缓存池
printf( "\t使用非页面缓存池高峰: 0x%08X\n",
pmc.QuotaPeakNonPagedPoolUsage ); //使用非页面缓存池高峰
printf( "\t使用非页面缓存池: 0x%08X\n",
pmc.QuotaNonPagedPoolUsage ); //使用非页面缓存池
printf( "\t使用分页文件: 0x%08X\n", pmc.PagefileUsage ); //使用分页文件
printf( "\t使用分页文件高峰: 0x%08X\n",
pmc.PeakPagefileUsage ); //使用分页文件高峰
}
CloseHandle( hProcess );
}
void main( )
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return;
// Calculate how many process identifiers were returned.
cProcesses = cbNeeded / sizeof(DWORD);
// Print the memory usage for each process
for ( i = 0; i < cProcesses; i++ )
PrintMemoryInfo( aProcesses[i] );
int tem;
scanf("%d",&tem);
return;
}
1. EnumProcesses (
DWORD * lpidProcess,
DWORD cb,
DWORD * cbNeeded
);
获取所有进程的ID
lpidProcess是保存进程ID的数组。cb是进程组数的大小。
cbNeeded是返回进程数组的大小。
2. OpenProcess( __in DWORD dwDesiredAccess,
__in BOOL bInheritHandle,
__in DWORD dwProcessId);
打开进程,获取进程句柄 hProcess
dwDesiredAccess是访问进程的权限。
bInheritHandle是句柄是否继承进程属性。
dwProcessId是进程ID。
3.
BOOL WINAPI EnumProcessModules( __in HANDLE hProcess, __out HMODULE *lphModule, __in DWORD cb, __out LPDWORD lpcbNeeded );
枚举进程模块信息
hprocess 进程句柄
lphmodule 模块信息
cb模块信息的大小,sizeof(HMODULE)
lpcbNeeded: The number of bytes required to store all module handles in the lphModule array
4.
DWORD WINAPI GetModuleBaseName( __in HANDLE hProcess, __in_opt HMODULE hModule, __out LPTSTR lpBaseName, __in DWORD nSize );
hProcess [in]
A handle to the process that contains the module.
A handle to the module. If this parameter is NULL, this function returns the name of the file used to create the calling process.
A pointer to the buffer that receives the base name of the module. If the base name is longer than maximum number of characters specified by the nSize parameter, the base name is truncated.
The size of the lpBaseName buffer, in characters.
5.BOOL
WINAPI
GetProcessMemoryInfo(
HANDLE Process,
PPROCESS_MEMORY_COUNTERS ppsmemCounters,
DWORD cb
);
Process是获取内存使用情况的进程句柄。
ppsmemCounters是返回内存使用情况的结构。
cb是结构的大小。
参考:http://blog.csdn.net/li_guotao/article/details/3741507
http://hi.baidu.com/hypkb/blog/item/40a335fb4c08279059ee90e9.html
http://archlord.blog.hexun.com/29111085_d.html