C# 无法加载 DLL(无法找到模块 HRESULT:0x8007007E)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9003072/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 05:32:05  来源:igfitidea点击:

Unable to load DLL (Module could not be found HRESULT: 0x8007007E)

c#c++dllpinvoke

提问by Ingimar Andresson

I have dll library with unmanaged C++ API code I need to use in my .NET 4.0 application. But every method i try to load my dll i get an error:

我有一个包含非托管 C++ API 代码的 dll 库,我需要在我的 .NET 4.0 应用程序中使用。但是我尝试加载 dll 的每种方法都会出现错误:

Unable to load DLL 'MyOwn.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)

无法加载 DLL 'MyOwn.dll':找不到指定的模块。(来自 HRESULT 的异常:0x8007007E)

I have read and tried severa solutions i have found on the internet. Nothing works..

我已经阅读并尝试了在互联网上找到的几种解决方案。什么都不行。。

I have tried using following methods:

我尝试使用以下方法:

[DllImport("MyOwn.dll",  CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs((UnmanagedType.I4))]
public static extern Int32 MyProIni(string DBname, string DBuser_pass,
    string WorkDirectory, ref StringBuilder ErrorMessage);

When I tried following this articleand when I run this example (from the downloaded code) it runs without a problem (the dll used is in the bin/debug folder)

当我尝试遵循本文并运行此示例(来自下载的代码)时,它运行没有问题(使用的 dll 位于 bin/debug 文件夹中)

I have copied my dll (along with all files the it depends on into my bin folder).

我已将我的 dll(连同它所依赖的所有文件一起复制到我的 bin 文件夹中)。

I also tried this approach but got the same error:

我也尝试过这种方法,但得到了同样的错误:

[DllImportAttribute(MyOwnLibDllPath, EntryPoint="TMproIni")]
[return: MarshalAs(UnmanagedType.I4)]
public static extern  int MyproIni(string DBname, string DBuser_pass, 
    string WorkDirectory, ref StringBuilder ErrorMessage);

Any suggestions?

有什么建议?

回答by Headpuster

Try to enter the full-path of the dll. If it doesn't work, try to copy the dll into the system32 folder.

尝试输入dll的完整路径。如果不行,尝试将dll复制到system32文件夹中。

回答by Felice Pollano

Ensure that all dependencies of your own dll are present near the dll, or in System32.

确保您自己的 dll 的所有依赖项都存在于 dll 附近或System32.

回答by display101

From what I remember on Windows the search order for a dll is:

根据我在 Windows 上的记忆,dll 的搜索顺序是:

  1. Current Directory
  2. System folder, C:\windows\system32 or c:\windows\SysWOW64(for 32-bit process on 64-bit box).
  3. Reading from the Pathenvironment variable
  1. 当前目录
  2. 系统文件夹,C:\windows\system32 or c:\windows\SysWOW64(用于 64 位机器上的 32 位进程)。
  3. Path环境变量中读取

In addition I'd check the dependencies of the DLL, the dependency walker provided with Visual Studio can help you out here, it can also be downloaded for free: http://www.dependencywalker.com

另外我会检查DLL的依赖关系,Visual Studio提供的dependency walker可以帮到你,也可以免费下载:http: //www.dependencywalker.com

回答by Chris O

Turn on the fusion logging, see this questionfor lots of advice on how to do that. Debugging mixed-mode apps loading problems can be a right royal pain. The fusion logging can be a big help.

打开融合日志记录,请参阅此问题以获取有关如何执行此操作的大量建议。调试混合模式应用程序加载问题可能是一个正确的皇家痛苦。融合日志可以是一个很大的帮助。

回答by Anthony Hayward

You can use the dumpbin tool to find out the required DLL dependencies:

您可以使用 dumpbin 工具找出所需的 DLL 依赖项:

dumpbin /DEPENDENTS my.dll

This will tell you which DLLs your DLL needs to load. Particularly look out for MSVCR*.dll. I have seen your error code occur when the correct Visual C++ Redistributable is not installed.

这将告诉您 DLL 需要加载哪些 DLL。特别注意 MSVCR*.dll。我已经看到您的错误代码在未安装正确的 Visual C++ Redistributable 时出现。

You can get the "Visual C++ Redistributable Packages for Visual Studio 2013" from the Microsoft website. It installs c:\windows\system32\MSVCR120.dll

您可以从 Microsoft 网站获取“Visual C++ Redistributable Packages for Visual Studio 2013”​​。它安装 c:\windows\system32\MSVCR120.dll

In the file name, 120 = 12.0 = Visual Studio 2013.

在文件名中,120 = 12.0 = Visual Studio 2013。

Be careful that you have the right Visual Studio version (10.0 = VS 10, 11 = VS 2012, 12.0 = VS 2013...) right architecture (x64 or x86) for your DLL's target platform, and also you need to be careful around debug builds. The debug build of a DLL depends on MSVCR120d.dll which is a debug version of the library, which is installed with Visual Studio but not by the Redistributable Package.

请注意您的 DLL 目标平台是否拥有正确的 Visual Studio 版本(10.0 = VS 10、11 = VS 2012、12.0 = VS 2013...)、正确的体系结构(x64 或 x86),并且您还需要小心调试构建。DLL 的调试版本取决于 MSVCR120d.dll,它是库的调试版本,它是随 Visual Studio 一起安装的,但不是由可再发行组件包安装的。

回答by Grantly

Make sure you set the Build Platform Target to x86 or x64 so that it is compatible with your DLL - which might be compiled for a 32 bit platform.

确保将 Build Platform Target 设置为 x86 或 x64,以便它与您的 DLL 兼容——它可能是为 32 位平台编译的。

回答by SharpC

If the DLL and the .NET projects are in the same solution and you want to compile and run both every time, you can right click the properties of the .NET project, Build events, then add something like the following to Post-build event command line:

如果DLL和.NET项目在同一个解决方案中,并且每次都想编译运行,可以右键单击.NET项目的属性,Build events,然后在Post-build event中添加如下内容命令行:

copy $(SolutionDir)Debug\MyOwn.dll .

It's basically a DOS line, and you can tweak based on where your DLL is being built to.

它基本上是一条 DOS 行,您可以根据 DLL 的构建位置进行调整。

回答by Eugenio Miró

I think your unmanaged library needs a manifest.
Hereis how to add it to your binary. and hereis why.

我认为您的非托管库需要一个清单。
是将它添加到二进制文件的方法。而这里是为什么。

In summary, several Redistributable library versions can be installed in your box but only one of them should satisfy your App, and it might not be the default, so you need to tell the system the version your library needs, that's why the manifest.

总而言之,您的盒子中可以安装多个可再发行库版本,但只有其中一个应该满足您的应用程序,并且它可能不是默认值,因此您需要告诉系统您的库需要的版本,这就是清单。

回答by kakopappa

I had the same problem when I deployed my application to test PC. The problem was development PC had msvcp110d.dlland msvcr110d.dllbut not the test PC.

当我将应用程序部署到测试 PC 时,我遇到了同样的问题。问题是开发 PC 有msvcp110d.dllmsvcr110d.dll而不是测试 PC。

I added "Visual Studio C++ 11.0 DebugCRT (x86)" merge module in InstalledSheild and it worked. Hope this will be helpful for someone else.

我在 InstalledSheild 中添加了“Visual Studio C++ 11.0 DebugCRT (x86)”合并模块并且它起作用了。希望这对其他人有帮助。

回答by Jeremy Thompson

The DLL has to be in the bin folder.

DLL 必须在 bin 文件夹中。

In Visual Studio, I add the dll to my project (NOT in References, but "Add existing file"). Then set the "Copy to Output Directory" Property for the dll to "Copy if newer".

在 Visual Studio 中,我将 dll 添加到我的项目中(不是在参考文献中,而是“添加现有文件”)。然后将 dll 的“复制到输出目录”属性设置为“如果更新则复制”。