问题
I think I should get 8 when I use IntPtr.Size. However I still get 4 on x64 machine with Widnows 7 x64, why?
回答1:
check your file CPU architecture, is it x86? It should be CPU any / 64bit
回答2:
The 64 bit operating system implements an emulated environment known as WOW64 which emulates the 32 bit Windows environment. You are building your program targeting x86, i.e. 32 bit. That means that your process runs under the emulator as a 32 bit process and of course pointers are 4 bytes wide.
If you change your options to target x64 or AnyCPU then the pointer size will be 8 bytes when your process runs on a 64 bit system.
回答3:
In addition to the answers above, even if you have selected Any CPU architecture, VS2013 has a new option in project properties called "Prefer 32-bit". You must turn it off on 64-bit development machines to get IntPtr.Size = 8.
回答4:
Check your build target: x86/x64 or any cpu If your configuration is x86 or any cpu, then, the intptr is 4 maybe.
More suggestion:
If you don't have requirement to run your program in x64 mode, please donn't change the build target to x64 because x64 mode has negative effects on both performance and space usage. I forgot the link of original article on MSDN, but the main reason is the increase of ptr size and GC burden, you can search that article.
来源:https://stackoverflow.com/questions/9206483/why-intptr-size-is-4-on-windows-x64