我试图使用相同的DllImport调用加载32/64位本地dll。
目录结构:
根:
我试过使用this solution,但正如你所看到的,没有成功。
例如,这个函数调用:
[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设置为不同的路径。
有什么帮助吗?
发布于 2017-07-28 17:09:21
kernel32开发出了一些问题。但是,通过为当前流程会话设置PATH环境变量有一个解决办法。
string dllFolder = "<somewhere>";
string path = Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", dllFolder + ";" + path);
在PATH变量中注册文件夹之后,LoadLibrary完美地工作,并从指定的路径加载Dll(s)。
发布于 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);
祝好运
https://stackoverflow.com/questions/36153156
复制相似问题