0

I am building a C# project for a DLL that will be used in an x64 environment. Originally I was using 'Any CPU" as the platform target.

However, when I test it I found it failed during library loading and I found it loaded some 32 bit libraries. I changed it to 'x64' and everything works as expected. In what case should I use 'x64' and in what case I should use 'Any CPU'?

I used to think 'Any CPU' is safer and it will automatically determine whether 32-bit or 64-bit library is required.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    read this https://social.msdn.microsoft.com/Forums/en-US/5ad0ff2c-558c-43ba-a59d-9cd0a0785103/any-cpu-vs-x86-vs-x64-solution-platforms?forum=netfx64bit https://stackoverflow.com/questions/516730/what-does-the-visual-studio-any-cpu-target-mean – Jun Rikson Apr 02 '18 at 02:55
  • 2
    Sorry I've made an error in my post. I'm building a c# project for **EXE** instead of DLL that runs on 64-bit environment. – Jianhui Wang Apr 02 '18 at 02:56

1 Answers1

0

The answer to this I believe lies in the specification of the environment the program is going to be run on. You can find this out by looking at your operating system.

Since you said in your comment that it's an EXE file (Windows), you can go into system information and look at the system type. If you want it to run on an x64 environment then use x64 and keep doing what you're doing. Your question is detailed enough that it almost answers itself.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jamin
  • 1,362
  • 8
  • 22
  • 1
    The executable is on 64-bit system and I'm pretty sure all project settings are the same, but when I use 'Any CPU' to build, it failed the library loading and as I switch to 'x64' everything works fine. Strange? – Jianhui Wang Apr 02 '18 at 02:59
  • It is a little strange that it doesn't work on the Any CPU to build option. It's possible and likely that you are using code that is only supported on 32 bit systems when you choose that option and it crashes when you run your code on a 64 bit system. This may be inherited naturally, and you may have to dictate specific properties for it to run properly. – Jamin Apr 02 '18 at 03:03
  • One thing I forget to mention is when I set the target to 'x64', the 'Prefer 32bit' check box is checked. I'm not sure if this will make things different – Jianhui Wang Apr 02 '18 at 03:08
  • That can actually makes a huge difference as noted in this article : http://blogs.microsoft.co.il/sasha/2012/04/04/what-anycpu-really-means-as-of-net-45-and-visual-studio-11/. I'd test it without the checkbox if you really want/need it to work on both x86 and x64 systems. – Jamin Apr 02 '18 at 03:13
  • 1
    Hi Jamin, Thanks for pointing this out! – Jianhui Wang Apr 02 '18 at 03:32