无法在 C# 中加载 DLL

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

Unable to load DLL in C#

c#dlldllimport

提问by john

how to load a dll in a c# project

如何在ac#项目中加载dll

error:

错误

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

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

code sample:

代码示例

[DllImport("Reader.dll")]
 public static extern byte OpenReader(ref IntPtr hCom, byte LinkType, string com_port);

image: exception screenshot

图片: 异常截图

采纳答案by john

Although the reader.dll is unable to load GPSVC.dll and IESHIMS.DLL. i managed to make it work by running the corflags command on application.exe the application is now marked as 32bit:

虽然 reader.dll 无法加载 GPSVC.dll 和 IESHIMS.DLL。我设法通过在 application.exe 上运行 corflags 命令使其工作,该应用程序现在被标记为 32 位



corflags application.exe /32bit+


Version : v4.0.30319

版本:v4.0.30319

CLR Header: 2.5

CLR 标头:2.5

PE : PE32

聚乙烯:PE32

CorFlags : 3

CorFlags : 3

ILONLY : 1

国际:1

32BIT : 1

32位:1

Signed : 0

签名:0

回答by Peter C

If it's a simple C DLL it just needs to be in the same folder as the .exe.

如果它是一个简单的 C DLL,它只需要与 .exe 位于同一文件夹中。

回答by Will Dean

If the problem is really "cannot be found", then using ProcMon from Sysinternals will show you where the system is looking for the DLL.

如果问题确实是“无法找到”,那么使用 Sysinternals 的 ProcMon 将显示系统正在寻找 DLL 的位置。

However, often these sort of exceptions mean 'I found the DLL but I can't load it', and that can be because a dependency of the DLL is missing rather than the DLL itself, or because the DLL is incompatible with the app trying to load it. If your C# app is set for 'Any CPU' and you're on a 64bit machine, you'll get this sort of error loading unmanaged 32-bit DLLs.

但是,这些异常通常意味着“我找到了 DLL 但我无法加载它”,这可能是因为缺少 DLL 的依赖项而不是 DLL 本身,或者因为 DLL 与尝试的应用程序不兼容加载它。如果您的 C# 应用程序设置为“任何 CPU”,并且您使用的是 64 位计算机,则在加载非托管 32 位 DLL 时会出现此类错误。

One way to isolate the problem would be to create a simple C/C++ project which loads the DLL. (Load it dynamically with LoadLibrary if you don't have access to the import lib.) Then use Dependency Walker to profile the test harness, and it will report the names of missing DLLs.

隔离问题的一种方法是创建一个加载 DLL 的简单 C/C++ 项目。(如果您无权访问导入库,请使用 LoadLibrary 动态加载它。)然后使用 Dependency Walker 来分析测试工具,它会报告丢失的 DLL 的名称。

回答by Alex Mendez

I found this in another post. Maybe it will help your situation

我在另一个帖子中找到了这个。也许它会帮助你的情况

NUnit "missing" GPSVC.DLL on Windows 7/64

Windows 7/64 上的 NUnit“缺少”GPSVC.DLL