Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:Overview:
GetProcessMemoryInfo (HANDLE, PPROCESS_MEMORY_COUNTERS, DWORD)
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.
Now we'll write the source code help you get memory info.
Below picture is description for using GetProcessMemoryInfo to get Skype program RAM used
Private Working Set is WorkingSetSize attribute
Source code
Using WorkingSetSize div for 1024 to get memory in PC and This code will be show all process and Ram used is active in your PC.// GetMemoryProcessInformation.cpp : Defines the entry point for the console app
/* ***************************************************
Developer: Zidane (huuvi168@gmail.com)
Last Modified: 2015-08-28
Blog: https://learn-tech-tips.blogpsot.com
* **************************************************/
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <psapi.h>
#include <conio.h>
#pragma comment(lib, "psapi.lib") //remember added it
void PrintMemoryInfo( DWORD processID )
{
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 );
}
int _tmain(int argc, _TCHAR* argv[])
{
// Get the list of process identifiers.
DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i;
if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return 1;
// 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] );
_getch();
return 0;
}
Note:
You will see this error when using GetProcessMemoryInfo
Error 1 error LNK2019: unresolved external symbol _GetProcessMemoryInfo@12 referenced in function "void __cdecl
PrintMemoryInfo(unsigned long)" (?PrintMemoryInfo@@YAXK@Z)
Error 2 error LNK2019: unresolved external symbol _EnumProcesses@12 referenced in function _main
Error 3 fatal error LNK1120: 2 unresolved externals
To fix this issue, I added "pragma comment" instruction to places a library-search record in the object file and
compiling is now working fine.
#include <windows.h>
#include <stdio.h>
#include <psapi.h>
#pragma comment(lib, “psapi.lib”) //added
Are you interested in topic Get process memory information with process id by GetProcessMemoryInfo API from Webzone Tech Tips? If you have any thoughts or questions, please share them in the comment section below. I would love to hear from you and chat about it
Webzone Tech Tips Zidane