C# AnyCPU 和读/写进程内存
我的 C# 程序是使用 AnyCPU 选项编译的,我使用 P/Invoke 以这种方式调用本机 api:
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);
我的疑问是,我的 C# 程序自使用 AnyCPU 编译以来可以读取和写入 32 位和 64 位进程吗?或者会有问题吗?我问这个是因为我只有 32 位操作系统,所以我无法测试它。谢谢
My C# program is compiled with AnyCPU option and i am using P/Invoke to call native apis this way:
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool WriteProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, uint nSize, out UIntPtr lpNumberOfBytesWritten);
My guestion is, can my c# program read from and write to both 32bit and 64bit processes since its compiled with anycpu? or would be there problems? i am asking this because i have only 32bit OS so i can't test it. Thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 32 位操作系统上,所有进程都是 32 位,因此不会出现问题。在 64 位操作系统上,您的 AnyCPU 进程运行 64 位,唯一可能的不匹配是 32 位进程。但是用64位指针存储32位地址是没有问题的。如果您尝试从 32 位进程读取/写入 64 位进程中的内存,您将会陷入困境。但既然你做了相反的事情,那就没有问题了。
On a 32 bit OS, all processes are 32 bit and so no issues arise. On a 64 bit OS your AnyCPU process runs 64 bit and the only possible mismatch is then with 32 bit processes. But it's no problem to store a 32 bit address in a 64 bit pointer. If you were trying to read/write memory in a 64 bit process from a 32 bit process you would be stuck. But since you are doing the opposite there's no trouble.