2
我想在C中獲取進程內存信息。我在Windows XP上使用Cygwin和GCC 4.5。我包含#include <psapi.h>
並在構建程序時使用-lpsapi
。 我得到的錯誤獲取進程中的內存信息C
undefined reference to [email protected]
請告訴正確的方式來獲得內存的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早於具有主函數的.c文件的名稱。 – Pyjong 2012-03-06 07:05:54
@stupid_idiot可能是對的。請顯示您用來構建程序的命令行。 – 2012-03-06 07:09:49
[This](http://justinsboringpage.blogspot.com/2009_05_14_archive.html)和[this](http://old.nabble.com/Build-problems-on-MinGW-td21445405.html)建議訂單命令行中的'-lpsapi'很重要。嘗試把它放在最後。 – 2012-03-06 07:50:39