CRT初始化和DLLMain [英] CRT initialization and DLLMain

查看:723
本文介绍了CRT初始化和DLLMain的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

报价:

来自创建DLL的最佳实践文档 http://download.microsoft.com/download/a/f/7/af7777e5- Microsoft的7dcd-4800-8a0a-b18336565f5b / DLL_bestprac.doc

From the document "Best Practices for Creating DLLs" http://download.microsoft.com/download/a/f/7/af7777e5-7dcd-4800-8a0a-b18336565f5b/DLL_bestprac.doc of Microsoft:

DLL通常具有复杂的相互依赖性,隐含地定义了
中的顺序,库加载程序会有效地分析这些
依赖关系,计算正确的加载顺序,并以该
顺序加载DLL。 [1]

"DLLs often have complex interdependencies that implicitly define the order in which they must be loaded. The library loader efficiently analyzes these dependencies, calculates the correct load order, and loads the DLLs in that order."[1]

(在DLLMain中)使用动态C运行时的内存管理功能-
时间(CRT)。如果未初始化CRT DLL,则对这些函数的调用可以
导致进程崩溃。 [2]

"(within DLLMain) Use the memory management function from the dynamic C Run- Time (CRT). If the CRT DLL is not initialized, calls to these functions can cause the process to crash."[2]

来自MSDN: http://msdn.microsoft.com/en-us/l ibrary / 988ye33t.aspx

_ DllMainCRTStartup函数完成了几项工作,包括调用
_CRT_INIT,该初始化C / C ++运行时库并在静态非局部变量上调用C ++
构造函数。如果没有此功能,运行
时间库将保持未初始化状态。 [3]

"The _DllMainCRTStartup function does several things, including calling _CRT_INIT, which initializes the C/C++ run-time library and invokes C++ constructors on static, non-local variables. Without this function, the run- time library would be left in an uninitialized state."[3]

要初始化C运行时库,_DllMainCRTStartup调用一个名为DllMain的
函数。 [4]

"In addition to initializing the C run-time library, _DllMainCRTStartup calls a function called DllMain."[4]

问题:

如果您的DLL依赖于CRT DLL,则基于 [1] ,CRT DLL将被加载
首先(首先初始化),那么 [2] 会如何发生?

If your DLL depends on CRT DLLs, based on [1], CRT DLLs will be loaded first (be initialized first), so how [2] could happen ?

基于 [3] [4] ,_ DllMainCRTStartup将调用_CRT_INIT,其中
会初始化CRT,那么 [2] 会如何发生?

Based on [3] and [4], _DllMainCRTStartup will calls _CRT_INIT which initializes the CRT, so how [2] could happen ?

如果可执行文件通过隐式链接加载DLL,则会在可执行文件的入口
点(mainCRTStartup或WinMainCRTStartup)之前调用DLL的
_DllMainCRTStartup(和DLLMain)。 ,基于
[3] -_DllMainCRTStartup调用_CRT_INIT,其中初始化CRT,并且
mainCRTStartup也将初始化CRT,那么使用CRT实际发生的
是什么?

If an executeable file loads your DLL by "Implicit linking", the _DllMainCRTStartup (and DLLMain) of your DLL will be called before the entry point (mainCRTStartup or WinMainCRTStartup) of the executeable file, based on [3] - _DllMainCRTStartup calls _CRT_INIT which initializes the CRT, and the mainCRTStartup is going to initialize the CRT too, so what actually happend with the CRT ?

如果DLL将在mainCRTStartup之前加载,在DLLMain或其他导出函数中调用CRT函数
是否安全?

If your DLL will be loaded before mainCRTStartup, the calling to CRT functions within DLLMain or other export functions is safe or not ?

谁将实际初始化CRT DLL?

Who will initialize CRT DLLs actually ?

推荐答案

您在假设DLL的入口点始终为 _DllMainCRTStartup 。事实并非如此,这只是链接器的默认设置。它可以是程序员希望通过链接器的/ ENTRYPOINT选项快速,轻松地进行更改的任何内容。 Microsoft无法采取任何措施来防止这种情况。

You are working from the assumption that the entrypoint for a DLL is always _DllMainCRTStartup. This is not the case, it is merely the linker's default. It can be anything a programmer wants it to be, swiftly and easily changed with the linker's /ENTRYPOINT option. There is nothing that Microsoft can do to prevent this. Not a very good practice, pointing that out is was the point of that document.

因此,如果这样的自定义入口点也没有发生,那么[2]事故很容易被调用,这不是一个很好的做法。确保显式初始化CRT。这不仅涉及初始化CRT运行时状态,还涉及初始化DLL的全局状态,例如调用C初始化程序,静态C ++对象的构造函数以及分配线程局部变量。 CRT的DLL版本无法执行某些操作。请记住,_DllMainCRTStartup和_CRT_INIT链接到DLL本身,该代码不在CRT的DLL版本中。

So the [2] mishap is easily invoked if such a custom entrypoint doesn't also ensure to initialize the CRT explicitly. Which doesn't just involve initializing the CRT runtime state, it also involves initializing global state of the DLL, like calling C initializers, the constructors of static C++ objects and allocating thread-local variables. Something the DLL version of the CRT cannot do. Keep in mind that _DllMainCRTStartup and _CRT_INIT are linked into the DLL itself, that code is not in the DLL version of the CRT.

动态CRT自身的运行时状态由初始化CRT DLL自己的入口点,Windows加载程序确保它首先运行。

The dynamic CRT's own runtime state is initialized by the CRT DLL's own entrypoint, the Windows loader ensures it runs first.

这篇关于CRT初始化和DLLMain的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆