I am having trouble running a C# executable from memory. I have seen a number of questions and articles covering this issue however I have been unable to get these solutions to work and none of them seem to be running into the same error that I am.
Link 1
Link 2
Link 3
Link 4
Loader:
using System;
using System.Reflection;
namespace MyApp
{
internal class Program
{
static async Task Main(string[] args)
{
String filepath = "C:\Users\Bob\source\repos\ConsoleApp1\bin\Debug\net8.0\ConsoleApp1.exe";
FileStream fs = new FileStream(filepath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();
Assembly a = Assembly.Load(bin);
Console.WriteLine("A.entrypoint = {0}", a.EntryPoint); //Crashes here
}
}
}
The application being loaded is the default template that you get when you start a C# console project. If I run that program in isolation it works normally.
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
The crash I am getting:
Unhandled exception. System.BadImageFormatException: Bad IL format.
So I am assuming somewhere along the way the binary is being altered and the one were trying to execute is different from what is being loaded.