Don't you like advertising? Neither do I...

...but CoolSoft is free, supported only by donations and a few (and non intrusive) banners.

Please add CoolSoft to your whitelist (click here to know how).

Thanks for your support.

 
 

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

warning

Warning, JavaScript is disabled!

JavaScript is not available, maybe because you disabled it globally into your browser settings or you are using an addon like NoScript.

We do not have any dangerous JavaScript running here.
Please enable JavaScript; if you're using NoScript this image will help you adding CoolSoft to your whitelist.

Thanks for your comprehension and enjoy CoolSoft.

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;
        }
 
 
        /// <summary>
        /// Print usage infos
        /// </summary>
        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...

Add new comment