2

I want to get the process memory Info in C. I am using Cygwin with GCC 4.5 on Windows XP. I include #include <psapi.h> and use -lpsapi when I build the program. I get the error

undefined reference to _getprocessmemoryinfo@12

Please tell the correct way to get the Process Info of Memory in C. I read the this question, but it is not helping with my problem.

I use the code.

#include <windows.h>
#include <stdio.h>
#include <psapi.h>

void PrintMemoryInfo( DWORD processID )
{
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;

    // Print the process identifier.

    printf( "\nProcess ID: %u\n", processID );
    getchar();

    // 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( "\tPageFaultCount: 0x%08X\n", pmc.PageFaultCount );
        printf( "\tYour app's PEAK MEMORY CONSUMPTION: 0x%08X\n", 
                  pmc.PeakWorkingSetSize );
        printf( "\tYour app's CURRENT MEMORY CONSUMPTION: 0x%08X\n", pmc.WorkingSetSize );
        printf( "\tQuotaPeakPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakPagedPoolUsage );
        printf( "\tQuotaPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPagedPoolUsage );
        printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaPeakNonPagedPoolUsage );
        printf( "\tQuotaNonPagedPoolUsage: 0x%08X\n", 
                  pmc.QuotaNonPagedPoolUsage );
        printf( "\tPagefileUsage: 0x%08X\n", pmc.PagefileUsage ); 
        printf( "\tPeakPagefileUsage: 0x%08X\n", 
                  pmc.PeakPagefileUsage );
    }

    CloseHandle( hProcess );
}

int main( )
{
  PrintMemoryInfo( GetCurrentProcessId() );

    return 0;
}

Answers and suggestions are welcome.

Community
  • 1
  • 1
Code Breaker
  • 499
  • 1
  • 4
  • 19
  • 1
    One time i had this problem caused because I used -l sooner than the name of the .c file with main function. – Pyjong Mar 06 '12 at 07:05
  • 2
    @stupid_idiot is probably right. Please show the command line you use to build the program. – Some programmer dude Mar 06 '12 at 07:09
  • 3
    [This](http://justinsboringpage.blogspot.com/2009_05_14_archive.html) and [this](http://old.nabble.com/Build-problems-on-MinGW-td21445405.html) suggest that the order of `-lpsapi` in the command line is important. Try placing it at the end. – Alexey Frunze Mar 06 '12 at 07:50
  • @Alex thnkx dear I got my answer. – Code Breaker Mar 06 '12 at 11:08
  • 2
    @Alex: Maybe you should post your comment as an answer, so Code Breaker can accept it and it won't show up on the list of unanswered questions... – tomlogic Mar 28 '12 at 16:22
  • Any specific reason tu use NULL before? if (NULL == hProcess) – Cyro Nov 19 '20 at 04:56

1 Answers1

1

This and this suggest that the order of -lpsapi in the command line is important. Try placing it at the end.

Alexey Frunze
  • 61,140
  • 12
  • 83
  • 180