我有这个问题,无法解决这个测试代码。
无效参数候选人是:
? GetProcessMemoryInfo(?,_PROCESS_MEMORY_COUNTERS *,?)
How to determine CPU and memory consumption from inside a process?
我尝试了GetProcessMemoryInfo(GetCurrentProcess(),& info,info.cb); 和GetProcessMemoryInfo(GetCurrentProcess(),(* PROCESS_MEMORY_COUNTERS)& info,info.cb); 我使用mingw64版本MinGW-W64-builds-4.2.0 gcc版本4.9.2: - std = c ++ 1y -O0 -g3 -Wall -c -fmessage-length = 0 我尝试添加路径和包含,在eclipse中添加了-lpaspi到gcc ++构建参数,但似乎没有任何帮助。 任何想法?
#include <windows.h>
#include <psapi.h>
LPVOID file_version;
HANDLE handle;
PROCESS_MEMORY_COUNTERS_EX info;
MEMORYSTATUSEX memoryInfo;
DWORDLONG totalVirtualMemory;
DWORDLONG virtualMemoryUsed;
SIZE_T virtualMemoryUsedByMe;
DWORDLONG totalPhysicalMemory;
DWORDLONG physicalMemoryUsed;
SIZE_T physicalMemoryUsedByMe;
void init(){
bool error = GetFileVersionInfo("psapi.lib",0,GetFileVersionInfoSize("psapi.h",0),file_version);
info.cb = sizeof(info);
bool okay = GetProcessMemoryInfo(GetCurrentProcess(),(*PROCESS_MEMORY_COUNTERS)&info,info.cb);
memoryInfo.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memoryInfo);
totalVirtualMemory = memoryInfo.ullTotalPageFile;//Total Virtual Memory:
virtualMemoryUsed = memoryInfo.ullTotalPageFile - memoryInfo.ullAvailPageFile;//Virtual Memory currently used:
}
double GetCurrentValue(){
HANDLE handle = GetCurrentProcess();
info.cb = sizeof(info);
GetProcessMemoryInfo(handle, (PROCESS_MEMORY_COUNTERS*)&info,info.cb);
virtualMemoryUsedByMe = info.PrivateUsage; //Virtual Memory currently used by current process:
totalPhysicalMemory = memoryInfo.ullTotalPhys;//Total Physical Memory (RAM):
physicalMemoryUsed = memoryInfo.ullTotalPhys - memoryInfo.ullAvailPhys;//Physical Memory currently used:
physicalMemoryUsedByMe = info.WorkingSetSize;//Physical Memory currently used by current process:
}
答案 0 :(得分:1)
您正在传递未初始化的指针(file_version)。它应该指向一个缓冲区。有关如何确定所需缓冲区大小的信息,请参阅MSDN文档。