如何将 byte[] 数组转换为 IntPtr?[复制]
-
14-11-2019 - |
题
可能的重复:
如何在 C# 中从 byte[] 获取 IntPtr
我正在从内存中读取字符串
byte[] array =
reader.ReadProcessMemory((IntPtr)address, (uint)255, out bytesReadSize);
然后我将此数组转换为字符串。
我现在遇到一个问题,因为在程序内存中的地址 003A53D4 下有一个指针,它指向一个字符串。如何获取字符串的地址?谢谢 :)
这就是我尝试过的:
IntPtr pointers_address = new IntPtr(module_base_address + 3822548);
byte[] pointer_arrays =
reader.ReadProcessMemory(pointers_address, (uint)16, out bytesReadSize2);
IntPtr pointer_for_string = new IntPtr();
Marshal.Copy(pointers_array, 0, pointer_for_string, 16);
它说(大约第四行):
值不能为空。参数名称:目的地
当我改变时 新的 IntPtr() 到新的 整数指针(1)它说
尝试读取或写入受保护的内存。这通常表明其他记忆是损坏的。
解决方案
您可以使用 Encoding.GetString()
将字节转换为字符串。使用哪种编码取决于字符串的编码,例如Encoding.UTF8.GetString(pointer_arrays, 0) 表示 UTF8 编码,Encoding.Unicode 表示 unicode,Encoding.ASCII 表示 ASCII 或 Encoding.Default 表示系统的默认代码页。
其他提示
最好的方法(IMO)如下:
GCHandle pinned = GCHandle.Alloc(array , GCHandleType.Pinned);
IntPtr address = pinned.AddrOfPinnedObject();
reader.ReadProcessMemory(address, (uint)255, out bytesReadSize);
pinned.Free();
不隶属于 StackOverflow