【发布时间】:2019-08-30 18:36:49
【问题描述】:
我已经尝试使用 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 上
【问题讨论】:
-
"因为 wchar_t 有 2 个字符大小。" - 你真的确定你真的保证吗?
-
请在问题中包含确切的错误消息,而不仅仅是“类似的东西”
-
// could use reserve?不。 Reserve 实际上并没有增加字符串的大小。您仍然会超出字符串的范围进行写入(即使您知道分配的内存对它有好处)。例如,如果您复制L,则可能无法正确复制数据。据它所知,大小仍为 0,因此它会尝试复制 0 个字符。