简体   繁体   English

获取过程内存信息

[英]Get process memory information

I've been trying to get process memory information in windows, and I've been following Microsoft docs and I've got this code but it doesn't seem to work. 我一直在尝试在Windows中获取进程内存信息,并且一直在关注Microsoft文档,并且已经获得了这段代码,但它似乎不起作用。 It is supposed to print succeeded, but it doesn't print anything at all. 它应该可以成功打印,但是根本不打印任何内容。 My debugging tells me its because hProcess = null, but i don't understand why. 我的调试告诉我它是因为hProcess = null,但是我不明白为什么。 Here is my current code 这是我当前的代码

#include <windows.h>
#include <stdio.h>
#include <psapi.h>
#include<iostream>

using namespace std;

int main()
{
    DWORD aProcesses[1024], cbNeeded, cProcesses;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
    {
        return 1;
    }

    cProcesses = cbNeeded / sizeof(DWORD);

    for (int i = 0; i < cProcesses; i++ )
    {
    int processID = aProcesses[i];
    HANDLE hProcess;
    PROCESS_MEMORY_COUNTERS pmc;
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID );

    if (NULL == hProcess)
        return 2;

    if ( GetProcessMemoryInfo( hProcess, &pmc, sizeof(pmc)) )
    {
        cout<<"SUCCEEDED";
    }

    CloseHandle( hProcess );
    }

    return 0;
}

You probably don't have permission to read memory of certain (privileged system) processes. 您可能无权读取某些(特权系统)进程的内存。 Check GetLastError if you get a NULL handle back from OpenProcess . 如果您从OpenProcess返回了NULL句柄,请检查GetLastError

In general checking and handling Win32 error conditions is a good practice, even if the API 'usually' works for you. 通常,即使API“通常”为您工作,检查和处理Win32错误情况也是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM