加载库 193

发布于 2025-01-07 06:56:03 字数 2085 浏览 1 评论 0原文

我正在创建一个 C++/CLI dll,它将加载到旧版 C++ 应用程序中。遗留应用程序通过传统的 LoadLibrary 调用来完成此操作。应用程序和 C++/CLI dll 均以 64 位模式编译。

当发生 LoadLibrary 调用时,它会失败并出现错误 193。这通常意味着某些非 64 位组件正在尝试加载。当我查看 Visual Studio 2010 中的 dll 加载输出时,我发现加载 mscoree.dll 时发生故障(确切地说,我看到加载了我的 dll,然后加载了 mscoree,然后卸载了 mscoree,然后卸载了我的 dll) ,然后返回错误)。具体来说,C:\Windows\System32\mscoree.dll 正在被加载,当我检查这个 mscoree.dll 时,我发现它的目标是 I386。

如何确保我的应用程序链接到正确的 mscoree.dll?我知道这可以通过清单来完成,但我找不到任何有关设置清单的好信息。理想的解决方案将允许在 32 位或 64 位模式下进行编译并以正确的 mscoree.dll 为目标。

附带说明一下,我在并排文件夹中发现了一个 mscoree.dll,我验证该文件夹是 64 位模式,并将其复制到我的应用程序目录中,希望它能首先找到该文件。这不起作用,C:\Windows\System32 版本仍然加载。

谢谢,

最大

C++/CLI dll 上 CorFlags.exe 的

Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32+
CorFlags  : 16
ILONLY    : 0
32BIT     : 0
Signed    : 0

输出C:\System32\mscoree.dll 上 pedump.exe 的输出

PS C:\Windows\System32> pedump.exe .\mscoree.dll
Dump of file .\MSCOREE.DLL

File Header
  Machine:                      014C (I386)
  Number of Sections:           0004
  TimeDateStamp:                4B90752B -> Thu Mar 04 22:06:19 2010
  PointerToSymbolTable:         00000000
  NumberOfSymbols:              00000000
  SizeOfOptionalHeader:         00E0
  Characteristics:              2102
    EXECUTABLE_IMAGE
    32BIT_MACHINE
    DLL
...

(pedump 从这里继续描述导入和导出,但这在这里并不重要)

扩展加载信息

这是失败负载的完整输出。

注意:C++/CLI dll 称为 DsfClr.dll
通过运行 gflags.exe -i [exename] +sls 并在调试器 http://pastebin 中检查结果来获得输出

。 com/FyumUiMN

更新:

使用 Reuben 在下面的评论中发布的提示,我能够确定 mscoree.dll 确实针对 AMD64,但 pedump 提供了无效信息,因为它是正在WOW64中运行。话虽这么说,我仍然无法加载这个库,如果有人有任何建议,他们将不胜感激。
我还尝试过一件事:我创建了一个新的 C# 应用程序并引用了 C++/CLI dll,然后在 main() 函数中,我在 C++/CLI dll 中实例化了一个类。这会在调用 main() 函数之前导致访问冲突异常。当我删除实例化时,主函数运行正常。我的猜测是,实例化导致了 C++/CLI 程序集的延迟加载,这导致了我在本机程序集中看到的相同加载错误。

I am creating a C++/CLI dll that will be loaded into a legacy c++ application. The legacy application does this with a traditional call to LoadLibrary. Both the application and the C++/CLI dll are compiled in 64 bit mode.

When the LoadLibrary call happens, it fails with error 193. This usually means that some non-64bit component is trying to load. When I look at the dll load output in visual studio 2010, I see the the failure is occurring when mscoree.dll is being loaded (to be exact, I see my dll loaded, then mscoree loaded, then mscoree unloaded, then my dll unloaded, then the error returned). Specifically C:\Windows\System32\mscoree.dll is being loaded, when I examine this mscoree.dll, I find that it is targeting I386.

How can I ensure that my application will link against the correct mscoree.dll? I understand this could be done with a manifest, but I can't find any good information about setting one up. The ideal solution would allow compilation in 32bit or 64bit mode and target the correct mscoree.dll.

As a side note, I found an mscoree.dll in a side-by-side folder that I verified is 64bit mode and copied it into my application directory with the hopes that it would pick up that one first. This didnt work and the C:\Windows\System32 version was still loaded.

Thanks,

Max

Output of CorFlags.exe on the C++/CLI dll

Microsoft (R) .NET Framework CorFlags Conversion Tool.  Version  4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Version   : v4.0.30319
CLR Header: 2.5
PE        : PE32+
CorFlags  : 16
ILONLY    : 0
32BIT     : 0
Signed    : 0

Output of pedump.exe on C:\System32\mscoree.dll

PS C:\Windows\System32> pedump.exe .\mscoree.dll
Dump of file .\MSCOREE.DLL

File Header
  Machine:                      014C (I386)
  Number of Sections:           0004
  TimeDateStamp:                4B90752B -> Thu Mar 04 22:06:19 2010
  PointerToSymbolTable:         00000000
  NumberOfSymbols:              00000000
  SizeOfOptionalHeader:         00E0
  Characteristics:              2102
    EXECUTABLE_IMAGE
    32BIT_MACHINE
    DLL
...

(pedump goes on from here to describe imports and exports but thats not important here)

Extended loading information

This is the full output from failed load.

Note: The C++/CLI dll is called DsfClr.dll
the output was obtained by running gflags.exe -i [exename] +sls and examining the results in a debugger

http://pastebin.com/FyumUiMN

UPDATE:

Using a tip posted in a below comment by Reuben, I was able to determine that mscoree.dll is indeed targeting AMD64, but pedump is providing invalid information because it is being run in WOW64. That being said I still cannot load this library, if anyone has any suggestions they would be greatly appreciated.
One more thing I have tried: I made a new C# application and referenced the C++/CLI dll, then, in the main() function, I instantiated a class in the C++/CLI dll. This caused an access violation exception before the main() function is called. When I remove the instantiation, the main function runs fine. My guess is that the instantiation is causing a delay load of my C++/CLI assembly, which causes the same load error I was seeing from the native assembly.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心↖ 2025-01-14 06:56:03

如果有人遇到这个错误,事实证明它是由我的本机库使用 boost::threading 引起的。 boost::threading 库使用了一些奇怪的编译设置。结果是生成与 clr 或混合模式二进制文件不兼容的静态库。当然,Visual Studio对此一无所知,因此它很乐意将boost链接进来,并在加载dll时崩溃。
解决方案是在 boost::threading 中动态链接。最简单的方法是在项目设置中定义 BOOST_THREAD_DYN_LINK。一旦我定义了它,dll 就可以正常加载了。
在 google 上快速搜索 C++/CLI boost threading 将提供有关此错误的更多信息

In case anyone runs across this error, it turned out that it was caused by my native libraries use of boost::threading. The boost::threading library uses some weird compilation settings. The result is a static library that is incompatible with clr or mixed-mode binaries. Of course, visual studio has no idea of this, so it happily links boost in and crashes when the dll is loaded.
The solution is to dynamically link in boost::threading. The easiest way to do this is to define BOOST_THREAD_DYN_LINK in your project settings. Once I defined that, the dll loaded fine.
A quick google search of C++/CLI boost threading will give plenty more information about this error

柠檬色的秋千 2025-01-14 06:56:03

我只是有类似的情况。
LoadLibrary 失败,错误代码为 193。
我的 DLL 是一个托管 C++/CLI DLL,通过 LoadLibrary 从本机应用程序调用。

不过我只在win7系统上遇到错误。 win10上加载正常。这个原始问题的日期表明它是win7。

我将其范围缩小到 thread_local 类。
看来win7只支持基本类型,例如C指针作为thread_local。任何更复杂的东西,甚至是 win10 接受的 std::shared_ptr,都会在 Dll 加载时生成错误 193。

郑重声明,编译器是VS2015,代码风格是c++11。

I just has a similar scenario.
LoadLibrary failed with 193.
My DLL is a managed C++/CLI DLL called from a native application with LoadLibrary.

However I only get the error on win7 systems. It loads fine on win10. The date of this original question suggests it was win7.

I narrowed it down to a thread_local class.
It appears win7 only supports basic types such as C pointers as thread_local. Anything more complex, even std::shared_ptr which win10 accepts, generates error 193 on Dll load.

For the record, the compiler is VS2015, and the code style is c++11.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文