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

标签 c++ windows dll winapi entry-point

我正在编写一个 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/

相关文章:

android - fatal error : iostream: No such file or directory

windows - 用于演示目的的 'read-only' 、 'non-removable' 应用程序安装日期的存储位置

c# - "Console"到底是什么?

windows - 如何在 Windows 命令行中执行 for 循环?

c# - 将连接字符串从 linqpad 传递到自定义库导致登录失败

visual-studio -/SUBSYSTEM :CONSOLE to/SUBSYSTEM:WINDOWS in a DLL 之间切换的影响

c++ - C++中多继承中使用的reinterpret_cast

c++ - 函数调用缺少参数列表警告

c++ - 在 OpenCV C++ 中将彩色图像中的像素 (i,j) 设置为黑色

dll - PCL 库和类似的 File.Exists 函数