1

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

Kara
  • 6,115
  • 16
  • 50
  • 57
kagali-san
  • 2,964
  • 7
  • 48
  • 87
  • Document your question better, it is *very* unclear. – Hans Passant Sep 07 '10 at 18:04
  • I think more detail is needed. What I understand is that you have a program that is mostly native code but has some IL. You want to delay startup of the framework to increase startup speed of your native program, and think that you can do that by extracting the IL into its own assembly and dynamically invoking it. Is this correct? – KeithS Sep 07 '10 at 18:04
  • No. It is pure IL, and native code is just a reference to .NET loader. I want to extract IL, attach it to another - Win32 native - program as a Win32 PE segment and let .NET execute it, if "another program" can't find it's own framework DLLs. – kagali-san Sep 07 '10 at 18:06

4 Answers4

1

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

Jeff Richter has examples

pm100
  • 48,078
  • 23
  • 82
  • 145
1

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

softveda
  • 10,858
  • 6
  • 42
  • 50
1

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

Community
  • 1
  • 1
Govert
  • 16,387
  • 4
  • 60
  • 70
  • 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! – kagali-san Oct 10 '10 at 15:16
0

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
  • 74,861
  • 18
  • 132
  • 169