Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Targeting in C#.NET for 32 bit and 64 bit

Tags:

c#

.net

Supposing I have a windows app developed based on C#. I want to ensure that it works on 32 bit and 64 bit both. But I don't want to change the config settings or application settings time and again. Is there a way to test both variants?

like image 865
Jasmine Avatar asked Jul 09 '12 17:07

Jasmine


2 Answers

There are a couple of options.

  1. You can target AnyCPU. If you're program is 100% C#/managed code, with no native dependencies, this will cause it to run 64bit on 64bit Operating systems and 32bit on 32bit Operating systems.

  2. Target x86. This will cause it to run 32bit everywhere, which works properly on 64bit Windows (via WOW64). This works properly if you're using native (32bit) libraries, as well.

  3. Make two builds, and two separate deployments. This allows you to use native code and still run 64bit on 64bit operating systems, but is far more work.

Since 32bit applications run well on 64bit operating systems, there is rarely reason to run the program natively in 64bit. This is typically only really beneficial if you're processing large amounts of data and truly need access to larger memory space than you can get in 32bit processes. In .NET, this typically means you'll want to build 64bit if you're going to use more than 1.2-1.6gb of RAM for your program. Otherwise, 32bit will work fine everywhere.

like image 93
Reed Copsey Avatar answered Nov 07 '22 09:11

Reed Copsey


If you target AnyCPU, most .NET programs will run unaltered on 32-bit or 64-bit machines. The code will be jitted to 32-bit or 64-bit code as appropriate for the target operating system.

The exception where you need to use care is when using interop to unmanaged code.

If you do interop to unmanaged code and require to run as 64-bit on a 64-bit platform, you will have to make two builds.

If you do interop to unmanaged code and can accept running as a 32-bit process, target x86. That avoids two builds, and will run your .NET code as a 32-bit process in 32-bit and 64-bit platforms.

like image 33
Eric J. Avatar answered Nov 07 '22 08:11

Eric J.