views:

731

answers:

6

I need to build an application for 64-bit platform to utilizes some DLL's built for 64-bit machines. I know how to you can build each project separately to target for 64-bit platform. But when you have many projects in one Visual Studio solution, and some DLL's are dependent of the others. How do you decide what projects to build as x64, x86, or Any CPU.

Thanks in advance.

A: 

If there is one 64 bit-only dependency then you need to build every component as 64 bit.

ZippyV
+3  A: 

The bitness the application is determined by the executable that loads the DLL's. As long as the launching process is configured to target x64, then the DLL's will be loaded as x64 as long as they are configured as x64 or Any CPU. So, you don't need to configure all the DLL's as x64, only the launching application. The rest can be left as Any CPU unless they have specific 64 bit dependencies.

Michael McCloskey
+2  A: 

For me it really comes down to what I depend on. Or more accurately what the transitive closure of DLL dependencies for my application depend on. If any of them have a particular architecture dependency (x86 or amd64) I set my project to have the same dependency. This way I help ensure than any issue with loading in the wrong platform will show up as early as possible.

If there is no platform dependency and my code has none I set it to AnyCPU until I find some reason to do otherwise.

JaredPar
+6  A: 

When a program is compiled in "ANY CPU" it means that it is architecture neutral - It can be run in both 64bit process or a 32bit process. It is determined by the platform that the program is being executed on. If it is a 64bit platform, a program built in ANY CPU will load as a 64 bit process. On the other hand if it is a 32 bit platform, a program built in ANY CPU will load in a 32 bit process.

However, you cannot build everything as "ANY CPU" because you may have dependencies that requires one platform or the other

A program must be built with x64 mode if it has x64 dependencies (relying on other libraries that are built in x64). This also requires the program to be executed on a 64 bit platform

A program must be built with x86 mode if it has unmanaged 32 bit dependencies or if it has managed libraries that are buit in x86.

A program program has neither x64 bit dependency or unmanaged 32 bit dependencies can be built in "any cpu".

However, regardless of these rules, if you build in ANY CPU you should test thoroughly on both a 64 bit platform and a 32 bit platform. This post discusses other possible issues in more detail.

More on ANY CPU

Sprklnh2o
+2  A: 

To add on top of other great answers. I found some useful information on MSDN

On a 64-bit Windows operating system:

  • Assemblies compiled with /platform:x86 will execute on the 32 bit CLR running under WOW64.
  • Executables compiled with the /platform:anycpu will execute on the 64 bit CLR.
  • DLLs compiled with the /platform:anycpu will execute on the same CLR as the process into which it is being loaded.

Also note that x86 DLLs cannot be loaded on 64 bit process. If x64 version of the DLLs cannot be obtained, the alternative is to run x86 DLLs on a 32-bit (WOW64) process and do interprocess communication (IPC) back with the 64 bit process. More detail.

weilin8
+1  A: 

There was a great discussion from last year about this on .NET Rocks, here, it really helped me understand all the nuances

johnc