我正在使用多个具有相同名称的DLL,并且在使用dllimport时,我希望只有一个路径用于搜索DLL。
到目前为止我的代码如下:
'Trying to remove default search paths
<System.AttributeUsage(System.AttributeTargets.Assembly Or System.AttributeTargets.Method, AllowMultiple:=False)>
Public NotInheritable Class DefaultDllImportSearchPathsAttribute
Inherits Attribute
End Class
'Trying to add my own path
<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Public Shared Function SetDllDirectory(ByVal lpPathName As String) As Long
End Function
'Importing my DLL
<DllImport("A.dll", CallingConvention:=CallingConvention.StdCall)>
Public Shared Sub B(<MarshalAs(UnmanagedType.BStr)> ByRef X As String)
End Sub
Dim path As String = My.Computer.Registry.GetValue("HKEY_CURRENT_USER\Software\A\", "path", Nothing)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
SetDllDirectory(path)
End Sub
它一直在加载另一个DLL,并且永远不会到达在“路径”中搜索DLL的阶段。我怎么才能让它工作呢?
发布于 2019-07-06 08:18:19
如果您知道DLL的完整路径,则使用传递该完整路径的LoadLibrary
调用显式地加载它。如果在从DLL调用任何pinvoke函数之前执行此操作,则系统将使用您显式加载DLL。
https://stackoverflow.com/questions/56910989
复制相似问题