gpt4 book ai didi

c++ - 为什么不从入口点函数调用 FreeLibrary?

转载 作者:可可西里 更新时间:2023-11-01 13:54:10 25 4
gpt4 key购买 nike

我正在编写一个 DLL,需要多次动态调用一个单独的 DLL。我想保持被调用者加载,然后在我的 DLL 卸载时卸载它。但根据微软的说法,这是一个 bad idea .

The entry point function should only perform simple initialization tasks and should not call any other DLL loading or termination functions. For example, in the entry point function, you should not directly or indirectly call the LoadLibrary function or the LoadLibraryEx function. Additionally, you should not call the FreeLibrary function when the process is terminating.

这是有问题的代码。有人可以解释为什么我不应该从 DLL 的入口点调用 LoadLibrary 和 FreeLibrary 吗?

BOOL APIENTRY DllMain( HANDLE hModule, 
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call) {
case DLL_PROCESS_DETACH :
if (hLogLib != NULL) FreeLibrary(hLogLib);
break;
}
return TRUE;
}

最佳答案

我想我找到了 the answer .

The entry-point function should perform only simple initialization or termination tasks. It must not call the LoadLibrary or LoadLibraryEx function (or a function that calls these functions), because this may create dependency loops in the DLL load order. This can result in a DLL being used before the system has executed its initialization code. Similarly, the entry-point function must not call the FreeLibrary function (or a function that calls FreeLibrary) during process termination, because this can result in a DLL being used after the system has executed its termination code.

关于c++ - 为什么不从入口点函数调用 FreeLibrary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1903008/

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