Can .NET VM (mscorlib) be called from native Win32 program to execute IL/other internal compiled code format?

StackOverflow https://stackoverflow.com/questions/3661265

문제

Old question: I have an .exe (PE) with IL (.NET) code in it. When it is started, a mscorlib.dll (.NET framework) function is called to start IL code. Can I extract .NET code segment and append it to other program (that calls mscorlib.dll to execute that segment)?

New question:

I want to append the compiled code from a C# .NET program to a native, pure Win32 application that can run without any .NET Runtimes, and execute it by dynamically calling mscorlib.dll's functions, if .NET is present. This is like a 'executable joiner' technique, not a native compilation.

I do not want to write some .NET .exe to a temp directory and execute it; it is already done and now want to get rid of temp folder by calling .NET vm directly on .NET code inside my file (by giving out an offset of found structure/session, or just by assigning correct PE .section name - know just a little about PortableExecutable format).

Reference: http://webcache.googleusercontent.com/search?q=cache:u9tTX2sfkhAJ:social.msdn.microsoft.com/Forums/en/clr/thread/2677c0b6-6b3c-4a51-9fcc-c2d8838c7b8b+compiled+IL

도움이 되었습니까?

해결책

Perhaps you are looking for a linker that will take your .NET library and referenced assemblies, and create an executable or .dll that can run without requiring .NET to be installed.

Two commercial options for this are

It should also be possible with mono. It has utility called mkbundle which precompiles and add dependencies, but you would need Cygwin and a bit more expertise to get it to work. This question seems like a good start: How to convert a simple .Net console project a into portable exe with Mono and mkbundle?.

다른 팁

you can host the CLR runtime in a C++ app and then call methods in the hosted environment

Jeff Richter has examples

You can host the CLR inside a native process using a COM API provided with the Windows SDK. IIS, SQL Server and few other products do exactly that. In fact you can write your own replacement for ASP.Net that handles requests from IIS in theory. I have no experience to share though. Check the links below:

http://msdn.microsoft.com/en-us/magazine/cc163567.aspx
http://msdn.microsoft.com/en-us/library/9x0wh2z3%28VS.90%29.aspx

While VS does not explicitly support this, .Net allows you to reference an .exe assembly jst the same way as you would a .dll assembly, and call public classes.

Of course, if the code you need to call is private or internal, you might have to resort to reflection to get it to work.

If you actually need to move the code into your .dll, you could try ILMerge to create one binary. However, I have not tried using ILMerge with an assembly referencing another .exe assembly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top