Background
Break News
How to add local font to Tailwind Css and NextJS? - Tutorial Design Pattern? - Blockchain Technology, How to create own Bitcoin virtual currency - Zustand mordern management state - Design Pattern - Flyweight Pattern? - Docker Full training Topic

[Tips] Get process memory information with process id by GetProcessMemoryInfo API

Monday 31 August 2015
|
Read: Completed in minutes

[Tips] Get process memory information with process id by GetProcessMemoryInfo API

Do You want to get memory of process with process id? or you can consider do it with GetProcessMemoryInfo API.







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


Get process memory information with process id by GetProcessMemoryInfo API





Get process memory information with process id by GetProcessMemoryInfo API

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


🙇🏼 Your Feedback Is Valuable and Helpful to Us - Webzone - all things Tech Tips web development Zidane 🙇🏼
Popular Webzone Tech Tips topic maybe you will be like it - by Webzone Tech Tips - Zidane
As a student, I found Blogspot very useful when I joined in 2014. I have been a developer for years . To give back and share what I learned, I started Webzone, a blog with tech tips. You can also search for tech tips zidane on Google and find my helpful posts. Love you all,

I am glad you visited my blog. I hope you find it useful for learning tech tips and webzone tricks. If you have any technical issues, feel free to browse my posts and see if they can help you solve them. You can also leave a comment or contact me if you need more assistance. Here is my blog address: https://learn-tech-tips.blogspot.com.

My blog where I share my passion for web development, webzone design, and tech tips. You will find tutorials on how to build websites from scratch, using hot trends frameworks like nestjs, nextjs, cakephp, devops, docker, and more. You will also learn how to fix common bugs on development, like a mini stackoverflow. Plus, you will discover how to easily learn programming languages such as PHP (CAKEPHP, LARAVEL), C#, C++, Web(HTML, CSS, javascript), and other useful things like Office (Excel, Photoshop). I hope you enjoy my blog and find it helpful for your projects. :)

Thanks and Best Regards!
Follow me on Tiktok @learntechtips and send me a direct message. I will be happy to chat with you.
Webzone - Zidane (huuvi168@gmail.com)
I'm developer, I like code, I like to learn new technology and want to be friend with people for learn each other
I'm a developer who loves coding, learning new technologies, and making friends with people who share the same passion. I have been a full stack developer since 2015, with more than years of experience in web development.
Copyright @2022(November) Version 1.0.0 - By Webzone, all things Tech Tips for Web Development Zidane
https://learn-tech-tips.blogspot.com