如何根据平台加载Dll

发布于 2024-12-14 07:40:55 字数 532 浏览 3 评论 0原文

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

Public Function GetPlateform() As String

    Dim var1 As String
    If (IntPtr.Size = 8) Then
        var1 = hellox64
    Else
        var1 = hello
    End If
    Return var1
End Function

当表单加载时 这个var1被分配给var,最后。

Public Declare Function function1 Lib "var" (ByVal Id As Integer) As Integer

但是当我调试代码时,出现“DllNotFoundException”。 注意:dll 是用 vc++ 编写的。

I have a dll for 32bit and 64bit and now I want that my exe call the dll from according to solution platform,means when x64 is set then the dll of 64bit will call.For this I declare a function GetPlatform().

Public Function GetPlateform() As String

    Dim var1 As String
    If (IntPtr.Size = 8) Then
        var1 = hellox64
    Else
        var1 = hello
    End If
    Return var1
End Function

and when the form load
this var1 is assign to var and finally.

Public Declare Function function1 Lib "var" (ByVal Id As Integer) As Integer

But When I debug the code "DllNotFoundException" is ocuured.
NOTE:The dll is in vc++.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

離殇 2024-12-21 07:40:55

将本机 dll 存储到子文件夹中,并通过相应填写 库加载器 来提示 < code>PATH 进程环境变量,包含要加载的正确版本的路径。

例如,给定这个树布局......

Your_ assembly.dll
  |_NativeBinaries
      |_x86
          |_your_native.dll
      |_amd64
          |_your_native.dll

...并且此代码(抱歉,C#,没有 VB.Net :-/ )...

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);
}

...function1function2 将动态绑定到 32或本机代码的 64 位版本,具体取决于 IntPtr 的大小(Scott Hanselman 或这个 StackOverflow问题 )。

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

注2:这已经在LibGit2Sharp

Store your native dlls into subfolders and hint the Library Loader by filling accordingly the PATH process environment variable with the path to the correct version to load.

For instance, given this tree layout...

Your_assembly.dll
  |_NativeBinaries
      |_x86
          |_your_native.dll
      |_amd64
          |_your_native.dll

...and this code (sorry, C#, no VB.Net :-/ )...

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);
}

...function1 and function2 would be dynamically bound to either the 32 or 64 bits version of the native code, depending on the size of an IntPtr (more on this in this post from Scott Hanselman or this StackOverflow question).

Note 1: This solution is especially useful when both versions of the dll bear the same name or if you're not willing to duplicate every extern references.

Note 2: This has already been successfully implemented in LibGit2Sharp.

一梦等七年七年为一梦 2024-12-21 07:40:55

不可以,您不能在 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)。

No, you cannot dynamically create a reference to the DLL in a lib statement. However, you may (disclaimer: have not tried) be able to create two references and call the appropriate one in your code.

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

You will then need to branch on the platform and call the appropriate alias function name (Function132 or Function164) depending on the platform.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文