views:

31

answers:

1

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", "foo");

and have it do something equivalent to:

[DllImport("someLibrary.dll")]
static extern int foo();

...

int result = foo();
+1  A: 

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.

spender
That is what I'm looking for, though I was hoping I could avoid Reflection.Emit.
Phil Lamb