进程内存读取始终返回0

时间:2018-12-10 17:39:36

标签: c++ winapi readprocessmemory

我试图读取内存地址(float类型)。因此,在任何情况下(我尝试过int,float等。内存地址值)它都将返回0。在其他程序(Cheat Engine)中都可以工作。

#include <windows.h>
#include <process.h>
#include <iostream>

DWORD adresss = 0x00179574;
DWORD pid;
float id;



int main()
{
    HWND game = FindWindowA(0, ("Window name"));
    GetWindowThreadProcessId(game, &pid);
    HANDLE maincs = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
    for (;;)
    {
        ReadProcessMemory(maincs, (void*)adresss, &id, sizeof(int), 0);

        std::cout << id << std::endl;
        Sleep(100);
    }
}

2 个答案:

答案 0 :(得分:0)

检查您的Windows API调用是否失败:

#include <Windows.h>
#include <iostream>
#include <vector>
#include <string>


LPVOID adresss = (LPVOID)0x00179574;
DWORD pid;
float id;

int main()
{
    HWND game;
    HANDLE maincs;

    if ((game = FindWindowA(0, ("Window name"))))
    {
        GetWindowThreadProcessId(game, &pid);
        if ((maincs = OpenProcess(PROCESS_VM_READ, FALSE, pid)))
        {
            for (;;)
            {
                if (ReadProcessMemory(maincs, adresss, &id, sizeof(float), 0))
                {
                    std::cout << id << std::endl;
                }
                else std::cout << "ReadProcessMemory failed with : " << GetLastError() << std::endl;

                Sleep(100);
            }
            CloseHandle(maincs);
        }
        else std::cout << "OpenProcess failed with : " << GetLastError() << std::endl;
    }
    else std::cout << "FindWindowA failed with : " << GetLastError() << std::endl;

    std::string dummy;
    std::cin >> dummy;
    return 0;
}

告诉我们打印出的内容或是否有效

答案 1 :(得分:-1)

似乎您正在从流程中读取int的值,但将其另存为float。尝试将其更改为sizeof(float)。另外,制作address LPVOID,而不是将其强制转换为空指针。

如果仍然失败,则还有另外一件事。您似乎正在阅读的地址似乎太小。在该过程中的通用地址将类似于0x7ff6d91c0000

此外,您不需要<Process.h>ReadProcessMemory的最后一个地址应该是nullptr,而不是0