在 DLL 构建期间包含特定文件时 LoadLibrary 失败

发布于 2024-07-16 02:12:43 字数 372 浏览 10 评论 0原文

我的 C++ 应用程序的一个 DLL 中出现了非常奇怪的行为。 它可以正常工作并加载,直到我在 DLL 的主文件中使用 #include 包含单个文件。 然后我收到此错误消息:

从 D:/Targets/bin/MatrixWorkset.dll 加载组件 无法加载“D:/Targets/bin/MatrixWorkset.dll”:无法加载库 MatrixWorkset:对内存位置的访问无效。

现在我已经通过代码和谷歌搜索了又搜索,但我无法弄清楚发生了什么。 到目前为止,所有内容都在一个 DLL 中,我决定将其拆分为两个较小的 DLL。 导致问题的文件是另一个第二个库的一部分(可以正常加载)。

任何想法都会非常感激。 谢谢, 哈科

I'm getting really strange behavior in one of the DLLs of my C++ app. It works and loads fine until I include a single file using #include in the main file of the DLL. I then get this error message:

Loading components from D:/Targets/bin/MatrixWorkset.dll
Could not load "D:/Targets/bin/MatrixWorkset.dll": Cannot load library MatrixWorkset: Invalid access to memory location.

Now I've searched and searched through the code and google and I can't figure out what is going on. Up till now everything was in a single DLL and I've decided to split it into two smaller ones. The file that causes the problems is part of the other second library (which loads fine).

Any ideas would really be appreciated.
Thanks,
Jaco

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

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

发布评论

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

评论(6

撩发小公举 2024-07-23 02:12:43

可能的原因是全局类类型。 构造函数从 DllMain() 运行,而 DllMain()LoadLibrary() 返回之前依次运行。 在 DllMain() 返回之前您可以执行的操作有很多限制。

The likely cause is a global with class type. The constructor is run from DllMain(), and DllMain() in turn runs before LoadLibrary() returns. There are quite a few restrictions on what you can do until DllMain() has returned.

深者入戏 2024-07-23 02:12:43

标头是否可能在某处包含 #pragma comment(lib,"somelibrary.lib") 语句? 如果是这样,它会自动尝试导入库。

为了解决这个问题,我首先查看带有depends的二进制文件(http://www.dependencywalker.com/),查看是否存在任何您不期望的 DLL 依赖项。 如果您确实找到了某些内容并且位于 Visual Studio 中,则应该在链接器上打开“显示进度”AKA /VERBOSE。

由于您收到对内存位置的无效访问,因此 DLLMAIN 或某些静态初始化程序中可能存在某些内容正在崩溃。 你能简化MatrixWorkset.dll(假设你写了它)吗?

Is it possible that header includes a #pragma comment(lib,"somelibrary.lib") statement somewhere? If so it's automatically trying to import a library.

To troubleshoot this I'd start by looking at the binary with depends (http://www.dependencywalker.com/), to see if there are any DLL dependencies you don't expect. If you do find something and you are in Visual Studio, you should turn on "Show Progress" AKA /VERBOSE on the linker.

Since you are getting the Invalid Access to memory location, it's possible there's something in the DLLMAIN or some static initializer that is crashing. Can you simplify the MatrixWorkset.dll (assuming you wrote it)?

南烟 2024-07-23 02:12:43

您描述的错误听起来像是运行时错误。 这个错误是 Windows 自动显示的还是您的程序发出的?

我说将调试器附加到您的应用程序并跟踪此错误的来源。 Windows 无法加载依赖项吗? 您的库是否在加载时失败了?

如果您想排除或排除您所包含的此头文件,请尝试使用或不使用此 #include 来预编译主源文件,并比较两个结果。

The error you describe sounds like a run-time error. Is this error displayed automatically by windows or is it one that your program emits?

I say attach a debugger to your application and trace where this error is coming from. Is Windows failing to load a dependency? Is your library somehow failing on load-up?

If you want to rule in/out this header file you're including, try pre-compiling your main source file both with and without this #include and diff the two results.

静若繁花 2024-07-23 02:12:43

我还是没搞定。 让我回答一些问题:

1)Windows 没有加载依赖项失败,我认为因为 Dependency Walker 显示一切正常。
2) 我已经附加了一个调试器,当它尝试加载 MatrixWorkset.dll 时,它基本上会打印以下内容:

            10:04:19.234
stdout:&"warning: Loading components from D:/ScinericSoftware/VisualWorkspace/trunk/Targets/bin/MatrixWorkset.dll\n"
            10:04:19.234
stdout:&"\n"
status:Stopped: "signal-received"
status:Stopped.
            10:04:19.890
stdout:30*stopped,reason="signal-received",signal-name="SIGSEGV",signal-meaning="Segmentation fault",thread-id="1",frame={addr="0x7c919994",func="towlower",args=[],from="C:\\WINDOWS\\system32\\ntdll.dll"}
input:31info shared
input:32-stack-list-arguments 2 0 0
input:33-stack-list-locals 2
input:34-stack-list-frames
input:35-thread-list-ids
input:36-data-list-register-values x
            10:04:19.890

3) MSalters:我不确定“全局类类型”是什么意思。 出现问题的文件已包含在另一个 DLL 中,该 DLL 工作正常并且 DLL 加载成功。

这是 MatrixVariable.h 文件的顶部:

#include "QtSF/Variable.h"   // Located in depending DLL (the DLL in which this file always lived.
#include "Matrix.h"    // File located in this DLL
#include "QList"      // These are all files from the Qt Framework
#include "QModelIndex"
#include "QItemSelection"
#include "QObject"

using namespace Zenautics;
using namespace std;

class MatrixVariable : public Variable
{
    Q_OBJECT
    Q_PROPERTY(int RowCount READ rowCount WRITE setRowCount)
    Q_PROPERTY(int ColumnCount READ columnCount WRITE setColumnCount)
    Q_PROPERTY(int UndoPoints READ undoPoints WRITE setUndoPoints)

public:
    //! Default constructor.
    MatrixVariable(const QString& name, int rows, int cols, double fill_real = 0, double fill_complex = 0, bool isReal = true);

等等。等等。

一个可能的解决方案是将 MatrixVariable 文件放回到原始 DLL 中,但这违背了将 DLL 分成更小的部分的整个想法,这实际上不是一个选项。

I'm still not getting it going. Let me answer some of the questions asked:

1) Windows is not failing to load a dependency, I think since Dependency Walker shows everything is ok.
2) I've attached a debugger which basically prints the following when it tries to load MatrixWorkset.dll:

            10:04:19.234
stdout:&"warning: Loading components from D:/ScinericSoftware/VisualWorkspace/trunk/Targets/bin/MatrixWorkset.dll\n"
            10:04:19.234
stdout:&"\n"
status:Stopped: "signal-received"
status:Stopped.
            10:04:19.890
stdout:30*stopped,reason="signal-received",signal-name="SIGSEGV",signal-meaning="Segmentation fault",thread-id="1",frame={addr="0x7c919994",func="towlower",args=[],from="C:\\WINDOWS\\system32\\ntdll.dll"}
input:31info shared
input:32-stack-list-arguments 2 0 0
input:33-stack-list-locals 2
input:34-stack-list-frames
input:35-thread-list-ids
input:36-data-list-register-values x
            10:04:19.890

3) MSalters: I'm not sure what you mean with a "global with class type". The file that is giving the problems have been included in a different DLL in which it worked fine and the DLL loaded successfully.

This is the top of the MatrixVariable.h file:

#include "QtSF/Variable.h"   // Located in depending DLL (the DLL in which this file always lived.
#include "Matrix.h"    // File located in this DLL
#include "QList"      // These are all files from the Qt Framework
#include "QModelIndex"
#include "QItemSelection"
#include "QObject"

using namespace Zenautics;
using namespace std;

class MatrixVariable : public Variable
{
    Q_OBJECT
    Q_PROPERTY(int RowCount READ rowCount WRITE setRowCount)
    Q_PROPERTY(int ColumnCount READ columnCount WRITE setColumnCount)
    Q_PROPERTY(int UndoPoints READ undoPoints WRITE setUndoPoints)

public:
    //! Default constructor.
    MatrixVariable(const QString& name, int rows, int cols, double fill_real = 0, double fill_complex = 0, bool isReal = true);

etc. etc. etc.

A possible solution is to put the MatrixVariable file back in the original DLL but that defeats the whole idea of splitting the DLL into smaller parts which is not really a option.

北方的韩爷 2024-07-23 02:12:43

最近,当我无法从命令行 EXE 加载 DLL 时,我从 GetLastError() 中收到该错误。 它曾经可以工作,然后我向 DLL 添加了一些 MFC 代码。 现在所有的赌注都落空了。

I get that error from GetLastError() when I fail to load a DLL from a command line EXE recently. It used to work, then I added some MFC code to the DLL. Now all bets are off.

网白 2024-07-23 02:12:43

我刚刚遇到了同样的问题。 一个一直工作正常的 dll 突然停止工作了。 我在初始化静态对象的 CRT 内容中遇到了访问冲突。 全部重建并没有解决问题。 但是当我手动注释掉所有静态数据时,链接器抱怨文件损坏。 再次链接:有效。 现在我可以加载库了。 然后,我将静态数据一一添加回去。每次,我都会重新编译并测试 LoadLibrary。 每次都工作得很好。 最终,我所有的静态数据都恢复了,一切正常。

如果我不得不猜测,链接器使用的某些中间文件已损坏(我看到 ilk 文件不断被 link.exe 损坏)。 如果可以的话,也许清除所有文件并进行干净的构建? 但我猜你已经想通了,因为这已经是 6 个月大的事了……

I just had this exact same problem. A dll that had been working just fine, suddenly stopped working. I was taking an access violation in the CRT stuff that initializes static objects. Doing a rebuild all did not fix the problem. But when I manually commented out all the statics, the linker complained about a corrupt file. Link again: Worked. Now I can LoadLibrary. Then, one by one, I added the statics back in. Each time, I recompiled and tested a LoadLibrary. Each time it worked fine. Eventually, all my statics were back, and things working normally.

If I had to guess, some intermediate file used by the linker was corrupted (I see the ilk files constantly getting corrupted by link.exe). If you can, maybe wipe out all your files and do a clean build? But I'm guessing you've already figured things out since this is 6 months old ...

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