How to provide custom implementation for extern methods in .NET?

半世苍凉 提交于 2019-12-11 09:06:37

问题


In C# the extern modifier is used to declare a method that is implemented externally. Usually it is used with DllImport attribute to call some function in unmanaged code.

I wonder if there is a way to provide custom implementation of extern method?

To understand better the problem consider the following use case. I have a set of functions implemented in unmanaged code and I'd like to supply pointers to these function in run-time (during the loading of assembly).

The same thing DllImport attribute does, but I'd like to provide pointers by myself.


回答1:


This is possible although you would need to PInvoke several things (LoadLibrary and GetProcAddress - see links below)... it is called "late binding native code"...

Some relevant links with source:

  • http://blogs.msdn.com/b/junfeng/archive/2004/07/14/181932.aspx
  • http://www.codeproject.com/KB/cs/dyninvok.aspx
  • http://pinvoke.net/default.aspx/kernel32/GetPRocAddress.html
  • http://www.pinvoke.net/default.aspx/kernel32.loadlibrary



回答2:


You would need to use LoadLibrary and GetProcAddress



来源:https://stackoverflow.com/questions/7245507/how-to-provide-custom-implementation-for-extern-methods-in-net

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