gpt4 book ai didi

c# - 在运行时动态加载 DLL

转载 作者:行者123 更新时间:2023-12-03 09:00:59 26 4
gpt4 key购买 nike

我正在开发一个支持两个通信接口(interface)的 C# 应用程序,每个接口(interface)都由自己的 DLL 支持。每个 DLL 都包含相同的函数名称,但它们的实现根据支持的接口(interface)略有不同。事实上,用户通常只会在他们的机器上安装一个 DLL,而不是两者都安装。旧接口(interface)的 DLL 是这样导入的:

[DllImport("myOldDll.dll", 
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
public static extern int MyFunc1( void );
public static extern int MyFunc2( void );
public static extern int MyFunc3( void );

这是尝试引入任一 DLL 的有效方法吗?
[DllImport("myOldDll.dll", 
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
[DllImport("myNewDll.dll",
CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall)]
public static extern int MyFunc1( void );
public static extern int MyFunc2( void );
public static extern int MyFunc3( void );

理想情况下,如果尝试加载第一个 DLL 失败,我认为检测丢失的 DLL 并加载第二个 DLL 会很好。有没有优雅的方法来做到这一点?

最佳答案

对“LoadLibrary”进行 P/Invoke 怎么样?

关于c# - 在运行时动态加载 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3680051/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com