c# - C# 程序员在 C++ 中读取 ProcessMemory,始终失败

标签 c# c++ readprocessmemory

我对 C++ 的了解很模糊,但我在 C# 方面有一些丰富的经验,尽管在这种情况下它不是很有用。我有一些 C# 代码,它工作得很好。我有我认为在 C++ 中非常相似的代码,但我似乎无法让它工作或调试它。所以,这里是我编写和测试非常彻底的 C# 代码:

    [DllImport("kernel32.dll")]
    public static extern Int32 ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress,
        [In, Out] byte[] buffer, UInt32 size, out IntPtr lpNumberOfBytesRead);

    public byte[] ReadBytes(IntPtr Handle, Int64 Address, uint BytesToRead)
    {
        IntPtr ptrBytesRead;
        byte[] buffer = new byte[BytesToRead];
        ReadProcessMemory(Handle, new IntPtr(Address), buffer, BytesToRead, out ptrBytesRead);
        return buffer;
    }

    public int ReadInt32(long Address, uint length = 4, IntPtr? Handle = null)
    {
        return BitConverter.ToInt32(ReadBytes(getIntPtr(Handle), Address, length), 0);
    }

因此,此函数 ReadInt32 获取一个地址,将其添加到我在初始化 Util 类时存储的基地址,并使用在初始化时再次获取的句柄读取最多 4 个字节的内存。如果我正确设置值,我 100% 知道这有效。

这段代码有点长,请原谅我的无知,但我不想把这些留给想象,我无法开始提出可能不正确的地方,因为我在这个世界上受的训练还不够C++ 的。

DWORD address = 0x3C02A8;
int value = 0;
DWORD pid;
HWND hwnd;
DWORD baseAddress;
DWORD toread;
SIZE_T bytesRead;

// Get a process handle
hwnd = FindWindow(NULL,L"Tibia - Knightski"); //Finds the Window called "Minesweeper"
if(!hwnd) //If none, display an error
{
    cout <<"Window not found!\n";
    cin.get();
}
else
{
    cout << "Window found: " << hwnd << endl;
    cin.get();
}

// Get a base address
GetWindowThreadProcessId(hwnd,&pid); //Get the process id and place it in pid
HANDLE phandle = OpenProcess(PROCESS_VM_READ,0,pid); //Get permission to read
if(!phandle) //Once again, if it fails, tell us
{
    cout <<"Could not get handle!\n";
    cin.get();
}
else
{
    cout << "Handle obtained: " << phandle << endl;
    cin.get();
    baseAddress = (DWORD)phandle;
    cout << "Base Address obtained: " << baseAddress << endl;
    cin.get();
}

toread = baseAddress + address;

// Read memory at base + address
if (ReadProcessMemory(phandle, (void*)address, &value, 4, &bytesRead))
{
    cout << value;
    cin.get();
}
else
{
    cout << "Failed to read memory" << GetLastError() << endl;
    cout << "Bytes read: " << bytesRead << endl;
    cin.get();
}

我试图读取的内存地址是 0x3BE1E0,但我已将基地址 (0x20C8) 添加到此以获得 0x3C02A8。我假设本网站的读者知道 handle 而不是...

感谢您的宝贵时间。请注意,我希望对我做错的事情做出解释而不是回答,所以如果您有空的话请记住这一点。一个答案就可以了,因为无论如何我很可能会研究结果。

输出是这样的:

Window found: 00D5014C

Handle obtained: 00000044

Base Address obtained: 68

Failed to read memory299
Bytes read: 0

最佳答案

这个转换是完全错误的。

baseAddress = (DWORD)phandle;

进程句柄根本不是内存地址(尽管模块句柄是)。进程句柄是内核保存的数组的索引。

您需要远程枚举进程的模块。

将允许您获取模块基址。

关于c# - C# 程序员在 C++ 中读取 ProcessMemory,始终失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17458286/

相关文章:

C# 创建对象 obj = new T()?

c# - 使用操作上下文跟踪相关事件的问题

c++ - 有没有一种简单的方法可以转发到具有匹配函数签名的成员函数?

C++ ReadProcessMemory 收到 998/3E6 错误

c# - 将函数从 C++ 转换为 C#

Python Ctypes Read/WriteProcessMemory() - 错误 5/998 帮助!

c# - 如何检查线程代码是否在 try-catch block 中运行

c# - 将 Windows Phone 和 Windows 8 应用程序连接到 SQL Server

c++ - CLion 禁用 C++98 模式以支持 C++11

c++ - 矩阵每一列的 eigen3 arraywise 矩阵 vector 乘积