Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to target x64 in Visual Studio

I recently started a new job, and one of the first things I noticed everybody talking about was "updating" All our .NET apps to x64. I initially thought this odd since we all know .NET compiles to platform agnostic IL and the specific CLR runs the code.

Looking a bit further, I found this helpful article and this SO post which helped explain things.

So now I understand that the IL isn't changed, only meta data basically saying to run in WOW64 or not on a x64 system (in a nutshell).

So if I'm on a x64 system, I can specify "Any CPU" to run natively, but won't support 32bit dlls; I can specify "x86" which will support 32bit dlls (since they both would be running under WOW64); but when would I specify "x64"? It seems that 64bit dlls would be supported in the "Any CPU" scenario on a x64 system. Is this if I want to prevent someone from running my app on a 32bit system or ensuring failure when trying to load 32bit dlls?

It would also seem to me that you would only need to set it to something other than "Any CPU" if you have some 3rd party dll to worry about in your project. Is it best to leave it as "Any CPU" for every other project not dealing with others dlls?

If I do happen to set my target to "x86" because I have a 32bit 3rd party dll, is my application actually considered to be running in 64bit if on a 64bit system just under WOW64?

like image 824
earthling Avatar asked Dec 08 '11 19:12

earthling


People also ask

Should I target x86 or x64?

Since x86 apps run on both x64 and x86 systems, the most compatible choice is to build x86. If you MUST build a 64 bit solution, you'll need to target x64 and use our x64 dlls.

What is x64 in Visual Studio?

Visual Studio enables you to set up your applications to target different platforms, including 64-bit platforms. For more information on 64-bit platform support in Visual Studio, see 64-bit applications. Note. Visual Studio 2022 on Windows is now a 64-bit application.

How do I change from x86 to x64 in Visual Studio?

From the BUILD menu in Visual Studio, select Configuration Manager. From the Active solution platform drop-down list, select New. The New Solution Platform dialog displays. In the Type or select new platform combination box, select x64.

How do I change from platform target to x64?

To enable x64 as a CPU platform targetClick Configuration Manager. In the Configuration Manager dialog, open the Active solution platform drop-down list box and click <New> …. In the New Solution Platform dialog, select x64 in the Type or select the new platform drop-down list box.


2 Answers

Yes, you would specify that the project should compile to x64 if you're calling a DLL that is itself 64 bit (either because it is native, or is a managed DLL that's itself calling a 64 bit native DLL, etc).

Likewise for specifying that it should be x86 if you're dealing with 32-bit 3rd party DLLs; it will not be considered a 64-bit application if running on a 64-bit version of Windows.

If you're just dealing with pure managed code, then I'd leave things as "any". I also generally leave DLLs as "any" as well, even if the executeable will be specified to be either x86 or x64.

And even if you're dealing with native dlls, you might still be able to get away with leaving it as "any" if you're using PInvoke; you can have two versions of a class that wraps around it, one for x86, one for x64, and choose which one to use at runtime by checking the IntPtr.Size property.

And of course, if you're application needs more than 4 GBs of RAM and you want to enforce that it has to run on a 64-bit OS, then you'll also need to target x64.

like image 159
The Grand User Avatar answered Sep 22 '22 02:09

The Grand User


You would specify x64 if you're using native code through COM or P/Invoke that doesn't have a 32-bit version.

like image 41
SLaks Avatar answered Sep 19 '22 02:09

SLaks