Last active
March 7, 2016 22:15
-
-
Save lennybacon/995038b57a41aaca463e to your computer and use it in GitHub Desktop.
RunAsX86
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Linq; | |
using System.Reflection; | |
using System.Windows.Forms; | |
/*********************************************************************** | |
* CoolSoft RunAsX86 | |
*********************************************************************** | |
* Based on code by CoolSoft: | |
* http://coolsoft.altervista.org/en/blog/2012/01/run-anycpu-net-applications-x86-mode | |
* Based on original Gabriel Schenker idea: | |
* http://lostechies.com/gabrielschenker/2009/10/21/force-net-application-to-run-in-32bit-process-on-64bit-os/ | |
************************************************************/ | |
// This class MUST be compiled as X86 exe | |
namespace RunAsX86 | |
{ | |
class Program | |
{ | |
[STAThread] | |
static int Main(string[] args) | |
{ | |
if (args.Length == 0) | |
{ | |
var ofd = new OpenFileDialog | |
{ | |
Title = "Choose a .NET executable to run in X86 mode", | |
Filter = "Executable files (*.exe;*.dll)|*.exe;*.dll|All files|*.*", | |
FilterIndex = 1, | |
CheckFileExists = true, | |
}; | |
if (ofd.ShowDialog() != DialogResult.OK) | |
{ | |
Usage(); | |
return 1; | |
} | |
args = new [] { ofd.FileName }; | |
} | |
args[0] = Path.GetFullPath(args[0]); | |
// test if file exists | |
if (!File.Exists(args[0])) | |
{ | |
Console.WriteLine("ERROR: exe file not found: " + args[0]); | |
return 1; | |
} | |
// load the assembly | |
var assemblyName = args[0]; | |
var binDir = Path.GetDirectoryName(assemblyName); | |
AppDomain.CurrentDomain.AssemblyResolve += (a, b) => { | |
var assemblyToLoad = b.Name; | |
var an = new AssemblyName(b.Name); | |
Console.WriteLine(b.Name); | |
var assemblyPath = Path.Combine(binDir, an.Name + ".dll"); | |
if (File.Exists(assemblyPath)) | |
{ | |
try | |
{ | |
return Assembly.LoadFile(assemblyPath); | |
} | |
catch (Exception ex) | |
{ | |
var color = Console.ForegroundColor; | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(ex.ToString()); | |
Console.ForegroundColor = color; | |
} | |
} | |
return null; | |
}; | |
try | |
{ | |
var setupPath1 = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; | |
AppDomain.CurrentDomain.AppendPrivatePath(binDir); | |
var setupPath2 = AppDomain.CurrentDomain.SetupInformation.PrivateBinPath; | |
var assembly = Assembly.LoadFile(assemblyName); | |
// find the entry point of the assembly | |
MethodInfo mi = null; | |
Type type = null; | |
foreach (var t in assembly.GetTypes()) | |
{ | |
var mainMethod = | |
t.GetMethod("Main", BindingFlags.Static | BindingFlags.Public) | |
?? t.GetMethod( | |
"Main", BindingFlags.Static | BindingFlags.NonPublic); | |
if (mainMethod != null) | |
{ | |
mi = mainMethod; | |
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 | |
var newArgs = args.Skip(1).ToArray(); | |
// call the entry point of the wrapped assembly and forward parameters | |
var ret = mi.Invoke(null, new object[] { newArgs }); | |
Console.WriteLine("Hit [ENTER] to exit"); | |
Console.ReadLine(); | |
return (ret == null) ? 0 : (int) ret; | |
} | |
catch (Exception ex) | |
{ | |
var color = Console.ForegroundColor; | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine(ex.ToString()); | |
Console.ForegroundColor = color; | |
Console.ReadLine(); | |
Console.WriteLine("Loaded assemblies:"); | |
var assemblies = AppDomain.CurrentDomain.GetAssemblies(); | |
foreach (var assembly in assemblies) | |
{ | |
Console.WriteLine(assembly.FullName); | |
Console.WriteLine(assembly.CodeBase); | |
Console.WriteLine(assembly.Location); | |
Console.WriteLine(); | |
} | |
Console.ReadLine(); | |
return -1; | |
} | |
} | |
/// <summary> | |
/// Print usage infos | |
/// </summary> | |
static void Usage() | |
{ | |
Console.WriteLine(@" | |
lennybacon - 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment