1

同じ DllImport 呼び出しを使用して、32/64 ビットのネイティブ dll をロードしようとしています。

ディレクトリ構造:

根:

  • アプリケーション.exe
  • /win64/
    • stb_image.dll
  • /win32/
    • stb_image.dll

このソリューションを使用してみましたが、成功していないことがわかります。

たとえば、この関数呼び出し:

[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 を使用している方法:

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 を別のパスに設定しても機能します。

何か助けはありますか?

4

1 に答える 1

0

kernel32 の開発で問題が発生しました。ただし、現在のプロセス セッションの PATH 環境変数を設定することによる回避策があります。

string dllFolder = "<somewhere>";

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

フォルダーを PATH 変数に登録した後、LoadLibrary は問題なく動作し、指定されたパスから Dll をロードします。

于 2017-07-28T17:09:21.960 に答える