调用运行时指定的本机代码
我正在开发一个应用程序,允许用户从托管和本机 .dll 调用外部代码。用户将能够指定在运行时调用什么库/方法/函数(它将存储在配置文件中)。
如果我知道我想在编译时调用什么 dll/函数,我知道如何使用 pinvoke 来执行此操作,但我找不到有关如何在运行时执行此操作的任何信息。
本质上我想做的是调用一个方法:
int result = ExecuteNativeFunction("someLibrary.dll", "foo");
并让它做相当于以下的事情:
[DllImport("someLibrary.dll")]
static extern int foo();
...
int result = foo();
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();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会是您正在寻找的吗?使用 System.Reflection.Emit,您可以动态地编译定义新 PInvoke 方法的代码。请参阅链接文件DllRegServer类> 了解详情。
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.