c++ - 我如何将内存读入 wstring?

标签 c++ wstring

我已经尝试使用 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/

相关文章:

c++ - C++ 中的 string.function() 或 function(string) 有什么区别?

c++ - 检查枚举有效性

c++ - 有没有办法通过逆向工程来恢复结构。指向该结构的指针由 DLL 中唯一用于导出的函数返回

c++ - 错误 C2593 : Operator = is ambiguous

C++ L"whatever"-> wstring -> basic_string<T>

c++ - 如何同时从多个线程访问 MySQL

c++ - 强制在 Visual Studio 2015 调试器中加载不匹配的符号

wstring - ChaiScript 中的 std::wstring

C++ 将 unsigned char append 到 wstring

Android NDK C++ 'wstring' 支持