C# 加载动态库并在库中使用函数
在C#中,我使用外部dll,使用loadLibrary,如下所示:C# 加载动态库并在库中使用函数,c#,dynamic-library,C#,Dynamic Library,在C#中,我使用外部dll,使用loadLibrary,如下所示: public class Utilities [DllImport("kernel32", CharSet= CharSet.Auto, SetLastError=true)] private static extern IntPtr LoadLibrary(string librayName); [DllImport("kernel32", CharSet= CharSet.Auto, SetLastE
public class Utilities
[DllImport("kernel32", CharSet= CharSet.Auto, SetLastError=true)]
private static extern IntPtr LoadLibrary(string librayName);
[DllImport("kernel32", CharSet= CharSet.Auto, SetLastError=true)]
private static extern IntPtr GetProcAddress(intPtr hwnd, string procedureName);
public static LoadAssembliesAndMethods() {
string mainPath = AppDomain.CurrentDomain.BaseDirectory;
string path = Path.Combine(mainPath, "MyAssembly.dll");
IntPtr ptr = LoadLibrary(path);
// What to do next in order to get all the list of functions/methods/class in the library and use them?
}
dll没有程序集的签名(它是第三方),所以我不能这样做
Assebly.LoadFile(path);
我需要获取dll的所有函数/方法/类,并使用C#使用其中的一些函数/方法/类
如何才能做到这一点。没有通用的方法以编程方式获取非托管库的导出列表
如果要列出非托管dll中的所有方法,请参阅以获取更多信息。此链接可帮助您: 更新:
IntPtr funcaddr = GetProcAddress(Handle,functionName);
YourFunctionDelegate function =
Marshal.GetDelegateForFunctionPointer(funcaddr,typeof(YourFunctionDelegate ))
as YourFunctionDelegate ;
function.Invoke(pass here your parameters);
您需要在编码时创建委托,或者使用DynamicModule在运行时创建委托要加载哪种类型的dll库?
LoadLibrary
然后GetProcAddress(HLLibrary,functionName)
返回地址(IntPtr
)对于所需函数,我不知道所需函数是在静态类中还是仅在函数中。我没有第三方库的来源。我需要检查类列表和方法列表,然后使用它们。谢谢。这就是帮助。我看到函数“AFunc”-如何调用该函数?谢谢。@如果要检查更新,请在运行时使用Delegate.CreateDelegate创建委托,执行以下操作后:IntPtr ptr=LoadLibrary(path)(作为我的代码);&IntPtr funcaddr=GetProcAddress(句柄,函数名);我得到funcaddr=0x00000000,但不是一个有效的地址(即使我看到函数名在列表中(如您提供的链接所示:)。确定。发现错误:正如我在上所看到的,我看到我需要更改GetProcAddress的修饰符:CharSet=CharSet.AnsiAlso,您能给我一个示例:GetDelegateForFunctionPointer(IntPtr)