The most usual technique to make a functionality of .NET Assembly accessible to native (unmanaged executables) is using COM. Relevant references can be found from the Answer by JF2015.
There are certain problems with COM. The technology is gradually becoming obsolete. Main problem is overhead. In particular, normally it requires registration of a component in the system registry -- one of the major sources of registry contamination. .NET and Microsoft are trying to go away from such things in favor of local approach to component dependency and versioning. Also, COM approach will hardly be available for Unix (with Mono or other product).
In view of all that, a direct export from managed assembly to the unmanaged executable would be quite attractive. But how to do this?
Officially, it is said that an assembly cannot export an umnanaged method, so .NET Assembly cannot be used by a native (unmanaged) application (MFC or something).
This is a subtle lie. If you look at the standard IL,
there is a way to export a method as unmanaged (native). Indeed, it cannot be done with C# along. So, what to do? Right!
The trick is to do several additional build steps to automatically do the trick. These steps can be nicely embedded into the project (see Microsoft documentation on MSBuild -- very comprehensive). Here are the steps:
1) After .NET Assembly is compiled, disassemble into IL code;
2) Open IL file and transform the text to mark the unmanaged entry points;
3) Re-compile modified code back to executable assembly; now it exports few unmanaged method.
This needs good knowledge of IL (but no programming skills). Note, that when the tool is already available, IL programming is not required at all.
We need another trick: how to mark what methods to export as unmanaged. Naturally, those should be some static functions using common data types for parameters. But usually we need to export not all of them, only a part.
For this purpose, we can create a special attribute and mark an assembly with this attribute:
[assembly:ExportToManaged(typeof(MyClass1))]
[assembly:ExportToManaged(typeof(MyClass2))]
This will mean that all static functions of the classes
MyClass1
and
MyClass2
should be exported as unmanaged. Alternatively (or additionally), separate methods can be marked by the attribute (in this case, only those methods will be exported as unmanaged -- even better).
The text-processing utility working with the disassembled IL code should perform text search to find the attribute and perform the modification to the specified methods.
These are the references to the original articles:
"Unmanaged code can wrap managed methods" by Emilio Reale, 29 Aug 2004.
"How to Automate Exporting .NET Function to Unmanaged Programs" by Selvin, 22 Nov 2006.
As it often happens to a decent serious publication, these CodeProject articles collected not very good votes (I suggest everyone who is interested supports the authors).
New work found in a reader's Message:
"C# Project Template for Unmanaged Exports" by Robert Giesecke, Jan 11, 2010.
100% credit to these authors.
I tried to use only slightly different approach (so it would be purely derivative work), but my code (circa 2006) is not operational yet. Later on, I lost interest to this topic due to deeper focus on managed platforms I developed. If I ever find time to put my minor improvements, and if still there is interest, I could post it as a derivative work -- not sure now though.