gpt4 book ai didi

c++ - DLL 中的全局构造函数和 MSVCRT

转载 作者:太空狗 更新时间:2023-10-29 23:07:07 27 4
gpt4 key购买 nike

DLL 中的以下代码会发生什么情况?

#include <vector>
std::vector<int> global_vector;

BOOL WINAPI DllMain(HANDLE hModule, DWORD fdwReason, LPVOID lpReserved)
{
if (fdwReason == DLL_PROCESS_ATTACH)
{
global_vector.push_back(1);
}

return TRUE;
}

...如果使用/MD 编译? vector 的构造函数和 push_back 调用的代码(即内存管理、异常处理...)位于 MSVCRT.DLL... 据我所知,在这个阶段不能保证被映射。它是否得到特殊对待?

最佳答案

来自 here :

In a nutshell, when DllMain is called, OS loader is in a rather fragile state. First off, it has applied a lock on its structures to prevent internal corruption while inside that call, and secondly, some of your dependencies may not be in a fully loaded state. Before a binary gets loaded, OS Loader looks at its static dependencies. If those require additional dependencies, it looks at them as well. As a result of this analysis, it comes up with a sequence in which DllMains of those binaries need to be called. It's pretty smart about things and in most cases you can even get away with not following most of the rules described in MSDN - but not always.

关于c++ - DLL 中的全局构造函数和 MSVCRT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13557855/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com