开发者

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

This question already has answers here: Is is possible to export functions from a C# DLL like in VS C++? (4 answers) Closed 6 years ago.

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


Have a look at Unmanaged Exports.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜