首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >SetDllDirectory不工作( DllNotFoundException )

SetDllDirectory不工作( DllNotFoundException )
EN

Stack Overflow用户
提问于 2016-03-22 11:26:49
回答 2查看 2.3K关注 0票数 1

我试图使用相同的DllImport调用加载32/64位本地dll。

目录结构:

根:

  • application.exe
  • /win64 64/
    • stb_image.dll

  • /win32 32/
    • stb_image.dll

我试过使用this solution,但正如你所看到的,没有成功。

例如,这个函数调用:

代码语言:javascript
运行
复制
[DllImport("stb_image.dll")]
private static extern IntPtr stbi_load(string filename, ref int x, ref int y, ref int n, int req_comp);

但当我拿到DllNotFoundException时,它就不起作用了。

我使用SetDllDirectory的方式:

代码语言:javascript
运行
复制
static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
        SetUnmanagedDllDirectory();

        GameConfiguration config = new GameConfiguration();
        config.FPSTarget = 60;
        config.FixedFPS = true;
        config.Resizable = false;

        TestGame game = new TestGame(config);
        game.Run();
    }

    public static void SetUnmanagedDllDirectory()
    {
        string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        path = Path.Combine(path, IntPtr.Size == 8 ? "win64 " : "win32");
        if (!SetDllDirectory(path)) throw new System.ComponentModel.Win32Exception();
    }

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool SetDllDirectory(string path);
}

这是我程序中的第一个调用,所以它应该设置正确的路径。它还返回true。

但是,如果我将64位本机dll放在exe的目录中,那么它甚至可以工作,即使我将DllDirectory设置为不同的路径。

有什么帮助吗?

EN

回答 2

Stack Overflow用户

发布于 2017-07-28 17:09:21

kernel32开发出了一些问题。但是,通过为当前流程会话设置PATH环境变量有一个解决办法。

代码语言:javascript
运行
复制
string dllFolder = "<somewhere>";

string path = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", dllFolder + ";" + path);

在PATH变量中注册文件夹之后,LoadLibrary完美地工作,并从指定的路径加载Dll(s)。

票数 0
EN

Stack Overflow用户

发布于 2022-10-19 12:23:43

DllImport(“exeDirectory的SubDirectoryName/stb_image.dll")私有静态IntPtr stbi_load(string filename,ref int x,ref int y,ref int n,int req_comp);

祝好运

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36153156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档