【发布时间】:2021-11-08 12:00:04
【问题描述】:
我正在开发一个使用 C++ dll 的 C# 桌面应用程序。我遇到了一个我无法解决的问题:
System.TypeInitializationException: 'The type initializer for 'Teigha.Core.GlobalsPINVOKE'
threw an exception.'
1 of 2 Inner Exceptions:
TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an
exception.
2 of 2 Inner Exceptions:
DllNotFoundException: Unable to load dll 'TD_SwigCore_22.7_16.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E).
-
我的第一次尝试是通过转到项目参考来添加 dll -> 添加参考,但随后出现错误消息:
A reference to C:\blablabla\TD_SwigCore_22.7_16.dll could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component. -
第二次尝试是使用 DllImport 进行尝试,如下所述:https://docs.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.dllimportattribute?redirectedfrom=MSDN&view=net-5.0 我得到一个编译器错误:
C50592: Attribute #dllImport' is not valid on this declaration type. it is only valid on 'method' declarations. -
第三次尝试是使用 regsvr32 作为可能的解决方案之一:How do I resolve "Please make sure that the file is accessible and that it is a valid assembly or COM component"?
regsvr32 "C:\blablabla\TD_SwigCore_22.7_16.dll"并收到错误消息:
the module "C:\blablabla\TD_SwigCore_22.7_16.dll" was loaded but the entry-point DllRegisterServer was not found. Make sure that "C:\blablabla\TD_SwigCore_22.7_16.dll" is a valid DLL or OCX file and then try again -
我的第四次也是最后一次尝试是找出我的“TD_SwigCore_22.7_16.dll”的所有依赖项是什么,我已经这样做了:
dumpbin /DEPENDENTS "C:\blablabla\TD_SwigCore_22.7_16.dll"我得到了一个包含所有依赖项的列表,然后我在文件夹 bin\x64\Debug 中添加了所有这些 dll,如堆栈溢出解决方案之一中所述,但我仍然遇到相同的错误。
到现在为止,我觉得我已经尝试了所有可以在 Internet 上找到的解决方案,但没有任何效果。 重要的是要知道我的程序在发布模式下运行良好,只是在调试模式下不行。
目标框架是:.NET Framework 4.8
我们将不胜感激!
【问题讨论】:
-
你能 dumpbin /exports "C:\blablabla\TD_SwigCore_22.7_16.dll" 并分享吗?甚至更好地使用dependencywalker.com
-
欢迎。您还可以使用fusion log viewer 查看已加载的 DLL 和无法找到的 DLL(甚至 CLR 尝试查找它们的位置)。
-
@Orenico 当我在导出中执行此操作时,列表长于 25k,因此我无法共享它,当我使用dependecywalker 并打开我的 .exe 文件时,我无法对其进行分析。我试过这里写的:www-archive.mozilla.org/quality/help/dependency-walker.html
-
@imsmn 我不确定这是否会有所帮助 Fuslogvw 实用程序主要用于解决托管程序集的绑定问题,如果我的问题正确,David 将面临一个非托管 c++ 编译和链接的问题
-
@David 我猜 c++ dll 不是一个 com 组件,所以尝试 regsvr 这个 dll 和你得到的错误 3) 是有道理的,因为没有 Dllregister 函数事情。如果您可以使用 dllimport 属性粘贴您尝试过的包装器,我可以尝试并提供进一步的帮助