Call native code specified at runtime
I'm developing an application that will allow users to call external code from both managed and native .dlls. The users will be able to specify what library/method/function to call at runtime (it will be stored in a configuration file).
I know how to do this using pinvoke for native libraries if I know what dll/function I want to call at compile time, but I can't find any information on how to do this at runtime.
Essentially what I'd like to do is call a method:
int result = ExecuteNativeFunction("someLibrary.dll", "fo开发者_JS百科o");
and have it do something equivalent to:
[DllImport("someLibrary.dll")]
static extern int foo();
...
int result = foo();
Would this be what you are looking for? Using System.Reflection.Emit, you can dynamically compile code that defines a new PInvoke method. See the class DllRegServer
in the linked file for details.
精彩评论