Aslan986 Aslan986 - 1 month ago 20
C++ Question

How to use GetProcessMemoryInfo in C++?

I'm trying to use the function

GetProcessMemoryInfo
of
psapi.h
inside a C++ application on Windows 7 32-bit.

I followed some tutorial and I did something like:

PPROCESS_MEMORY_COUNTERS pMemCountr;

pMemCountr = new PROCESS_MEMORY_COUNTERS();
bool result = GetProcessMemoryInfo(GetCurrentProcess(),
pMemCountr,
sizeof(PPROCESS_MEMORY_COUNTERS));


The problem is that i always obtain "false" from the execution of the
GetProcessMemoryInfo()
method. What am I doing wrong here?

Answer

The problem is

sizeof(PPROCESS_MEMORY_COUNTERS)

yields the size of PPROCESS_MEMORY_COUNTERS which is a PROCESS_MEMORY_COUNTERS* type pointer (note double P in the beginning).

What you want is

sizeof(PROCESS_MEMORY_COUNTERS)

Also you'll be much better off without new here:

PROCESS_MEMORY_COUNTERS memCounter;
bool result = GetProcessMemoryInfo(GetCurrentProcess(),
                                   &memCounter,
                                   sizeof( memCounter ));
Comments