在Qt中出现"Physical Memory currently used by current process"错误

Getting "Physical Memory currently used by current process" error in Qt

本文关键字:used by current 错误 process currently Physical Qt Memory      更新时间:2023-10-16

我正在尝试在Qt 5.5中获得"当前进程当前使用的物理内存"与本教程:如何获得系统cpu/ram使用在Windows上的c++当我试图将此函数添加到我的应用程序时,我得到一个错误…

PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc)); // error C2664
SIZE_T physMemUsedByMe = pmc.WorkingSetSize;
错误:

C2664: 'BOOL K32GetProcessMemoryInfo(HANDLE,PPROCESS_MEMORY_COUNTERS,DWORD)' : cannot convert argument 2 from 'PROCESS_MEMORY_COUNTERS_EX *' to 'PPROCESS_MEMORY_COUNTERS'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

谢谢你的帮助。

根据文档GetProcessMemoryInfo可以接受指向PROCESS_MEMORY_COUNTERSPROCESS_MEMORY_COUNTERS_EX的指针。最新的类型包含一个额外的字段。

它可能取决于SDK版本,但是在我的头psapi.h中,该函数仅用指向PROCESS_MEMORY_COUNTERS的指针声明。因此,扩展结构版本无法编译。

两种方法都有效:

// use only PROCESS_MEMORY_COUNTERS structure
PROCESS_MEMORY_COUNTERS pmc;
// or cast structure type
PROCESS_MEMORY_COUNTERS_EX pmc;
GetProcessMemoryInfo(GetCurrentProcess(),
    reinterpret_cast<PPROCESS_MEMORY_COUNTERS>(&pmc), sizeof(pmc));

由于GetProcessMemoryInfo也有结构体大小作为参数,扩展的结构体PROCESS_MEMORY_COUNTERS_EX也被填充。

我没有足够的声誉来评论。我从你的问题中得到的信息有限。如果我处在你的位置,我会试着指导你怎么做。

  1. 你是使用Qt与mingw32或visual studio?

你提到的过程将不工作与Qt使用mingw32。这是为visual studio准备的。

  • 如果你正在使用Visual studio尝试遵循没有Qt介入的方法,即为它编写一个独立的程序。如果它仍然给出错误检查MSDN
  • 的函数原型
    相关文章: