当大家打开Windows任务管理器时就会看到每个进程使用内存分布情况往往会发现有些进程占用大量内存在这种情况也是种异常情况可以作为是否恶意软件Software标志的下面就来使用APIGetProcessMemoryInfo来获取内存使用情况
GetProcessMemoryInfo声明如下:
BOOL
WINAPI
GetProcessMemoryInfo(
HANDLE Process,
PPROCESS_MEMORY_COUNTERS ppsmemCounters,
DWORD cb
);
Process是获取内存使用情况进程句柄
ppsmemCounters是返回内存使用情况结构
cb是结构大小
例子如下:
#001 //获取某个进程内存信息
#002 //蔡军生 2007/12/18 qq:9073204 深圳
#003 void TestGetProcessMemoryInfo(void)
#004 {
#005 //
#006 const nBufSize = 512;
#007 TCHAR chBuf[nBufSize];
#008 ZeroMemory(chBuf,nBufSize);
#009
#010 //
#011 DWORD dwProcs[1024];
#012 DWORD dwNeeded;
#013
#014 //枚举所有进程ID
#015 ( !EnumProcesses( dwProcs, (dwProcs), &dwNeeded ) )
#016 {
#017 //输出出错信息
#018 wsprf(chBuf,_T("EnumProcesses failed (%d).\n"), GetLastError );
#019 OutputDebugString(chBuf);
#020
#021 ;
#022 }
#023
#024 // 计算有多少个进程ID
#025 DWORD dwProcCount = dwNeeded / (DWORD);
#026
#027 wsprf(chBuf,_T("EnumProcesses Count(%d).\n"), dwProcCount );
#028 OutputDebugString(chBuf);
#029
#030 //遍历所有进程ID打开进程
#031 for (DWORD i = 0; i < dwProcCount; i)
#032 {
#033 wsprf(chBuf,_T("EnumProcesses (%d).\r\n"), dwProcs[i] );
#034 OutputDebugString(chBuf);
#035
#036 //根据进程ID打开进程
#037 HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
#038 PROCESS_VM_READ,
#039 FALSE, dwProcs[i] );
#040
#041 (hProcess)
#042 {
#043 //
#044 PROCESS_MEMORY_COUNTERS pmc;
#045 pmc.cb = (PROCESS_MEMORY_COUNTERS);
#046
#047 //获取这个进程内存使用情况
#048 ( ::GetProcessMemoryInfo( hProcess, &pmc, (pmc)) )
#049 {
#050 ZeroMemory(chBuf,nBufSize);
#051
#052 wsprf(chBuf,_T("\t缺页中断次数: 0x%08X\n"), pmc.PageFaultCount );
#053 OutputDebugString(chBuf);
#054
#055 wsprf(chBuf,_T("\t使用内存高峰: 0x%08X\n"),
#056 pmc.PeakWorkingSetSize );
#057 OutputDebugString(chBuf);
#058
#059 wsprf(chBuf,_T("\t当前使用内存: 0x%08X\n"), pmc.WorkingSetSize );
#060 OutputDebugString(chBuf);
#061
#062 wsprf(chBuf,_T("\t使用页面缓存Cache池高峰: 0x%08X\n"),
#063 pmc.QuotaPeakPagedPoolUsage );
#064 OutputDebugString(chBuf);
#065
#066 wsprf(chBuf,_T("\t使用页面缓存Cache池: 0x%08X\n"),
#067 pmc.QuotaPagedPoolUsage );
#068 OutputDebugString(chBuf);
#069
#070 wsprf(chBuf,_T("\t使用非分页缓存Cache池高峰: 0x%08X\n"),
#071 pmc.QuotaPeakNonPagedPoolUsage );
#072 OutputDebugString(chBuf);
#073
#074 wsprf(chBuf,_T("\t使用非分页缓存Cache池: 0x%08X\n"),
#075 pmc.QuotaNonPagedPoolUsage );
#076 OutputDebugString(chBuf);
#077
#078 wsprf(chBuf,_T("\t使用分页文件: 0x%08X\n"), pmc.PagefileUsage );
#079 OutputDebugString(chBuf);
#080
#081 wsprf(chBuf,_T("\t使用分页文件高峰: 0x%08X\n"),
#082 pmc.PeakPagefileUsage );
#083 OutputDebugString(chBuf);
#084 }
#085
#086 //
#087 CloseHandle(hProcess);
#088 }
#089 }
#090
#091 }
最新评论