Exporting a native C function from a .net DLL? [duplicate]

久未见 提交于 2020-01-09 07:52:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!