winapi GetProcessMemoryInfo 参数无效问题
Posted
技术标签:
【中文标题】winapi GetProcessMemoryInfo 参数无效问题【英文标题】:winapi GetProcessMemoryInfo invalid arguments problem 【发布时间】:2014-12-22 14:24:39 【问题描述】:我有这个问题,无法通过这个测试代码得到解决。
无效的参数候选者是:
? 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 我尝试添加路径和包含,将 -lpaspi 添加到 eclipse 中的 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:
【问题讨论】:
确切的错误是什么? nvalid arguments ' 候选人是: ? GetProcessMemoryInfo(?, _PROCESS_MEMORY_COUNTERS *, ?) 这是编译时错误还是运行时错误? 它是eclipse keplar中的编译时错误 【参考方案1】:您正在传递一个未初始化的指针 (file_version)。它应该指向一个缓冲区。有关如何确定所需缓冲区大小的信息,请参阅 MSDN 文档。
【讨论】:
您说得对,但这暂时不会出错。文件说:如果PSAPI_VERSION为2或更大,这个函数在Psapi.h中定义为K32GetProcessMemoryInfo msdn.microsoft.com/de-de/library/windows/desktop/…以上是关于winapi GetProcessMemoryInfo 参数无效问题的主要内容,如果未能解决你的问题,请参考以下文章