您的位置:首页 > 编程语言

一个windows下取进程内存的代码

2013-01-21 20:07 162 查看
先保存一下,以免以后用到又要重写

1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <time.h>
5 #include "psapi.h"
6
7 DWORD GetMemory(int interval, int pid)
8 {
9 HANDLE hProcess = NULL;
PROCESS_MEMORY_COUNTERS pmc;
PERFORMACE_INFORMATION pmi;

hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid);
if (NULL == hProcess)
{
fprintf(stderr, "pid: %d not exist.\n", pid);
return -1;
}

setvbuf(stdout, NULL, _IONBF, 0);
while(1)
{
time_t t = time(NULL);
struct tm* nowtime = localtime(&t);
#if 1
if ( GetProcessMemoryInfo( hProcess, &pmc, (BYTE)sizeof(pmc)) )
#else
if (GetPerformanceInfo( hProcess, &pmi, (BYTE)sizeof(pmi)));
#endif
{
printf("%02d:%02d:%02d,%d\n", nowtime->tm_hour, nowtime->tm_min, nowtime->tm_sec,pmc.WorkingSetSize>>10);
}
Sleep(interval*1000);
}

CloseHandle( hProcess );
return 0;
}

int main(int argc, char* argv[])
{
int interval, pid;
if(argc != 3)
{
fprintf(stderr, "useage: interval pid\n", argv[0]);
exit(-1);
}
interval = atoi(argv[1]);
pid = atoi(argv[2]);

return GetMemory(interval, pid);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: