Using a .net compiled dll inside native c++

2019-02-16 01:36发布

问题:

as i understand that any .NET program gets compiled to MSIL which is fed to the CLR which compiles it to the assembly code and along with the help of JIT it executes it.

I was wondering, as .NET is a wrapper around the win32 api and the CLR ultimately converts the MSIL to assembly program. Isn't it possible for me to write some functionality in C#, make to a dll and then i use a tool which makes it a complete .net independent file for me to use inside unmanaged code like in C or C++.

Am i talking about Interops and COM? Isn't this idea different from it? My aim is to run a .NET dll on machine not having .NET framework.

回答1:

You can use the Native Image Generator (Ngen.exe) to compile a MSIL DLL to a native code DLL, but this will not allow you to run it on a system without the .NET Framework. You will still have references to other DLLs of the framework and even if you include these DLLs, it will not work on a system without the .NET framework, because the framework is more than just a collection of DLLs.



回答2:

This KB article explains a way to call managed methods from native code. However, you still need to have the .NET framework.



回答3:

This project will allow you to make unmanaged exports .NET of static methods.



回答4:

It's not a supported way and many features (like Reflection) rely on metadata provided at a higher level than raw machine code. There are some programs (called .NET linkers) that might help, but they are not 100% reliable.



回答5:

If you poke around on the web, I think there are a number of tools to 'compile' .NET assemblies/code to remove their need of the framework. Not sure how well they work....