IntPtr 到 Uint32? C#
我需要调用 VirtualAllocEx ,它返回 IntPtr 。 我调用该函数来获取一个空地址,以便我可以在那里编写我的 codecave(这是在另一个进程中)。
如何将结果转换为 UInt32,以便我最近可以使用该地址调用 WriteProcessMemory() ?
I need to call VirtualAllocEx and it returns IntPtr.
I call that function to get an empty address so I can write my codecave there(this is in another process).
How do I convert the result into UInt32,so I could call WriteProcessMemory() lately with that address?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您调用 WriteProcessMemory 时,您应该传递地址的 IntPtr 而不是 UInt32 (因为 WriteProcessMemory 需要一个指针,而不是整数)。 因此,您应该能够直接使用 VirtualAllocEx 返回的 IntPtr,而不需要将其转换为 UInt32。
When you call WriteProcessMemory, you should be passing an IntPtr for the address rather than a UInt32 (because WriteProcessMemory expects a pointer, not an integer). So you should be able to use the IntPtr returned by VirtualAllocEx directly without the need to convert it to a UInt32.
我相信你可以用 (uint)ptr 来投射它(如果它不能很好地投射,请先尝试 ptr.ToInt32() 或 ToInt64() 。至少我不知道这种方法有任何问题,还没有不过,考虑到 UInt32 的范围比 Int32 更大,并且在非负方面与 Int64 相同,它应该足够好,
尽管我不确定 Int32 在 64 位架构上的行为如何。 IntPtr 的目的是提供与平台无关的方式来存储指针。
You could just cast it with (uint)ptr I believe (If it won't cast nicely, try ptr.ToInt32() or ToInt64() first. At least I don't know of any issues with this approach, haven't used it -that- much though. Given UInt32 has larger range than Int32 and same as Int64 on non-negative side it should be good enough.
Although not sure how the Int32 behaves on 64 bit architectures. Badly I'd imagine as the reason for IntPtr is to provide platform independant way to store pointers.
您的 WriteProcessMemory 的 DLlImport 不正确,基地址是一个指针,因此应将其定义为 IntPtr。
MSDN
您需要修复 DllImport 语句。
Your DLlImport for WriteProcessMemory is incorrect, base address is a pointer so it should be defined as IntPtr.
MSDN
You need to fix your DllImport statement.