Vb.net 如何根据平台加载Dll

Vb.net 如何根据平台加载Dll,vb.net,visual-c++,dll,dllnotfoundexception,Vb.net,Visual C++,Dll,Dllnotfoundexception,我有一个32位和64位的dll,现在我希望我的exe根据解决方案平台调用dll,这意味着当设置x64时,64位的dll将被调用。为此,我声明了一个函数GetPlatform() 当表单加载时 这个var1被分配给var和finally Public Declare Function function1 Lib "var" (ByVal Id As Integer) As Integer 但当我调试代码时,会记录“DllNotFoundException”。 注意:dll在vc++中。否,您不能

我有一个32位和64位的dll,现在我希望我的exe根据解决方案平台调用dll,这意味着当设置x64时,64位的dll将被调用。为此,我声明了一个函数GetPlatform()

当表单加载时 这个var1被分配给var和finally

Public Declare Function function1 Lib "var" (ByVal Id As Integer) As Integer
但当我调试代码时,会记录“DllNotFoundException”。
注意:dll在vc++中。

否,您不能在lib语句中动态创建对dll的引用。但是,您可能(免责声明:尚未尝试)能够创建两个引用并在代码中调用相应的引用

Public Declare Function Function132 Lib "My32BitLib.DLL" Alias "function1" (ByVal Id As Integer) As Integer

Public Declare Function Function164 Lib "My64BitLib.DLL" Alias "function1" (ByVal Id As Integer) As Integer

然后,您需要在平台上进行分支,并根据平台调用相应的别名函数名(Function132或Function164)。

将本机DLL存储到子文件夹中,并通过相应地用要加载的正确版本的路径填充
PATH
流程环境变量来提示

例如,给定此树布局

…这段代码(对不起,C#,没有VB.Net:-/)

function1
function2
将动态绑定到32位或64位版本的本机代码,具体取决于IntPtr的大小(本文将从或此对此进行详细介绍)

注意1:当dll的两个版本具有相同的名称或您不愿意复制每个外部引用时,此解决方案特别有用


注意2:这已经在

中成功实现,只需使用一个名称安装正确的DLL!!!!创建2个安装程序包,但@Martin是否可行。交换函数名和别名。您需要
声明函数function164。。。别名“Function1”
Public Declare Function Function132 Lib "My32BitLib.DLL" Alias "function1" (ByVal Id As Integer) As Integer

Public Declare Function Function164 Lib "My64BitLib.DLL" Alias "function1" (ByVal Id As Integer) As Integer
Your_assembly.dll
  |_NativeBinaries
      |_x86
          |_your_native.dll
      |_amd64
          |_your_native.dll
internal static class NativeMethods
{
    private const string nativeName = "your_native";

    static NativeMethods()
    {
        string originalAssemblypath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;

        string currentArchSubPath = "NativeBinaries/x86";

        // Is this a 64 bits process?
        if (IntPtr.Size == 8)
        {
            currentArchSubPath = "NativeBinaries/amd64";
        }

        string path = Path.Combine(Path.GetDirectoryName(originalAssemblypath), currentArchSubPath);

        const string pathEnvVariable = "PATH";
        Environment.SetEnvironmentVariable(pathEnvVariable,
            String.Format("{0}{1}{2}", path, Path.PathSeparator, Environment.GetEnvironmentVariable(pathEnvVariable)));
    }

    [DllImport(nativeName)]
    public static extern int function1(int param);

    [DllImport(nativeName)]
    public static extern int function2(int param);
}