我已经尝试使用 wchar_t 和一个 for 循环来通过 wchar 读取内存 wchar 并且它有效。 工作代码:
int cl = 20;
std::wstring wstr;
wchar_t L;
for (int i = 0; i < cl; i++) {
ReadProcessMemory(ProcHandle, (unsigned char*)Address, &L, 2, NULL);
Address += 2;
wstr.push_back(L);
}
std::wcout << wstr << std::endl;
现在,当我尝试使用 std::wstring 并直接读取它时,无论出于何种原因,它都失败了。
int cl = 20;
std::wstring L;
L.resize(cl); // could use reserve?
ReadProcessMemory(ProcHandle, (unsigned char*)Address, &L, cl*2, NULL);
std::wcout << L << std::endl;
我想我会使用 (cl * 2)
作为大小,因为 wchar_t 有 2 个字符大小。
我希望它能将 wstring 打印到 wcout,但它却出现了类似于 Failed to read sequence
注意:我不能使用 wchat_t[20],因为我以后希望 cl 是动态的。
编辑:忘了说我在 std c++17 上
最佳答案
std::vector<wchar_t>
更适合你的情况。
&L
是字符串对象的地址,而不是字符串缓冲区。你想用 &L[0]
, 第一个 wchar 的地址。
关于c++ - 我如何将内存读入 wstring?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57731738/