0

I have a binary data from a c++ application that works. I want to run that specific binary code directly from my C# application.

I have this function that I got from internet:

 static void MemExe(byte[] buffer) { Assembly asm = Assembly.Load(buffer); if (asm.EntryPoint == null) throw new ApplicationException("No entry point found!"); MethodInfo ePoint = asm.EntryPoint; object ins = asm.CreateInstance(ePoint.Name); ePoint.Invoke(ins, null); } 

It it works well with C# binary code, but if I try to open a C++ binary code it crashes.

The exception right here:

Additional information: Could not load file or assembly '5154816 bytes loaded Test, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null' or one of its dependencies. It was made an attempt to load program with an incorrect format.

I guess it is something related to the header? I am no expert...

Is it possible to run c++ binary in the same way that the function MemEx runs C# binary?

Thanks.

8
  • 1
    The assembly.Load() method or its overloads cannot load unmanaged libraries (e.g. C/C++ dlls). You need to use C#'s interoperability functionalities (e.g. DllImportAttribtute). Google for C/C++ interoprability/pinvoke for further information. Commented Oct 8, 2016 at 17:53
  • This question may help. Commented Oct 8, 2016 at 17:56
  • @f.nasim Thanks. But in that question I think the user is trying to execute a function that is inside the dll itself. I don't want that. I have a full application written in C++ that I want to run, but directly from its binary code. Commented Oct 8, 2016 at 18:09
  • @f.nasim And also, I need to decrypt the C++ application before running it. Commented Oct 8, 2016 at 18:12
  • @MichalHainc But I don't want to create any file to run it. I want to run it from the memory or inject the code in other process. Commented Oct 8, 2016 at 19:07

1 Answer 1

0

Assembly.Load(...) will only load .NET binaries.

If you want to have access to the API of binary other than .NET, you would want to use P/Invoke signatures.

If you want to run an executable, use System.Diagnostics.Process.Start(...).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.