检测CRT初始化是否在注入进程中完成

发布于 2024-12-17 16:03:29 字数 582 浏览 1 评论 0原文


我正在开发一个在进程启动时注入 dll 的应用程序(挂起 --> 注入 --> 恢复) DllMain 中使用 DLL_PROCESS_ATTACH (在我注入的 dll 中)的第一个调用是对 MessageBox() 的调用(仅用于调试目的)。 但是,对 MessageBox() 的调用有时会弹出错误并使注入的进程崩溃。

运行时错误!
程序:C:\Program Files\Microsoft Office\Office14\OUTLOOK.EXE

R6030
- CRT 未初始化

例如,这可以通过 Outlook 和 Winword 重现。通过记事本、IE、CMD、Calc 和许多其他 - 打印消息框并正常继续。

打印消息框对我来说不是必须的,所以我只是想能够检查CRT是否已完成初始化,这样我就可以像这样正常继续:

case DLL_PROCESS_ATTACH:
     if (IsCRTInitialized())
        MessageBox(...);

如果缺少某些信息,请告诉我。 谢谢!

I'm working on an application that injects a dll when a process starts (Suspend --> Inject --> Resume)
The very first call in DllMain with DLL_PROCESS_ATTACH (in the dll I injected) is a call to MessageBox() (just for debugging purpose).
However, this call to MessageBox() sometimes pops an error and crashes the injected process.

Runtime Error!
Program: C:\Program Files\Microsoft
Office\Office14\OUTLOOK.EXE

R6030
- CRT not initialized

This is reproducible with Outlook and Winword for example. Though Notepad, IE, CMD, Calc and many others - print the message box and continue normally.

Printing a message box is not a must-have for me, so I just want to be able to check whether CRT has done initialization or not, so I can continue normally like this:

case DLL_PROCESS_ATTACH:
     if (IsCRTInitialized())
        MessageBox(...);

Please let me know if some information is missing.
Thanks!

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

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

发布评论

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

评论(2

初与友歌 2024-12-24 16:03:29

当入口函数 DLLMain 被调用。 MessageBox 驻留在 user32.dll 中,并按照 user32.dll 创建 DLL 调用函数的最佳实践是严格禁止的。

您可以

  1. 调用OutputDebugString 用于任何调试器跟踪。此函数驻留在 kernel32.dll 中,应该可以安全调用。

  2. 在您的应用程序加载任何其他 dll 之前,请自行调用 MessageBox。这将确保 user32.dll 及其依赖项
    已经加载。这样在 DllMain 中调用 MessageBox 可能会有
    更好的成功机会。但您的情况可能会有所不同。

Kernel32.dll is guaranteed to be loaded in the process address space when the entry-point function DLLMain is called. MessageBox resides in user32.dll and as per Best practices for creating DLL calling functions from user32.dll is a strict no-no.

You can either

  1. Call OutputDebugString for any debugger tracing. This function resides in kernel32.dll and should be safe to call.

  2. Before your application loads any other dlls, call MessageBox yourself. This will ensure that user32.dll and its dependencies are
    already loaded. This way calling MessageBox in DllMain may have
    a better chance of succeeding. But your mileage may vary.

难理解 2024-12-24 16:03:29

问题不在于 CRT。您不得从 DllMain 调用 MessageBox 或任何其他重要函数

The problem isn't the CRT. You're not allowed to call MessageBox or any other non-trivial function from DllMain

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