tags:

views:

113

answers:

4

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

A: 

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.

Franci Penov
+1  A: 

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

Jeff Richter has examples

pm100
In which book? Amazon has "CLR Via C++/CLI" by Richter and Templeman.
mhambra
CLR via C#.....
pm100
page 524.......
pm100
still reading it: mostly offtopic, but few moments are related..
mhambra
+1  A: 

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

Pratik
+1  A: 

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: http://stackoverflow.com/questions/1321207/how-to-convert-a-simple-net-console-project-a-into-portable-exe-with-mono-and-mk.

Govert
Hmm. For example, I have a .net "hello world", and another .exe program, for example, - a hdd-killing virus. If that virus finds .NET installed, it just runs helloworld, if no - native virus code kills anything in 5000 meter range.. With a linker, I just get a third application, that has nothing in common with this idea - it may have it's own VM, whatsoever. But, will check the principle; thanks!
mhambra