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.
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.