3

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

thatoneguy
  • 45
  • 1
  • 3

1 Answers1

3

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.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490