Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在特定地址读取进程内存?_C++_Windows_Winapi_Memory_Memory Management - Fatal编程技术网

C++ 如何在特定地址读取进程内存?

C++ 如何在特定地址读取进程内存?,c++,windows,winapi,memory,memory-management,C++,Windows,Winapi,Memory,Memory Management,我想从一个进程的内存中从一个已知的内存位置开始读取64个字节。为此,我编写了以下C++代码: #include <iostream> #include <Windows.h> #include <iomanip> using namespace std; int main() { LPCVOID Address = (LPCVOID)0x0000029FC0C41FF0; // the memory address where I want to

我想从一个进程的内存中从一个已知的内存位置开始读取64个字节。为此,我编写了以下
C++
代码:

#include <iostream>
#include <Windows.h>
#include <iomanip>

using namespace std;

int main() {
    LPCVOID Address = (LPCVOID)0x0000029FC0C41FF0; // the memory address where I want to read from
    byte buffer[64];
    HANDLE hProcess = OpenProcess(PROCESS_VM_READ, 0, 13868);  // the process ID of the process


    ReadProcessMemory(hProcess, Address, &buffer, sizeof(buffer), 0);

    const int siz_ar = sizeof(buffer) / sizeof(int);   // the rest is trying to display the bytes read on stdout

    for (int i = 0; i < siz_ar; ++i)
        cout << hex << setfill('0') << setw(2) << buffer[i] << " ";
    cout << endl;
}

如何更正此程序以从特定进程(由进程ID指定)的特定内存地址读取原始字节?

您的
字节可能是
字符的别名。如果将
char
传递给
std::cout
,它将不会以数字形式打印,而是以字符形式打印。强制转换为非字符类型,如
unsigned int
first


然后您会注意到它只输出
0xCC
,这是未初始化内存(
在某些代码页中)。对您的呼叫执行错误检查,以找出为什么
缓冲区一直没有填满。

您的
字节可能是
字符的别名。如果将
char
传递给
std::cout
,它将不会以数字形式打印,而是以字符形式打印。强制转换为非字符类型,如
unsigned int
first


然后您会注意到它只输出
0xCC
,这是未初始化内存(
在某些代码页中)。对您的调用执行错误检查,以找出为什么
缓冲区
从未被填满。

您的代码不执行任何错误检查(例如,检查
OpenProcess
是否成功,检查
ReadProcessMemory
是否成功等)。首先这样做。也许现在太早了,但是for循环中的
hex
变量来自哪里?另外,在生产代码中,
使用名称空间std不受欢迎。请参见此处了解详细信息:下一步是添加错误检查。阅读您调用的两个api函数的文档,它们告诉您如何检查错误。@schaiba它是
std::hex
,所以是的,太早了!;-)当传递到
cout
时,需要将
buffer[i]
强制转换为
int
<代码>操作您的代码不执行任何错误检查(例如检查
OpenProcess
是否成功,以及
ReadProcessMemory
是否成功等)。首先这样做。也许现在太早了,但是for循环中的
hex
变量来自哪里?另外,在生产代码中,
使用名称空间std不受欢迎。请参见此处了解详细信息:下一步是添加错误检查。阅读您调用的两个api函数的文档,它们告诉您如何检查错误。@schaiba它是
std::hex
,所以是的,太早了!;-)当传递到
cout
时,需要将
buffer[i]
强制转换为
int
<代码>操作员
0╠ 0╠ 0╠ 0╠ 0╠ 0╠ 0╠ 0╠