Comment utiliser GetProcessMemoryInfo en C++?

j'essaie d'utiliser la fonction GetProcessMemoryInfopsapi.h à l'intérieur d'une application C++ sur Windows 7 32-bit.

j'ai suivi un tutoriel et j'ai fait quelque chose comme:

PPROCESS_MEMORY_COUNTERS pMemCountr;

pMemCountr = new PROCESS_MEMORY_COUNTERS();
bool result = GetProcessMemoryInfo(GetCurrentProcess(),
                                   pMemCountr,
                                   sizeof(PPROCESS_MEMORY_COUNTERS));

le problème est que j'obtiens toujours "false" de l'exécution du GetProcessMemoryInfo() méthode. Ce que je fais mal?

10
demandé sur Cody Gray 2011-12-27 15:25:28

3 réponses

Le problème est

sizeof(PPROCESS_MEMORY_COUNTERS)

donne la taille de PPROCESS_MEMORY_COUNTERS est un PROCESS_MEMORY_COUNTERS* tapez pointeur (notez double P au début).

Ce que vous voulez, c'est

sizeof(PROCESS_MEMORY_COUNTERS)

aussi vous serez beaucoup mieux sans new ici:

PROCESS_MEMORY_COUNTERS memCounter;
BOOL result = GetProcessMemoryInfo(GetCurrentProcess(),
                                   &memCounter,
                                   sizeof( memCounter ));
19
répondu sharptooth 2017-01-26 15:26:55

changement de taille (PPROCESS_MEMORY_COUNTERS) en taille (PROCESS_MEMORY_COUNTERS)

4
répondu marcinj 2011-12-27 11:39:19

on MSDN:

BOOL WINAPI GetProcessMemoryInfo( processus de manipulation, PPROCESS_MEMORY_COUNTERS ppsmemCounters, DWORD cb );

Exemple:

HANDLE hProcess;
PROCESS_MEMORY_COUNTERS pmc;

printf( "\nProcess ID: %u\n", processID );

// Print information about the memory usage of the process.
hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );
if (NULL == hProcess)
    return;

if (GetProcessMemoryInfo( ( hProcess, &pmc, sizeof(pmc)))
{
    printf( "\tWorkingSetSize: 0x%08X - %u\n",  pmc.WorkingSetSize,  
                                              pmc.WorkingSetSize / 1024);        
    printf( "\tQuotaPeakPagedPoolUsage: 0x%08X - %u\n", 
         pmc.QuotaPeakPagedPoolUsage ,   pmc.QuotaPeakPagedPoolUsage / 1024);
    printf( "\tQuotaPagedPoolUsage: 0x%08X - %u\n", pmc.QuotaPagedPoolUsage, 
                                              pmc.QuotaPagedPoolUsage / 1024);
    printf( "\tQuotaPeakNonPagedPoolUsage: 0x%08X - %u\n", 
               pmc.QuotaPeakNonPagedPoolUsage,pmc.QuotaPeakNonPagedPoolUsage / 1024 );
    printf( "\tQuotaNonPagedPoolUsage:0x%08X-%u\n",pmc.QuotaNonPagedPoolUsage ,   pmc.QuotaNonPagedPoolUsage / 1024);
    printf( "\tPagefileUsage: 0x%08X - %u\n", pmc.PagefileUsage,     pmc.PagefileUsage/1024 ); 
    printf( "\tPeakPagefileUsage: 0x%08X - %u\n", pmc.PeakPagefileUsage, pmc.PeakPagefileUsage/1024 );
    printf( "\tcb: 0x%08X - %u\n", pmc.cb , pmc.cb / 1024);     
}
CloseHandle(hProcess);

Ou Vous pouvez afficher l'intégralité du code de ici

2
répondu Zidane 2015-09-22 10:03:30