user2872935
user2872935

Reputation:

GetProcessMemoryInfo Error

I was writing code for finding the virtual memory of current process using psapi.h in C++ My code is as follows

#include "windows.h" 
#include "psapi.h" 

PROCESS_MEMORY_COUNTERS_EX pmc; 
GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc));
SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;

Now here is my problem when I wrote this code in vs2012 ultimate and compiled the compiler was telling me that

no possible conversion from PROCESS_MEMORY_COUNTERS_EX* to PPROCESS_MEMORY_COUNTER

I am not getting what to do. Please help. Thanks for you help in advance.

Upvotes: 0

Views: 991

Answers (1)

Anders
Anders

Reputation: 101646

You need to cast the parameter. This is not uncommon in the Windows API when a struct parameter pointer gets extended.

You also need to check the return value and the returned struct size:

PROCESS_MEMORY_COUNTERS_EX pmc; 
if (GetProcessMemoryInfo(GetCurrentProcess(), (PROCESS_MEMORY_COUNTERS*) &pmc, sizeof(pmc)) && pmc.cb >= sizeof(pmc))
{
  SIZE_T virtualMemUsedByMe = pmc.PrivateUsage;
  // use virtualMemUsedByMe here
}

Upvotes: 3

Related Questions