15

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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Aslan986
  • 9,984
  • 11
  • 44
  • 75

3 Answers3

23

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 ));
sharptooth
  • 167,383
  • 100
  • 513
  • 979
  • 4
    A good reason why you should always use the name of the variable as the argument to the `sizeof` operator, rather than the type name. – Cody Gray - on strike Dec 27 '11 at 13:54
  • 1
    @CodyGray: that's not true. You just need to understand what you put into your `sizeof` operator. Putting the pointer address or the `PPROCESS_MEMORY_COUNTERS` type will return the pointer size, which is 4 bytes on a x86 system. Putting the object pointed at this address or the `PROCESS_MEMORY_COUNTERS` type will return the actual object size. Neither is *always* the good solution. – Benoit Duffez Jun 13 '12 at 12:42
  • I get the performance warning 4800 `forcing value to bool`, do you know how to fix it? which variable could be a BOOL? – quimnuss Jan 25 '17 at 15:45
  • 1
    @quimnuss Function returns `BOOL` which is an integer, that's what the compier is trying to say. I fixed the code. – sharptooth Jan 26 '17 at 15:27
  • Wow, bravo Microsoft. So `int` would also.be correct (so long as they don't re-typedef it), and error is detected with `if(result == 0) cerr<<"error"< – quimnuss Jan 26 '17 at 15:33
  • 1
    @quimnuss It's not "bravo Microsoft", it's the result of designing API which can be called from C (no bool in C). It's better to use `BOOL` and compare it against zero instead of `int`. – sharptooth Jan 27 '17 at 09:06
  • @sharptooth: “no bool in C” – [C has had a boolean type since 1999.](https://en.cppreference.com/w/c/types) – phg Mar 13 '23 at 08:57
4

change sizeof(PPROCESS_MEMORY_COUNTERS) to sizeof(PROCESS_MEMORY_COUNTERS)

marcinj
  • 48,511
  • 9
  • 79
  • 100
3

On MSDN:

BOOL WINAPI GetProcessMemoryInfo( In HANDLE Process, Out PPROCESS_MEMORY_COUNTERS ppsmemCounters, In DWORD cb );

Example:

HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;

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;

if (GetProcessMemoryInfo( ( hProcess, &pmc, sizeof(pmc)))
{
    printf( "\tWorkingSetSize: 0x%08X - %u\n",  pmc.WorkingSetSize,  
                                              pmc.WorkingSetSize / 1024);        
    printf( "\tQuotaPeakPagedPoolUsage: 0x%08X - %u\n", 
         pmc.QuotaPeakPagedPoolUsage ,   pmc.QuotaPeakPagedPoolUsage / 1024);
    printf( "\tQuotaPagedPoolUsage: 0x%08X - %u\n", pmc.QuotaPagedPoolUsage, 
                                              pmc.QuotaPagedPoolUsage / 1024);
    printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X - %u\n", 
               pmc.QuotaPeakNonPagedPoolUsage,pmc.QuotaPeakNonPagedPoolUsage / 1024 );
    printf( "\tQuotaNonPagedPoolUsage:0x%08X-%u\n",pmc.QuotaNonPagedPoolUsage ,   pmc.QuotaNonPagedPoolUsage / 1024);
    printf( "\tPagefileUsage: 0x%08X - %u\n", pmc.PagefileUsage,     pmc.PagefileUsage/1024 ); 
    printf( "\tPeakPagefileUsage: 0x%08X - %u\n", pmc.PeakPagefileUsage, pmc.PeakPagefileUsage/1024 );
    printf( "\tcb: 0x%08X - %u\n", pmc.cb , pmc.cb / 1024);     
}
CloseHandle(hProcess);

Or You can view full code from here

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Zidane
  • 39
  • 3