km2442
km2442

Reputation: 827

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

I'm trying in Qt 5.5 to get "Physical Memory currently used by current process" with this tutorial: How to get system cpu/ram usage in c++ on Windows When I'm trying to add this function to my application I'm getting an error...

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

Error:

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

Thank you for any help.

Upvotes: 1

Views: 2128

Answers (2)

Orest Hera
Orest Hera

Reputation: 6776

According to the documentation GetProcessMemoryInfo can accept either pointer to PROCESS_MEMORY_COUNTERS or to PROCESS_MEMORY_COUNTERS_EX. The latest type contains one additional field.

It might depend on SDK version, however in my header psapi.h this function is declared only with pointer to PROCESS_MEMORY_COUNTERS. So, the extended structure version fails to compile.

Both solutions work:

// 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));

Sinse GetProcessMemoryInfo has also structure size as an argument, the extended structure PROCESS_MEMORY_COUNTERS_EX is also filled.

Upvotes: 4

Dr. Xperience
Dr. Xperience

Reputation: 495

I don't have enough reputation to comment. With the limited amount information I have from your question. I will try to guide you what I would have done in your shoes.

  1. Are you using Qt with mingw32 or visual studio?.

The process you mentioned will not work with Qt using mingw32. It's meant for visual studio.

  1. If you are using Visual studio try to follow the method without Qt internvention i.e. write a stand alone program for it. And if it still gives error check the function prototypes at MSDN

Upvotes: 0

Related Questions