问题
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
回答1:
Use this tool.
回答2:
Write a .Net library in Managed C++ and there you can export a "Native" method/function.
回答3:
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.
回答4:
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.
来源:https://stackoverflow.com/questions/5993719/exporting-a-native-c-function-from-a-net-dll