获取对应进程的内存使用情况(GetProcessMemoryInfo)



Retrieves information about the memory usage of the specified process.

Syntax

C++
BOOL WINAPI GetProcessMemoryInfo(
  _In_  HANDLE                   Process,
  _Out_ PPROCESS_MEMORY_COUNTERS ppsmemCounters,
  _In_  DWORD                    cb
);

Parameters

Process [in]

A handle to the process. The handle must have the PROCESS_QUERY_INFORMATION or PROCESS_QUERY_LIMITED_INFORMATION access right and the PROCESS_VM_READ access right. For more information, see Process Security and Access Rights.

Windows Server 2003 and Windows XP:  The handle must have the PROCESS_QUERY_INFORMATION and PROCESS_VM_READ access rights.

ppsmemCounters [out]

A pointer to the PROCESS_MEMORY_COUNTERS or PROCESS_MEMORY_COUNTERS_EX structure that receives information about the memory usage of the process.

cb [in]

The size of the ppsmemCounters structure, in bytes.

Return value

If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

Starting with Windows 7 and Windows Server 2008 R2, Psapi.h establishes version numbers for the PSAPI functions. The PSAPI version number affects the name used to call the function and the library that a program must load.

If PSAPI_VERSION is 2 or greater, this function is defined as K32GetProcessMemoryInfo in Psapi.h and exported in Kernel32.lib and Kernel32.dll. If PSAPI_VERSION is 1, this function is defined as GetProcessMemoryInfo in Psapi.h and exported in Psapi.lib and Psapi.dll as a wrapper that calls K32GetProcessMemoryInfo.

Programs that must run on earlier versions of Windows as well as Windows 7 and later versions should always call this function as GetProcessMemoryInfo. To ensure correct resolution of symbols, add Psapi.lib to the TARGETLIBS macro and compile the program with -DPSAPI_VERSION=1. To use run-time dynamic linking, load Psapi.dll.

Examples

For an example, see Collecting Memory Usage Information for a Process.

Requirements

Minimum supported client

Windows XP [desktop apps only]

Minimum supported server

Windows Server 2003 [desktop apps only]

Header

Psapi.h

Library

Kernel32.lib on Windows 7 and Windows Server 2008 R2; Psapi.lib (if PSAPI_VERSION=1) on Windows 7 and Windows Server 2008 R2; Psapi.lib on Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP

DLL

Kernel32.dll on Windows 7 and Windows Server 2008 R2; Psapi.dll (if PSAPI_VERSION=1) on Windows 7 and Windows Server 2008 R2; Psapi.dll on Windows Server 2008, Windows Vista, Windows Server 2003, and Windows XP

See also

EnumProcesses Memory Performance Information PROCESS_MEMORY_COUNTERS PROCESS_MEMORY_COUNTERS_EX Process Memory Usage Information PSAPI Functions

 

 

Community Additions

ADD

Flag PROCESS_VM_READ is redundant for successful call of GetProcessMemoryInfo

I've tested it under WinXP SP3 x86, Win7 SP1 x86, Win7 SP1 x64, Win8 x86. At all tested platforms GetProcessMemoryInfo was successful with the handle having only PROCESS_QUERY_INFORMATION access right (XP) or PROCESS_QUERY_LIMITED_INFORMATION (others).
This detail is essential because of existing Protected Processes in WinVista+. These processes (System (PID 4) and audiodg.exe for example) can't be OpenProcess'ed with PROCESS_VM_READ access right. But if I open them with PROCESS_QUERY_LIMITED_INFORMATION only, successive calls of GetProcessMemoryInfo give me all info I need.
8/15/2015

need to include <windows.h> before include <Psapi.h>

need to include <windows.h> before include <Psapi.h>
7/18/2013

How to compile under Windows 7 and Visual Studio 2010

To compile this function under Windows 7 and Visual Studio 2010, add the line:


#pragma comment(lib, "psapi.lib") // Added to support GetProcessMemoryInfo()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值