问题标签 [dllmain]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
537 浏览

c++ - 加载 Qt 共享库时不显示 Qt 小部件

要求:Qt 小部件在 Qt 共享库加载时显示,对于非 Qt应用程序。

经过一些网络搜索,我发现:

  1. 所有 Qt 小部件都必须存在于“主线程”中,“主线程”是 Qt 对象创建的第一个线程。因此,创建一个非 Qt 线程(std::thread),然后在该线程中创建 QApplication 和其他一些小部件应该可以工作,但不能。

  2. 在创建 QApplication 之前,不要在该非 Qt 线程中创建任何 Qt 相关对象或调用任何 Qt 相关静态方法。

  3. 线程解决方案不适用于 Mac OS,我的目标平台仅为 Windows,所以没关系。

  4. 就我而言,如果应用程序加载我的 Qt 库,并调用显示小部件的方法,它就可以工作。但由于某种原因,调用者无法手动调用我的 lib 方法。

  5. 如果宿主应用程序(加载共享库的应用程序)是 Qt 应用程序,您应该调用 QApplication::processEvents(),而不是 QApplication::exec()。就我而言,我应该在该线程中调用 QApplication::exec() 。

源代码在这里:

  • dll主要版本
  • 简单 C++ 静态类版本

两个版本都成功调用小部件初始化内容,但小部件未显示。

这是我使用 Qt 加载 lib 进行测试的方法,但是,我使用 Win32 API 加载 lib 的事件也失败了。

0 投票
1 回答
902 浏览

c++ - CreateRemoteThread 成功,但某些目标应用的 LoadLibrary 失败

我正在使用 CreateRemoteThread() + LoadLibrary() 方法注入代码。当我在我的 Windows7 64 位操作系统笔记本电脑上运行我的注入器时,一切都很好,并且对于某些目标应用程序,它仍然可以在 Windows Server 2012 R2 64 位中工作。

但是,在这个 Windows Server 2012 环境中,对于某些目标应用程序,它是旧的 MFC 应用程序,CreateRemoteThread 成功但 DllMain 没有被调用,我发现 LoadLibrary() 似乎失败了,通过在创建的远程线程上使用 GetExitCodeThread() .

对于要写入目标进程的内存,我计算了终止的 0 字节。

另外,我已经知道 kernel32.dll 地址对于 Windows 7 和 Windows Server 2012 是相同的,使用下面 URL 答案部分中介绍的方法。

CreateRemoteThread失败,可能是目标进程中的lpBaseAddress无效,是系统分配的?

下面的 GetExitCodeThread() 的退出代码为零。

你知道我接下来应该怀疑什么吗?

总而言之,在下图中,您可以看到唯一的失败是我在 Windows Server 2012 上运行注入器以注入一些旧的 MFC 应用程序。

2 操作系统上的结果

在下图中,有关于 MFC 应用程序有多旧的信息:

dll 使用旧的 MFC

我正在尝试提供足够的信息,如果您需要更多信息,请告诉我。

下面是注入我的 dll 的完整代码:

0 投票
0 回答
217 浏览

c++ - 为什么这个单例从 DllMain 调用时偶尔会崩溃?

我有以下在 Visual Studio 2015 中编译的类:

该类旨在从 DllMain 运行并检查 NtDll 加载的图像中的结构,以检查是否启用了某个缓解策略。

在极少数情况下(<0.0001% 的 dll 加载次数),此单例会导致应用程序崩溃。但是,崩溃与班级试图做的事情或众所周知的 DllMain 中不允许的事情列表无关(据我所知)。它与单例自身的实例化有关。

WinDbg 中的调用堆栈如下所示:

线路发生访问冲突static MitigationPolicyChecker instance

而崩溃的流水线是这里的最后一行,根据异常记录貌似rdx和rcx都是null(可能是线程本地存储失败??):

那么这里发生了什么?该进程在崩溃时只有一个线程处于活动状态,所以我不认为这是一个并发问题。据我所知:

  1. 静态对象应在首次访问时创建。
  2. 静态通常会在 DllMain 之前初始化,但在这种情况下,由于它是函数级别的静态,它会在调用时创建,并且由于它是 VS2015,它将被创建为“魔术静态”。

有什么我遗漏的东西使这不安全吗?

0 投票
1 回答
191 浏览

c++ - 注入时未调用 DllMain(),但使用 LoadLibrary() 调用

DLL代码:

加载库代码:

当我使用上面的代码加载 dll 时,消息框会按预期弹出。

当我尝试用我能找到的任何注入器注入 dll 时,DllMain 永远不会被调用。

目标进程和 dll 架构都是 x64。目标进程已加载 MessageBoxW() 所需的库。如果需要,这就是我编译 dll (mingw) 的方式:gcc.exe main.cpp -shared -fPIC -o dll.dll

也许我已经尝试过我的 5 个注射器不走运,有什么建议吗?

还有什么可能导致消息框不弹出?

0 投票
1 回答
395 浏览

c++ - c++ dll无限循环不使用线程

是否可以在不使用线程的情况下在 dll 函数中使用无限循环?

这是一些示例代码:

这是线程中的函数示例:

这段代码运行良好。我只是想知道是否有其他选择。例如:函数可以写入进程的内存吗?或稍后调用,而不是 DLL_PROCESS_ATTACH?

0 投票
0 回答
44 浏览

python - Adding custom dllmain to cythonized module

Consider the following scenario:

A python tool should be deployed for external users in a way that (a) the source is protected and (b) it is ensured that only users with valid license can use it. Furthermore, for internal users, the code should be available as pure python, so debugging with a python debugger is possible and no compiling is necessary.

A license check is available and written in C.

(a) is solved by using cython.

Thoughts on (b):

The license check could be applied to a "central" part of the program (i.e., "a class that contains enough business logic so that no one could replace it"). Of course I could use cython to link the license check to this class's *.pyd and use the c function, but then it would be impossible for the internal user to use / debug the class. On the other hand, applying the license check to a class that is "unimportant" while debugging would mean that it could (presumably) be exchanged rather easily.

Creating a separate function "licenseIsValid() -> bool" and calling it from a central position in the code is not feasible as it can be replaced easily.

As a matter of fact, (almost) everything written in python can be replaced and therefore is unsafe.

So I came up with the idea of implementing the license check before anything python-related is executed: inside of the dllmain function. I could call the license check function on startup of the dll / pyd and stop the application before executing any cython code.

Is there a way to add a custom dllmain to a cythonized module? Is it even safe to add a dllmain (or does cython require a "specialized" dllmain)?

One last remark: I am aware that python is the wrong choice of language for creating closed source code with license check. But the advantages of using python far outweigh this...

Thank you in advance, Jan

0 投票
1 回答
137 浏览

delphi - MSVC DLL 加载:是 __declspec(dllexport) 函数在没有 DllMain 的情况下直接加载的

我尝试在 VS C++ 中编写一个 DLL 项目,其中包含一些导出函数,如下所示:

我注意到 VS 项目带有包含 DllMain 入口函数的文件 dllmain.cpp。但是,我注释了 DllMain 函数并使用 Delphi exe 应用程序调用导出的函数,如下面的函数指针:

Delphi 应用程序成功调用了 DLL 导出函数。我认为这种方法是 DLL 显式链接。所以我想清楚地理解,是显式加载还是隐式加载。如果是这样,如何在没有 DllMain 的情况下加载导出的函数。我没有找到任何调用 LoadLibrary 来加载 DLL。

0 投票
1 回答
60 浏览

c++ - 在获取 DLL_PROCESS_DETACH 之前需要通知 DLL 正在关闭

我们有一个使用 C++11 在 Visual Studio 中构建的 DLL。此时我们的 DLL 有一个固定的 API,它包括一个openandclose函数(以及其他)。我们允许我们的用户在不关闭应用程序的情况openclose多次使用。在我们的 DLL 中,我们使用了一个第三方库,它不仅有一个openand close,还有一个initializeand uninitialize

openandclose映射到我们的and opencloseinitializeanduninitialize每次只能调用一次,在启动和关闭时。我initialize只能从我们的函数中调用它们一次open,但我找不到调用的地方uninitialize,因为我不知道应用程序何时将关闭。调用它的最合乎逻辑的地方是DllMainDLL_PROCESS_DETACH. 如果我只是放弃并删除他们的uninitialize电话,我仍然会得到一个未处理的异常,并且在任何情况下我都不会干净地关闭。

有没有办法获得应用程序在之前关闭的信号/通知DLL_PROCESS_DETACH

0 投票
1 回答
260 浏览

forms - 创建一个 Delphi Dll 并使用 DllMain 加载它

朋友们

我有一个小问题。我试图在 RAD Studio 中创建一个带有表单的 delphi Dll,但我不知道如何使用 DllMain 加载它。我想在运行时将此 Dll 注入到第三方进程中

我用没有问题的表单创建了 Dll 项目,但是我找不到与“如何使用 DllMain 加载它”相关的任何好东西,或者至少我发现的教程/东西对我没有帮助(或者我只是哑的)。有人能帮我吗?给我一些提示或我可以学习的网站/视频?

我真的很感谢你们的时间!=)

0 投票
2 回答
165 浏览

c++ - 删除在 Visual Studio 中创建 dll 项目时生成的额外文件

在 VS17 中创建 dll 项目时,我看到在初始化时创建了多个文件。

在此处输入图像描述

但是无论我从事哪个 C++ 项目,我都没有在他们的环境中看到任何这样的文件。如何在我的环境中摆脱这些文件。是否有任何解决方法可以完全删除它们或将这 4 个文件减少到一个文件以减少混乱?

同样在 VS17 之前,我们曾经有stdafx.h,必须包含此标头,但在少数项目中我找不到此文件,有没有办法完全删除这些初始文件?