Dynamically P/Invoking a DLL

后端 未结 4 742
夕颜
夕颜 2020-12-18 00:25

What is the best way to dynamically P/Invoke unmanaged code from .NET?

For example, I have a number of unmanaged DLL\'s with common C-style exports between them. I

相关标签:
4条回答
  • 2020-12-18 01:08

    One option you have is to create a native function which is responsible for loading the appropriate DLL and function into memory and then returning that function to managed code based on your path. This way you can use the GetProcAddress trick naturally and return the function point. You can then PInvoke into this function and get back a Delegate which will then invoke into the proper DLL.

    0 讨论(0)
  • 2020-12-18 01:09

    Also see this blog post from Jonathan Swift title Dynamically calling an unmanaged dll from .net

    0 讨论(0)
  • 2020-12-18 01:15

    This article describes a typesafe managed wrapper for GetProcAddress that should help you out.

    https://docs.microsoft.com/en-us/archive/blogs/jmstall/type-safe-managed-wrappers-for-kernel32getprocaddress

    0 讨论(0)
  • 2020-12-18 01:17

    You can do this by P/Invoking into LoadLibrary and GetProcAddress, and then using Marshal.GetDelegateForFunctionPointer.

    For details, see this article.

    0 讨论(0)
提交回复
热议问题