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?
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 ));
change sizeof(PPROCESS_MEMORY_COUNTERS) to sizeof(PROCESS_MEMORY_COUNTERS)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With