我有两个dll使用相同的API访问不同的硬件设备(构建为我)。我想根据计算机中检测到的硬件在运行时选择其中一个。
我发现在调用DLL中的任何函数之前,我可以使用windows函数LoadLibrary
加载两个库中的一个,而VB将使用加载的库 - 但这仅在文件名匹配时才有效函数Declare
(或Dllimport
)中的内容,即两个dll版本必须具有相同的文件名。这意味着dll不能位于同一目录中(例如在System32目录中)。
我可以有两个带有两个不同文件名的dll,它们在运行时为VB.NET提供相同的API吗?
答案 0 :(得分:0)
除了compilation constants之外,您无法真正进行条件导入。但是,您可以导入两个dll并根据您的条件创建一个调用其中一个的方法。
类似的东西:
<DllImport("firstversion.dll", EntryPoint:="GetDevice")> _
Public Shared Function GetDevice_v1(ByVal arg1 As IntPtr, ByVal arg2 As String) As IntPtr
End Function
<DllImport("secondversion.dll", EntryPoint:="GetDevice")> _
Public Shared Function GetDevice_v2(ByVal arg1 As IntPtr, ByVal arg2 As String) As IntPtr
End Function
Public Shared Function GetDevice(ByVal arg1 As IntPtr, ByVal arg2 As String) As IntPtr
If condition Then
Return GetDevice_v1(arg1, arg2)
Else
Return GetDevice_v2(arg1, arg2)
End If
End Function