The Core Technologies Blog

Professional Software for Windows Services / 24×7 Operation


Q & A: How do I force my .NET console application to run 64-bit?

Q & A - .NET
  We have installed AlwaysUp version 9.7.2.88 (Sep. 2 2016) on Windows Server 2012 R2 Standard. We have multiple jobs that run a 64-bit .NET Console Application as a windows service, but we keep getting a SystemOutMemoryException error. What we found watching the Task Manager is that the EXE always runs as 32-bit. How do we get AlwaysUp to run the console app as 64-bit?

— Clayton

Hi Clayton. There are a couple of ways to force your .NET application to run in 64-bit mode:

  1. Build a 64-Bit .NET Version

    If you use Microsoft’s Visual Studio, the build configuration is likely be set to Any CPU (the default):

    Visual Studio Any CPU Configuration

    This will produce an executable that will run in either 32-bit or 64-bit mode, depending on the version of .NET available.

    To run your executable in 64-bit only, change the configuration to x64 and rebuild.

    If your project doesn’t have a x64 configuration, create one like this:

    1. Select Build > Configuration Manager…

    2. In the Configuration Manager,

    3. click the Active solution platform entry and select <New> from the menu that appears:

      Create a New Visual Studio Configuration

    4. In the New Solution Platform window, choose x64 in the top box. We recommend copying settings from Any CPU and creating new project platforms (the default):

      Create a New Visual Studio Configuration: Select x64

    5. Click the OK button to record your changes. Close the Configuration Manager

    6. You should now have x64 as a build option. Set it and recompile to produce a 64-bit only executable:

      Activate the x64 Configuration in Visual Studio

  2. Use CorFlags to Force .NET 64-Bit

    If your application is built with “Any CPU” but you can’t rebuild it in Visual Studio, Microsoft’s free CorFlags utility will come to the rescue.

    Corflags.exe is distributed with the Windows 10 SDK. Be sure to select the “.NET Development Tools” component when installing.

    We actually found the CorFlags exe file in a couple of places on our hard drive:

    C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.6.1 Tools\Corflags.exe

    C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\Corflags.exe

    You may find CorFlags in older .NET SDK installations as well — it has been around since the release of .NET 2.0 (2005).

    Once you have located CorFlags:

    1. Make a copy of the executable you want to run in 64-bit. The application we used for testing is called SystemInfoDotNet.exe, so we copied it to SystemInfoDotNet64.exe.

    2. Open a command prompt with admin rights.

    3. CD to the folder containing the copied application.

    4. Run CorFlags on the copied application file. Specify the /32BIT- option to strip away the ability to run in 32-bit mode:

      Run CorFlags /32BIT-

    The copied executable will always run in 64-bit mode.

    For example, our SystemInfoDotNet utility — which displays basic process and system information — normally runs in 32-bit mode:

    SystemInfoDotNet running before CorFlags

    But after running CorFlags on SystemInfoDotNet64, it magically runs in 64-bit mode:

    SystemInfoDotNet running after CorFlags

Best of luck with your .NET application Clayton!

Posted in AlwaysUp | Tagged , , , , , , | 2 Comments

2 Responses to Q & A: How do I force my .NET console application to run 64-bit?

  1. Mangala V Sharma says:

    MY .dll is 32 my console application works on windows 10 but fails on server 2012 r2.

  2. Sorry to hear that Mangala!

    How does it fail on Server 2012? Is there an error message?

Leave a Reply

Your email address will not be published. Required fields are marked *