问题描述
从.NET动态P / Invoke非托管代码的最佳方法是什么?
What is the best way to dynamically P/Invoke unmanaged code from .NET?
例如,我有一些非托管DLL,具有常见的C风格导出它们之间。我想通过一个DLL的路径,然后P / Invoke一个基于导出的名称的函数。在运行时,我不知道DLL名称。
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.
基本上,相当于 LoadLibrary
和 GetProcAddress
for .NET? (我有现有的代码,使用这些功能完成相同的目标,完全在非托管代码中)。
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).
推荐答案
GetProcAddress的类型安全的托管包装应该可以帮助您。
This article describes a typesafe managed wrapper for GetProcAddress that should help you out.
这篇关于动态地调用DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!