-2

I need to convert an executable to Bytes, and then start a process with it.

What im using now is like:

using System.Reflection;
using System.Threading;

public static class MemoryUtils
{
    public static Thread RunFromMemory(byte[] bytes)
    {
        var thread = new Thread(new ThreadStart(() =>
        {
            var assembly = Assembly.Load(bytes);
            MethodInfo method = assembly.EntryPoint;
            if (method != null)
            {
                method.Invoke(null, null);
            }
        }));

        thread.SetApartmentState(ApartmentState.STA);
        thread.Start();

        return thread;
    }
}

Implementation:

var bytes = File.ReadAllBytes(@"C:\myexecutable.exe");
MemoryUtils.RunFromMemory(bytes).Join();

But the problem is, it only works with 32bit executables, and the executable is 64bit. Any idea of how I could do it, any suggestions? Thank you

The error I'm getting:

System.BadImageFormatException: 'Bad IL format.'
Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
nexxus
  • 11
  • 5
  • I'm sure I've done something similar with 64-bit assemblies before. Your host program - is it compiled with x86, x64, or AnyCpu? Maybe try building explicitly as x64 and see if that helps? – Ozzah Jul 20 '21 at 05:14
  • 1
    _"it only works with 32bit processes, and the executable is 64bit"_ -- not clear what you mean. The code you posted works fine for x64. Please be more specific. Do note that the file you load would need to be a valid managed assembly; for .NET Core, this typically means the .dll that's compiled, as the .exe is just a bootstrapper that loads your code. – Peter Duniho Jul 20 '21 at 05:15
  • @nexxus - please show an error message. @Ozzah - I just tried the OP's code loading the default Console app in Visual Studio that simply prints "Hello World" as the assembly to load. Using targets AnyCPU, x86 and x64 all threw `System.BadImageFormatException: 'Bad IL format.'` Maybe there is some extra trick to generating an acceptable COFF exe? – dmedine Jul 20 '21 at 05:25
  • @dmedine: as I noted in my comment, if you compile a .NET Core program, you need to load the .dll that's generated, not the .exe. The .exe is just the thing that loads the managed assembly; it's not a managed assembly itself and cannot work with `Assembly.Load()`. Likewise the .exe if you use one of the single-file/self-contained compilation options. Only the "portable" option for .NET Core works, and even then, only loading the .exe works. – Peter Duniho Jul 20 '21 at 05:27
  • @PeterDuniho- I didn't see your comment. It did seem odd to me that one would load an exe in this manner. – dmedine Jul 20 '21 at 05:42
  • Question *as currently asked* is duplicate of many existing questions about running exe from memory. If you have very specific case (which possibly is "load .Net Core x64 exe as .Net assembly and start it") please [edit] question to clarify the restrictions (and show errors you are getting). Use @PeterDuniho comments/answer as guide of what you may be asking. – Alexei Levenkov Jul 20 '21 at 05:50
  • Edited my post, current error is the "Bad IL format" – nexxus Jul 20 '21 at 07:33

1 Answers1

2

But the problem is, it only works with 32bit processes, and the executable is 64bit. Any ideia of how I could do it, any suggestions?

It's not entirely clear what error you're getting, or what you mean by "only works with 32bit processes". However, I suspect that you are trying to load the wrong file.

A non-portable .NET Core .exe is not a valid managed assembly. If you want to be able to load your .NET Core program using Assembly.Load(), you must compile it as a portable assembly, and you must load the .dll file that is generated, not the .exe (the latter just being the little stub that knows how to run the .dll).

So, you need to:

  1. Make sure you are compiling/publishing myexecutable.csproj as a portable assembly, and
  2. Make sure you load myexecutable.dll, and not myexecutable.exe (i.e. var bytes = File.ReadAllBytes(@"C:\myexecutable.dll");)

Please see Build .NET Core console application to output an EXE for lots more details about the various ways you can compile a .NET Core program.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
  • Can I create an dll of the .exe inside my code or I would need to recompile? The executable its not mine, but I will look for the source – nexxus Jul 20 '21 at 07:36
  • 1
    _"Can I create an dll of the .exe inside my code"_ -- if it's not a proper IL assembly, nothing you can do will allow the code to be executed as part of your existing process. Is there some particular reason you won't/can't just run the .exe as an external process (i.e. with `System.Diagnostics.Process`)? – Peter Duniho Jul 20 '21 at 16:34