我正在使用多个具有相同名称的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的阶段。我该如何工作?
答案 0 :(得分:0)
如果您知道DLL的完整路径,则通过传递完整路径的对yarn test
的调用来显式加载它。如果在从该DLL调用任何pinvoke函数之前执行此操作,则系统将使用您显式加载的DLL。