Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically P/Invoking a DLL

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).

like image 361
Nick Avatar asked Jul 29 '09 20:07

Nick


People also ask

How does PInvoke work?

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.

What is D invoke?

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.

What overarching namespace provides PInvoke to net?

Most of the P/Invoke API is contained in two namespaces: System and System. Runtime.


1 Answers

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

like image 190
Michael A. McCloskey Avatar answered Oct 05 '22 00:10

Michael A. McCloskey