C# 加载 64 位 dll 时出现 HRESULT 0x8007007E 的 DllNotFoundException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10774250/
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
DllNotFoundException with HRESULT 0x8007007E when loading 64-bit dll
提问by GSerg
I downloaded zliband compiled the library as both Windows 32-bit and Windows 64-bit dll. I now have zlibwapi.dlland zlibwapi64.dll.
我下载了zlib并将库编译为 Windows 32 位和 Windows 64 位 dll。我现在有zlibwapi.dll和zlibwapi64.dll。
The dlls are copied into my application folder and are referenced as follows:
这些 dll 被复制到我的应用程序文件夹中,并按如下方式引用:
[DllImport(@"zlibwapi.dll", EntryPoint = "uncompress", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = false)]
private static extern int uncompress32(
IntPtr dest,
ref uint destLen,
[In(), MarshalAs(UnmanagedType.LPArray)] byte[] source,
uint sourceLen
);
[DllImport(@"zlibwapi64.dll", EntryPoint = "uncompress", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, ExactSpelling = true, SetLastError = false)]
private static extern int uncompress64(
IntPtr dest,
ref uint destLen,
[In(), MarshalAs(UnmanagedType.LPArray)] byte[] source,
uint sourceLen
);
At runtime I check whether I'm 32-bit or 64-bit, and call appropriate version.
在运行时,我检查我是 32 位还是 64 位,并调用适当的版本。
This works fine if I'm 32-bit, but the 64-bit version gives
如果我是 32 位,这工作正常,但 64 位版本给出
Cannot load DLL "zlibwapi64.dll": Module not found. (HRESULT exception: 0x8007007E)
无法加载 DLL“zlibwapi64.dll”:找不到模块。(HRESULT 异常:0x8007007E)
I've found many similar questions on the Internet, and the suggested reason was that the library depends on some other libraries, and it is those libraries that may not be found.
This does not seem to the case:
我在网上发现了很多类似的问题,建议的原因是该库依赖于其他一些库,而这些库可能找不到。
情况似乎并非如此:
- zlibwapi64.dll only depends on Kernel32.dll and MSVCR90.dll. I do have VS2008 C++ runtimes installed, both 32 and 64 bit.
- When I try to load zlibwapi64.dll from a non-managed C++ application, it loads no problem. It is C# that fails to load it.
- zlibwapi64.dll 只依赖于 Kernel32.dll 和 MSVCR90.dll。我确实安装了 32 位和 64 位的 VS2008 C++ 运行时。
- 当我尝试从非托管 C++ 应用程序加载 zlibwapi64.dll 时,它加载没有问题。是 C# 无法加载它。
I have tried setting absolute path to the 64-bit dll, it does not help.
我试过设置 64 位 dll 的绝对路径,它没有帮助。
How do I make it work?
我如何使它工作?
采纳答案by Hans Passant
It is a fairly basic "file not found" kind of error, unfortunately it doesn't tell you explicitly what DLL it couldn't find. You already know about the issue with dependent DLLs. Note that you can avoid the irksome dependency on msvcr90.dll by compiling the code with /MT
这是一个相当基本的“找不到文件”类型的错误,不幸的是它没有明确告诉你它找不到什么 DLL。您已经了解相关 DLL 的问题。请注意,您可以通过使用 /MT 编译代码来避免对 msvcr90.dll 的令人讨厌的依赖
You'll need to debug the problem and that requires getting insight in where it is looking for DLLs. One good tool is SysInternals' ProcMon utility, it shows you exactly where your program is looking for files. You should see it probing for the DLL, searching through the directories of the PATH and failing to find the file.
您需要调试问题,这需要深入了解它在哪里寻找 DLL。一个很好的工具是SysInternals 的 ProcMon 实用程序,它可以准确地显示您的程序正在寻找文件的位置。您应该会看到它在探测 DLL,搜索 PATH 的目录,但未能找到该文件。
Unfortunately ProcMon is a bit chatty and has a habit of drowning you in the data. A more dedicated tool is GFlags.exe, a tool available from the Debugging Tools for Windows package. These days included with the Windows SDK. Stored in c:\program files (x86)\debugging tools for windows\gflags.exe after you installed it. You can turn on the "Show loader snaps" option. On later Windows versions, that tells the Windows loader to generate debugging messages when it is searching for DLLs. They'll appear in the Output window when you enable unmanaged debugging.
不幸的是,ProcMon 有点健谈,并且有让您淹没在数据中的习惯。一个更专用的工具是 GFlags.exe,该工具可从 Windows 调试工具包中获得。这些天包含在 Windows SDK 中。安装后存储在 c:\program files (x86)\debugging tools for windows\gflags.exe 中。您可以打开“显示加载程序快照”选项。在更高的 Windows 版本中,这会告诉 Windows 加载程序在搜索 DLL 时生成调试消息。当您启用非托管调试时,它们将出现在“输出”窗口中。
Try ProcMon first, much easier to get going.
首先尝试 ProcMon,更容易上手。
And of course consider pure managed solutions so you are not fighting these kind of install problems. Good ones are DotNetZip and SharpZipLib, take their first google hit.
当然,请考虑纯托管解决方案,这样您就不会遇到此类安装问题。好的是 DotNetZip 和 SharpZipLib,这是他们的第一个谷歌搜索。
回答by Daniel Williams
Another good tool for examining Dll dependencies is Dependency walker (depends). It looks at the file in a static way so a little easier than using process monitor.
另一个检查 Dll 依赖关系的好工具是 Dependency walker (depends)。它以静态方式查看文件,因此比使用进程监视器更容易一些。

