在运行时动态加载 DLL
我正在开发一个支持两个通信接口的 C# 应用程序,每个接口都有自己的 DLL 支持。每个 DLL 都包含相同的函数名称,但它们的实现根据支持的接口而略有不同。事实上,用户通常只会在其计算机上安装一个 DLL,而不是同时安装两者。旧接口的 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 );
Would this be a valid way to try to import in another 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。有没有一种优雅的方式来做到这一点?
I'm working on a C# application that supports two communications interfaces, each supported by its own DLL. Each DLL contains the same function names, but their implementation varies slightly depending on the supported interface. As it is, users will typically have only one DLL installed on their machine, not both. The DLL for the old interface is imported like this:
[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 );
Would this be a valid way to attempt to bring in either 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 );
Ideally, I suppose it would be nice to detect a missing DLL and load the second DLL if the attempt to load the first fails. Is there a graceful way to do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对“LoadLibrary”执行 P/Invoke 怎么样?
How about doing a P/Invoke to `LoadLibrary'?
在.NET 1.1中,您需要创建一个代理非托管DLL(用C或Delphi或...编写)并调用它的方法,并且非托管DLL将完成其余的工作。在 .NET 2.0 及更高版本中,您可以使用 Assembly.LoadFile() 及更多。不像您尝试使用的声明那么优雅,并且需要大量编码。因此,如果可能的话,我建议采用代理方式。
In .NET 1.1 you would need to create a proxy unmanaged DLL (write it in C or Delphi or ... ) and call it's methods, and that unmanaged DLL would do the rest. In .NET 2.0 and later you use Assembly.LoadFile() and further. Not as elegant as just declarations you attempted to use, and requires quite a lot of coding. So I'd suggest a proxy way if possible.
也许您应该为从任一 DLL 导入的方法指定不同的名称,然后在程序中使用一个委托来指向其中一个或另一个(以合适的为准),并且只调用该委托。
Perhaps you should give the methods imported from either DLL different names, and then have a delegate in your program that you point to one or the other (whichever is appropriate), and only call the delegate.
听起来您最好重新设计模块化插件风格的界面。
网络上有十亿个半这样的例子,像这样一个。简而言之,您可以在 DLL 目录上使用 LoadAssembly(),然后转换回公共基本接口。
It sounds like you would be best served re-architecting to a modular plugin style interface.
There are a billion and a half examples of this on the web, like this one. In a nutshell though, you use LoadAssembly() on a directory of DLLs, then cast back to your common base interface.
我想我找到了一个可行的解决方案:
C# 检查文件目标是否有效
谢谢大家的意见!
I think I found a workable solution:
C# check that a file destination is valid
Thanks, everyone, for your input!