一个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);
}
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);
}
相关文章推荐
- Windows 底下根据一个进程的名字杀死一个进程的代码
- windows编程点滴(三)之Windows一个进程读取另一个进程的内存
- 用VC写Assembly代码(6)--附录2[windows进程中的内存结构]
- 一个进程在内存中的布局
- 全面介绍Windows内存管理机制及C++内存分配实例(一):进程空间
- 一个问题阻止Windows正在检查此机器的许可证。错误代码:0x80070002
- windows进程通信-内存共享和信号量
- 一个由进程内存布局异常引起的问题
- windows 获取系统CPU和进程CPU 内存等信息
- 一个问题阻止windows正确检查此机器的许可证 错误代码:0x80070002
- Windows进程通信 -- 共享内存
- windows进程中的内存结构
- 一个问题阻止windows正确检查此机器的许可证。错误代码0x80070002
- 如何访问一个进程的内存空间
- Windows自带的一个关闭进程的命令
- 重头再来——创建一个最基本的Windows窗口的代码
- Windows Live Writer的一个代码语法高亮的插件——CodeInLiveWriter
- Windows进程中的内存结构
- 基于visual c++之windows核心编程代码分析(63)无模块dll进程注射