Search code examples
c#c++.netunmanaged

Incorporate an unmanaged EXE as resource in a managed C# code


I have a given code written in unmanaged C++ that I added as a resource in a C# program.

When trying to load the unmanaged EXE using Assembly.Load, I get a BadImageFormatException whose InnerException says that "the IL Format is incorrect" (obviously).

How can I make this work?

I know that using the code as a DLL would be better, but for some reason, the dll call does not behave the same way as when I run the unmanaged exe with System.Diagnostics.Process.Start.

And I don't want to have two separates EXE files. Any idea?


EDIT: OK, I no longer need to do that. My dll behaved a different way because I was using it in a different context (winform instead of console); therefore the function causing the problem, SetThreadDesktop, couldn't work as expected. Gonna close post now, thanks for all those who answered.


Solution

  • You can't load an unmanaged exe/dll with Assembly.Load(), it works only with managed ones. if you need to PInvoke on that unmanaged file, and the reason you want a single exe is just deploy, you can pack it as an embedded resource (as you currently did ) and unpack on the target machine in a file, and use it as usual.