RunAsX86, a tool to run AnyCPU .NET applications in X86 mode

Please let our ADS show!

This sites offers only FREE software and it's supported by a few advertisement boxes (no intrusive popups).
Please:

  • disable your AdBlocker by adding CoolSoft website to whitelist
  • give the proper cookie consent
  • enable JavaScript for this website

This seconds wait is to let you update your browser configuration...

Ok, I've done the required changes... now show me your content!
!!! Please enable JavaScript !!!

When compiling a .NET application you have three available choices for the Platform target parameter:

X86: The code will run in 32bit mode both on x86 and on x64 OS.

X64: Executable will run only in 64bit mode and on x64 OS, no way to run it on x86 OS.

AnyCPU: The same executable will run in 32bit mode on x86 OS and in 64bit mode on x64 OS. It's up to the .NET framework runtime to choose how to JIT compile it to native code.

Most of the times programmers choose the latter, because this way an application is flexible and runs everywhere.

But what if you need to force an app to run in X86 mode (i.e. it needs to interface with a 32bit only driver)?
The best way is to recompile it choosing X86 as platform target, but if you don't have the sources you're out of the game.

There's a lot of documentation on the NET on how to patch an existing EXE to make it run in X86 mode, and all of them require an utility called CorFlags.exe.
The sad part is that this utility is only available within the Platform SDK (hundreds of MB for just a few KB tool...).

That's why I wrote RunAsX86, a really small wrapper, based on the original idea of Gabriel Schenker, to (try to) run a .NET AnyCPU application in X86 mode.
It must be compiled in AnyCPU mode and when run, it loads your .NET application into its 32bit process then starts it.
These are the changes I made:

  • added a dialog for EXE file selection
  • better error handling
  • better command line parameters handling
  • removed null reference exception

To run a .NET application in X86 mode, copy RunAsX86.exe into the folder that contains your .NET executable, then simply run it and choose your .NET exe.
If you need to pass some command line parameters to the application, open a command prompt then type:
RunAsX86 filename.exe param1 param2 ...

Download Download RunAsX86 here.

Source code

using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
 
/***********************************************************************
 * CoolSoft RunAsX86
 ***********************************************************************
 * Based on original Gabriel Schenker idea:
 * http://lostechies.com/gabrielschenker/2009/10/21/force-net-application-to-run-in-32bit-process-on-64bit-os/
 *
 * changes:
 * - dialog for EXE file selection
 * - better error handling
 * - better command line parameters handling
 * - removed null reference exception
 ************************************************************/
 
// This class MUST be compiled as X86 exe
 
namespace RunAsX86
{
class Program
    {
        [STAThread]
        static int Main(string[] args)
        {
            // test if we have enough params
            if (args.Length == 0) {
                System.Windows.Forms.OpenFileDialog ofd = new System.Windows.Forms.OpenFileDialog();
                ofd.Title = "Choose a .NET executable to run in X86 mode";
                ofd.Filter = "Executable files (*.exe;*.dll)|*.exe;*.dll|All files|*.*";
                ofd.FilterIndex = 1;
                ofd.CheckFileExists = true;
                if (ofd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    Usage();
                    return 1;
                }
                else
                {
                    args = new string[] { ofd.FileName };
                }
            }
 
            // test if file exists
            if(! System.IO.File.Exists(args[0])) {
                Console.WriteLine("ERROR: exe file not found: " + args[0]);
                return 1;
            }
 
            // load the assembly
            var directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            var assemblyName = Path.Combine(directory, args[0]);
            var assembly = Assembly.LoadFile(assemblyName);
 
            // find the entry point of the assembly
            Type type = null;
            MethodInfo mi = null;
            foreach(Type t in assembly.GetTypes()) {
                if((mi = t.GetMethod("Main")) != null) {
                    type = t;
                    break;
                }
            }
 
            // is there a Main() method?
            if(type == null) {
                Console.WriteLine("ERROR: can't find valid entry point");
                return 1;
            }
 
            // extract arguments to be passed to the called entry point
            string[] newArgs = new string[args.Length-1];
            for (int i = 1; i < args.Length; i++) {
                newArgs[i-1] = args[i];
            }
 
            // call the entry point of the wrapped assembly and forward command line parameters
            object ret = mi.Invoke(type, new object[] { newArgs });
            return (ret == null) ? 0 : (int)ret;
        }
 
 
        /// 

/// Print usage infos ///

static void Usage() { Console.WriteLine(@" CoolSoft - RunAsX86 - v.1.0 --------------------------- Runs a .NET executable in X86 mode. Usage: RunAsX86 filename.exe [param1] [param2] ... filename.exe filename of the .NET executable to run (specify the full path if needed). param1, param2, ... These parameters will be passed untouched to filename.exe Press any key to continue. "); Console.ReadKey(true); } } }

 

 

Category: 

Comments

I clicked on your download link for this application, but got nothing but this webpage that says "page not found", where is this download?

Download link now fixed, thanks for your feedback.

Using AppDomain.CurrentDomain.ExecuteAssembly(..) is better solution than  looking for entry point of the assembly.

server2008 x64 - net cmdline exe - ERROR: can't find valid entry point

This wrapper works only for .NET executables.

What exe are trying to run?
Can you please write the whole command line?

How to Open and Run 32-bit Command Prompt in 64-bit (x64) Windows

64-bit or x64 version of Windows operating system such as Windows Server 2003, Windows XP Professional x64 Edition, Windows Vista and Windows Server 2008 uses the Microsoft Windows-32-on-Windows-64 (WOW64) subsystem layer to run 32-bit programs, binaries or drivers without modifications, change or transition to 64-bit based binaries, as the 64-bit OS is optimized to run native 64-bit programs and does not provide support for 16-bit binaries or 32-bit drivers.

To reduce compatibility issue and prevent a 32-bit program from accidentally accessing data from 64-bit application, WOW64 subsystem isolates 32-bit binaries from 64-bit binaries by redirecting registry calls and some file system calls. Other than separate registry hive for WOW64 redirected 32-bit values, %systemroot%\System32 and Program Files folder is designated as 64-bit DLLs only too, and all access or I/O of 32-bit binaries been redirected from %windir%\System32 folder to the %windir%\SysWOW64 folder, and from Program Files to Program Files (x86) directory.

The command prompt (cmd.exe) is also affected by File System Redirection feature, where command line commands or .bat and .cmd batch script may not be able to access, install, change, modify, delete or write to “Program Files” or “%windir%\System32″ folder. The WOW64 subsystem redirects and installs 32-bit programs in the “Program Files (x86)” and “%systemroot%\SysWOW64″ folder.

To access the correct folder, programmer or developer must change the command-line script, or using Sysnative virtual directory alias to access %windir%\System32 instead of %windir%\SysWOW64. When this is not possible, or when have to access original native Program Files folder, user can type the command-line script at a 32-bit command prompt. The 32-bit command prompt automatically redirects file system calls to the correct 32-bit directory.

To start and open a 32-bit command prompt, follow these steps:

    Click Start.
    Type %windir%\SysWoW64\cmd.exe in Start Search box.

    Alternatively, press Win + R keys (or type Run in Start Search) to open Run dialog, and type %windir%\SysWoW64\cmd.exe.
    Press Enter.

32-bit Command Prompt

In 32-bit command prompt, the %programfiles% path variable will point to Program Files (x86) folder which stores all 32-bit binaries.

EDIT: Original post: http://www.tipandtrick.net/how-to-open-and-run-32-bit-command-prompt-in-64-bit-x64-windows/

Thanks for sharing this useful information..Its very nice and useful for us...

I had some old installation that was working on Windows Embedded XP (x86) and unfortuently Assembly was compiled as AnyCPU while dependency DLL's (c/c++) was Win32 compiled.
Running this application on Windows 7 (x64) started reporting bad images == x64 running process tried to load Win32 modules.
Tried this RunAsX86 and my application crashed, which was even worse since running it as x64 it was working at least partially not loading some modules.
So I had to search further...

... Microsoft has tool to change directly assembly file.
here is link to site explaining procedure: https://sites.google.com/site/kishorenetblog/asp-net/corflags-exe

In essence all you have to do:

> CorFlags.exe AssemblyName.exe /32BIT+

and assembly file is changed to run as x86 (you can see in Task Manager after process name *32 marking process as x86).

Only problem will be locating CorFlags.exe file, here is the list where I have found on my computer:
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin
C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\x64
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\X64
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\x64
C:\Program Files (x86)\Microsoft Visual Studio 8\SDK\v2.0\Bin
C:\Program Files\Microsoft.NET\SDK\v2.0 64bit\Bin

I have used the first one I found, copying it to folder where I have put the copy of assembly.exe file I wanted to change to x86 platform only.
Changed Core Bits there, and copied back to original location. And application process run as x86 (*32).

So this solution qualifies as best since nothing else needs to be changed.
And everything else was loaded correctly *fortunately for me since I have >100 3rd party dll's in use by that application (don't ask).

hope this helps someone else.

Thanks for sharing your findings.

Well i downloaded the run file because a certain app said i needed it but when i put the app in the folder it didnt still work the app said the file is still missing

i tried opening the runfile a command prompt dialog box came up then i selected the app but the run application crashed i have tried like 7 times please i need help as i need this app badly can you reply me on my email or here 

Please consider that not all applications can be forced to run x86; if your target app is linked to native DLLs (i.e. written in C++), they cannot run with a different bitness than the one they were compiled for, so you're out of luck.

What exactly is the error message?
I suppose you're receiving a "file missing" message from the native DLL loader, not from runAsX86 itself...