加载与引用 .NET DLL 位于同一文件夹中的引用的 .NET DLL 时"Not found"异常

"Not found" exception on loading referenced .NET DLL that is in same folder as referncing .NET DLL

本文关键字:NET DLL 引用 异常 Not found 文件夹 于同一 加载      更新时间:2023-10-16

我有一个.NET DLL SigToolNet.DLL有多个可见的COM类。 它引用本机C++静态库FTDIVCPLIB的.NET DLL FTDIVcpLibNet.DLL包装器,该包装器又引用第三方本机DLL ftd2xx.dll。 SigToolNet.DLL已成功注册32位

C:Release> regasm SigToolNet.dll /codebase
RegAsm : warning RA0000 : Registering an unsigned assembly with /codebase can cause your assembly to interfere with other applications that may be installed on the same computer. The /codebase switch is intended to be used only with signed assemblies. Please give your assembly a strong name and re-register it.
Types registered successfully

旁注,我不知道为什么它会给我这个警告,因为 SigToolNet.dll 是代码签名的。 在安装了VS2019的开发系统上运行时.exe应用程序TestHarness(在SigToolNet中引用COM可见类.DLL成功运行。 但是,当SigToolNet.DLL安装在另一台计算机上时,我在运行时收到异常,即调用对COM可见方法之一(DetectDevice(的第二次调用

Command DetectDevice failed
Error code: -2147024770
Desc: Could not load file or assembly 'FTDIVcpLibNet.dll' or one of its dependencies. The specified module could not be found.

所有引用的DLL(包括FTDIVcpLibNet.dll都位于SigToolNet.DLL所在的同一文件夹中,该文件夹也是其注册的位置。

我尝试在 3 台不同的计算机上运行,这些计算机在 Windows 7 和 Windows 10 上都有相同的失败结果。 它成功运行的开发系统正在运行 Windows 10。

我还尝试使用程序集绑定日志查看器 (fuslogvw.exe( 跟踪程序集加载失败

*** Assembly Binder Log Entry  (6/26/2020 @ 4:00:00 PM) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from:  C:WindowsMicrosoft.NETFrameworkv4.0.30319clr.dll
Running under executable  C:Vendor Testing HarnessEpic 2018 And BeyondEpicESigTestHarness.exe
--- A detailed error log follows. 
=== Pre-bind state information ===
LOG: DisplayName = SigToolNet, Version=2020.0.0.3, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Vendor Testing Harness/Epic 2018 And Beyond/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = EpicESigTestHarness.exe
Calling assembly : (Unknown).
===
LOG: This bind starts in the default load context.
LOG: Download of the application configuration file was attempted from file:///C:/Vendor Testing Harness/Epic 2018 And Beyond/EpicESigTestHarness.exe.config.
LOG: Configuration file C:Vendor Testing HarnessEpic 2018 And BeyondEpicESigTestHarness.exe.config does not exist.
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Vendor Testing Harness/Epic 2018 And Beyond/SigToolNet.DLL.
LOG: Attempting download of new URL file:///C:/Vendor Testing Harness/Epic 2018 And Beyond/SigToolNet/SigToolNet.DLL.
LOG: Attempting download of new URL file:///C:/Vendor Testing Harness/Epic 2018 And Beyond/SigToolNet.EXE.
LOG: Attempting download of new URL file:///C:/Vendor Testing Harness/Epic 2018 And Beyond/SigToolNet/SigToolNet.EXE.
LOG: All probing URLs attempted and failed.

我发现这个日志令人困惑。 它说它无法找到SigToolNet.dll但它似乎在与TestHarness.exe相同的文件夹中查找它。 它应该在C:\Release中寻找它,这是SigToolNet.dll成功注册的地方。 此外,报告的异常不是它找不到SigToolNet.dll而是它找不到FTDIVcpLibNet.dll。 它看起来的第一个位置不应该是注册SigToolNet.dll的同一文件夹吗?

我在VS2019中使用了远程调试,并且能够在调用第一个方法Init时在SigToolNet中执行断点和单步。 但是,由于调用执行前的异常,第二次调用 DetectDevice 上的断点永远不会发生。

所有 .NET DLL 都使用 .NET 4.8

我一直无法确定为什么它在开发SigToolNet.dll并安装了VS2019的系统上成功运行,但没有在其他系统上运行。

您可能必须为您的 FTDIVCPLIB.dll 安装合适的 C++ Redisdistribaable 版本。在装有VS2019的机器上,您已经拥有了它。