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 would like to take the path to a DLL and then P/Invoke a function based on the exported name. I would not know the DLL name until runtime.
Basically, what is the equivalent of LoadLibrary
and GetProcAddress
for .NET? (I have existing code which uses these functions to accomplish the same goal, entirely in unmanaged code).
P/Invoke is the technique a programmer can use to access functions in these libraries. Calls to functions within these libraries occur by declaring the signature of the unmanaged function within managed code, which serves as the actual function that can be called like any other managed method.
Dynamic replacement for PInvoke on Windows. DInvoke contains powerful primitives that may be combined intelligently to dynamically invoke unmanaged code from disk or from memory with careful precision.
Most of the P/Invoke API is contained in two namespaces: System and System. Runtime.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With