在 .NET 中指定 DllImport 的搜索路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2864673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specify the search path for DllImport in .NET
提问by Stefan
Is there a way to specify the paths to be searched for a given assembly that is imported with DllImport?
有没有办法为使用 DllImport 导入的给定程序集指定要搜索的路径?
[DllImport("MyDll.dll")]
static extern void Func();
This will search for the dll in the app dir and in the PATH environment variable. But at times the dll will be placed elsewhere. Can this information be specified in app.config or manifest file to avoid dynamic loading and dynamic invocation?
这将在应用程序目录和 PATH 环境变量中搜索 dll。但有时 dll 会被放置在其他地方。是否可以在 app.config 或 manifest 文件中指定此信息以避免动态加载和动态调用?
回答by Chris Schmich
Call SetDllDirectorywith your additional DLL paths before you call into the imported function for the first time.
打电话SetDllDirectory与你额外的DLL路径,你打电话到首次导入功能之前。
P/Invoke signature:
P/调用签名:
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool SetDllDirectory(string lpPathName);
To set more than one additional DLL search path, modify the PATHenvironment variable, e.g.:
要设置多个额外的 DLL 搜索路径,请修改PATH环境变量,例如:
static void AddEnvironmentPaths(string[] paths)
{
string path = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
path += ";" + string.Join(";", paths);
Environment.SetEnvironmentVariable("PATH", path);
}
There's more info about the DLL search order here on MSDN.
MSDN 上有关于 DLL 搜索顺序的更多信息。
Updated2013/07/30:
2013/07/30更新:
Updated version of the above using Path.PathSeparator:
上面使用的更新版本Path.PathSeparator:
static void AddEnvironmentPaths(IEnumerable<string> paths)
{
var path = new[] { Environment.GetEnvironmentVariable("PATH") ?? string.Empty };
string newPath = string.Join(Path.PathSeparator.ToString(), path.Concat(paths));
Environment.SetEnvironmentVariable("PATH", newPath);
}
回答by jvrdev
Try calling AddDllDirectorywith your additional DLL paths before you call into the imported function for the first time.
AddDllDirectory在首次调用导入的函数之前,尝试使用其他 DLL 路径进行调用。
If your Windows version is lower than 8 you will need to install this patch, which extends the API with the missing AddDllDirectoryfunction for Windows 7, 2008 R2, 2008 and Vista (there is no patch for XP, though).
如果您的 Windows 版本低于 8,您将需要安装此补丁,它扩展了AddDllDirectoryWindows 7、2008 R2、2008 和 Vista缺少的 API (不过 XP 没有补丁)。
回答by Eric
This might be useful DefaultDllImportSearchPathsAttribute Class
E.g.
这可能很有用DefaultDllImportSearchPathsAttribute 类
例如
[assembly: DefaultDllImportSearchPaths(DllImportSearchPath.UserDirectories)]
Also note you can use AddDllDirectoryas well so you aren't screwing up anything already there:
另请注意,您也可以使用AddDllDirectory,这样您就不会搞砸已经存在的任何内容:
[DllImport("kernel32.dll", SetLastError = true)]
static extern bool AddDllDirectory(string path);

