c - 在C中获取进程内存信息

标签 c winapi gcc cygwin

我想在 C 中获取进程内存信息。我在 Windows XP 上使用带有 GCC 4.5 的 Cygwin。我包括 #include <psapi.h>并使用 -lpsapi当我构建程序时。 我得到错误

undefined reference to _getprocessmemoryinfo@12

请告诉我在 C 中获取内存进程信息的正确方法。我阅读了 this question ,但这对我的问题没有帮助。

我使用代码。

#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;
}

欢迎回答和建议。

最佳答案

Thisthis建议命令行中 -lpsapi 的顺序很重要。尝试将其放在末尾。

关于c - 在C中获取进程内存信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9578698/

相关文章:

c - 如何用C语言编写shift_elements函数?

c - 如何为您的程序分配更多内存(GCC)

c++ - 边界矩形碰撞测试?

c 变量递增时变为 -17918

c - 如何求解具有指数函数的联立非线性方程组?

linux - 在 linux 中创建共享库时如何包含依赖信息?

c - 将位图字节写入二进制文件时出现奇怪的运行时不一致行为

visual-studio - Windows 8 SDK 中的 DirectShow 示例在哪里?

c++ - 通过注入(inject)的 DLL 绕过成员函数

为窗口的 HBRUSH 创建位图图案