简体   繁体   English

如何根据DLL方面的要求卸载DLL模块?

[英]How do I unload a DLL module upon a request from the DLL side to unload it?

I have a main program and a DLL library. 我有一个主程序和一个DLL库。 Both codes can be summarized as below. 这两个代码可以总结如下。

// DLL Module (Start)
class DllModuleClass : public IDllModuleInterFace
{
    public:
        // ...
        void Terminate() override
        {
            // ...
            pMainModuleObject->OnDllModuleObjectTerminated();   // Notify the main program that the DLL can be unloaded now.
            // Error occurs at this point (as soon as OnDllModuleObjectTerminated() returns), because it frees the DLL module and this region does not exist anymore.
        }
        // ...
    private:
        IMainModuleInterFace * pMainModuleObject;
}

IDllModuleInterFace * GetDllModuleClassInstance();
// DLL Module (End)

// Main Module (Start)
class MainModuleClass : public IMainModuleInterFace
{
    public:
        // ...
        void OnDllModuleObjectTerminated() override
        {
            FreeLibrary(hDllModule); // DLL module is freed from memory here.
        }   // Tries to go back to `Terminate()` inside the DLL module, but the DLL module is freed now.
        // ...
    private:
        IDllModuleInterFace * pDllModuleObject;
}
// Main Module (End)

In my code, the DLL module calls a function in the main module in order to notify the main module that the DLL can be unloaded. 在我的代码中,DLL模块调用主模块中的一个函数,以通知主模块可以卸载DLL。 The main module does so, unloads the DLL from the memory. 主模块会这样做,从内存中卸载DLL。 But the caller from the DLL hasn't return yet. 但是DLL的调用者尚未返回。 So, after unloading the DLL, there is still a function in the DLL which is still running. 因此,在卸载DLL之后,DLL中仍然有一个功能正在运行。 This causes an obvious and inevitable runtime error. 这会导致明显的和不可避免的运行时错误。

Can you suggest a proper way of unloading the DLL in this structure? 您能建议在这种结构中卸载DLL的正确方法吗?

为此有一个函数: FreeLibraryAndExitThread

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM