我正在尝试在Windows 7 32位的C ++应用程序中使用GetProcessMemoryInfo
的函数psapi.h
。
我遵循了一些教程,我做了类似的事情:
PPROCESS_MEMORY_COUNTERS pMemCountr;
pMemCountr = new PROCESS_MEMORY_COUNTERS();
bool result = GetProcessMemoryInfo(GetCurrentProcess(),
pMemCountr,
sizeof(PPROCESS_MEMORY_COUNTERS));
问题是我总是从执行GetProcessMemoryInfo()
方法获得“假”。我在这里做错了什么?
答案 0 :(得分:21)
问题是
sizeof(PPROCESS_MEMORY_COUNTERS)
产生PPROCESS_MEMORY_COUNTERS
的大小,这是PROCESS_MEMORY_COUNTERS*
类型指针(在开头注意加倍P
)。
你想要的是
sizeof(PROCESS_MEMORY_COUNTERS)
如果没有new
,你也会好多了:
PROCESS_MEMORY_COUNTERS memCounter;
BOOL result = GetProcessMemoryInfo(GetCurrentProcess(),
&memCounter,
sizeof( memCounter ));
答案 1 :(得分:4)
将sizeof(PPROCESS_MEMORY_COUNTERS)更改为sizeof(PROCESS_MEMORY_COUNTERS)
答案 2 :(得分:3)
在MSDN上:
BOOL WINAPI GetProcessMemoryInfo( 在 HANDLE过程中, Out PPROCESS_MEMORY_COUNTERS ppsmemCounters, In DWORD cb );
示例:
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);
或者您可以查看here
中的完整代码