调用 C++/CLI DLL 时 .NET 应用程序中的文件未找到异常
File Not Found exception in .NET application when calling into C++/CLI DLL
我有一个用 C# 编写的简单 .NET 应用程序,它与 C++/CLI DLL 链接
它本身只是 C++ 静态库的薄包装。
应用程序 运行 在开发机器上运行良好 (Windows 8),但是,当 EXE 和
DLL 被复制到另一台机器 (Windows 10) 它立即崩溃
启动。
查看事件查看器,我可以看到崩溃是由于未处理的
异常,System.IO.FileNotFoundException
,并且调用堆栈表明
异常发生在调用 DLL 的函数中。
如果我立即将DLL中的函数编辑为return,那么崩溃仍然存在
发生。在调用周围添加 try-catch
块也没有效果。然而,
如果我完全删除调用,那么应用程序 运行s.
即使我将 EXE 和 DLL 移动到
不同的位置。但是,如果我从目录中删除 DLL
EXE定位,然后出现一模一样的崩溃。
这表明问题是应用程序在 运行 时找不到 DLL
另一台机器。
应用程序和 DLL 的项目都包含在同一个文件中
Visual Studio 2015 解决方案,应用程序通过一个链接到 DLL
项目参考。
应用程序的目标是.NET 4.5.2,另一台机器是.NET 4.6
安装。应用程序项目中唯一的其他参考是部分
.NET,例如 Microsoft.CSharp
和 System.Core
.
如何修复此崩溃?
编辑: 正如 在评论中所建议的,我尝试使用 fuslogvw.exe
记录绑定失败,它获得了以下日志摘录:
*** Assembly Binder Log Entry (08/04/2016 @ 14:47:02) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable D:\[Path]\[App Name].exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = [DLL Name], Version=1.0.5941.28221, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///D:/[Path]/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = [App Name].exe
Calling assembly : [App Name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.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:///D:/[Path]/[DLL Name].DLL.
LOG: Attempting download of new URL file:///D:/[Path]/[DLL Name]/[DLL Name].DLL.
LOG: Attempting download of new URL file:///D:/[Path]/[DLL Name].EXE.
LOG: Attempting download of new URL file:///D:/[Path]/[DLL Name]/[DLL Name].EXE.
LOG: All probing URLs attempted and failed.
所以好像找不到的文件是我的DLL,那个.NET是
确实在寻找文件的正确位置,但由于某种原因它是
在那里找不到它。
请注意,当我尝试 运行 删除了 DLL 的 EXE 时,我在我的开发机器上获得了相同的错误日志。
如评论中所述,我尝试安装 Visual C++ 2015 Redistributable。这在当时似乎没有任何效果。然而,几天后,问题不再重现,我想不出还有什么办法可以解决这个问题。 (可再发行安装程序没有要求重新启动,但我最好的猜测是实际上需要重新启动。)
我有一个用 C# 编写的简单 .NET 应用程序,它与 C++/CLI DLL 链接 它本身只是 C++ 静态库的薄包装。
应用程序 运行 在开发机器上运行良好 (Windows 8),但是,当 EXE 和 DLL 被复制到另一台机器 (Windows 10) 它立即崩溃 启动。
查看事件查看器,我可以看到崩溃是由于未处理的
异常,System.IO.FileNotFoundException
,并且调用堆栈表明
异常发生在调用 DLL 的函数中。
如果我立即将DLL中的函数编辑为return,那么崩溃仍然存在
发生。在调用周围添加 try-catch
块也没有效果。然而,
如果我完全删除调用,那么应用程序 运行s.
即使我将 EXE 和 DLL 移动到 不同的位置。但是,如果我从目录中删除 DLL EXE定位,然后出现一模一样的崩溃。
这表明问题是应用程序在 运行 时找不到 DLL 另一台机器。
应用程序和 DLL 的项目都包含在同一个文件中 Visual Studio 2015 解决方案,应用程序通过一个链接到 DLL 项目参考。
应用程序的目标是.NET 4.5.2,另一台机器是.NET 4.6
安装。应用程序项目中唯一的其他参考是部分
.NET,例如 Microsoft.CSharp
和 System.Core
.
如何修复此崩溃?
编辑: 正如 fuslogvw.exe
记录绑定失败,它获得了以下日志摘录:
*** Assembly Binder Log Entry (08/04/2016 @ 14:47:02) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable D:\[Path]\[App Name].exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: DisplayName = [DLL Name], Version=1.0.5941.28221, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///D:/[Path]/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = [App Name].exe
Calling assembly : [App Name], Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.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:///D:/[Path]/[DLL Name].DLL.
LOG: Attempting download of new URL file:///D:/[Path]/[DLL Name]/[DLL Name].DLL.
LOG: Attempting download of new URL file:///D:/[Path]/[DLL Name].EXE.
LOG: Attempting download of new URL file:///D:/[Path]/[DLL Name]/[DLL Name].EXE.
LOG: All probing URLs attempted and failed.
所以好像找不到的文件是我的DLL,那个.NET是 确实在寻找文件的正确位置,但由于某种原因它是 在那里找不到它。
请注意,当我尝试 运行 删除了 DLL 的 EXE 时,我在我的开发机器上获得了相同的错误日志。
如评论中所述,我尝试安装 Visual C++ 2015 Redistributable。这在当时似乎没有任何效果。然而,几天后,问题不再重现,我想不出还有什么办法可以解决这个问题。 (可再发行安装程序没有要求重新启动,但我最好的猜测是实际上需要重新启动。)