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
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.
Also see this blog post from Jonathan Swift title Dynamically calling an unmanaged dll from .net
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
You can do this by P/Invoking into LoadLibrary and GetProcAddress, and then using Marshal.GetDelegateForFunctionPointer.
For details, see this article.