简体   繁体   English

无法在 C# 中加载 DLL

[英]Unable to load DLL in C#

how to load a dll in ac# project如何在ac#项目中加载dll

error :错误

Unable to load DLL 'Reader.dll': The specified module could not be found.无法加载 DLL 'Reader.dll':找不到指定的模块。 (Exception from HRESULT: 0x8007007E) (来自 HRESULT 的异常:0x8007007E)

code sample :代码示例

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

image:图像:异常截图

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

One way to isolate the problem would be to create a simple C/C++ project which loads the DLL.隔离问题的一种方法是创建一个加载 DLL 的简单 C/C++ 项目。 (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. (如果您无权访问导入库,请使用 LoadLibrary 动态加载它。)然后使用 Dependency Walker 来分析测试工具,它会报告丢失的 DLL 的名称。

Although the reader.dll is unable to load GPSVC.dll and IESHIMS.DLL.虽然 reader.dll 无法加载 GPSVC.dll 和 IESHIMS.DLL。 i managed to make it work by running the corflags command on application.exe the application is now marked as 32bit :我设法通过在 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

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

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

For me the solution was to installing the C++ Redistrable X64 on client machine.对我来说,解决方案是在客户端机器上安装 C++ Redisstrable X64。 (Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019.) (适用于 Visual Studio 2015、2017 和 2019 的 Microsoft Visual C++ Redistributable。)

The dll was already on the right place, in the same folder than the .exe file. dll 已经在正确的位置,与 .exe 文件位于同一文件夹中。

Here you found the download link:在这里你找到了下载链接:

https://support.microsoft.com/en-us/topic/the-latest-supported-visual-c-downloads-2647da03-1eea-4433-9aff-95f26a218cc0 https://support.microsoft.com/en-us/topic/the-latest-supported-visual-c-downloads-2647da03-1eea-4433-9aff-95f26a218cc0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM