【发布时间】:2012-03-23 15:16:08
【问题描述】:
我想在 C 中获取进程内存信息。我在 Windows XP 上使用 Cygwin 和 GCC 4.5。我在构建程序时包含#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;
}
欢迎提供答案和建议。
【问题讨论】:
-
有一次我遇到了这个问题,因为我使用 -l 比使用 main 函数的 .c 文件的名称早。
-
@stupid_idiot 可能是对的。请显示您用于构建程序的命令行。
-
@Alex thnkx 亲爱的我得到了答案。
-
@Alex:也许你应该发表你的评论作为答案,这样 Code Breaker 就可以接受它并且它不会出现在未回答的问题列表中......