同じ 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 を別のパスに設定しても機能します。
何か助けはありますか?