Question

This question already has an answer here:

I have a .net assembly written in C#, and I'm looking to export a native C function from the assembly.

I have an application which will perform a 'LoadLibrary()' on any DLL's found in a 'plugin' folder. After loading the DLL, the application performs a 'GetProcAddress' looking for a function named 'Register'. The application expects this function to follow the C calling convention.

How can I export a function named 'Register' from my .net assembly, so I can successfully hookup with the plugin system for this application?

Thanks, Andrew

Was it helpful?

Solution

Use this tool.

OTHER TIPS

Write a .Net library in Managed C++ and there you can export a "Native" method/function.

Sadly Microsoft does not support this feature, and you have to change the msil after the build to expose those methods. It is possible as one guy has shown a reasonable solution on codeproject or here, but it requires a post build step and you are on your own after that. I don't know if this hack will work on .net 4.0 or later though. Hopefully Microsoft will listen to us and support this simple feature in C# since the CLR support is already there.

What you want is a Reverse P/Invoke. You can't actually embed a C function in a C# dll, if by that you mean a function actually implemented in C, but by following the tutorials given on the linked page you can create a DLL export that's callable by unmanaged C/C++ code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top