windows上获取内存信息的代码

代码如下:
#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.

hModule [in, optional]

A handle to the module. If this parameter is NULL, this function returns the name of the file used to create the calling process.

lpBaseName [out]

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.

nSize [in]

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值