DLL unloading itself

后端 未结 4 1397
不思量自难忘°
不思量自难忘° 2020-12-03 16:33

Is it possible for a function that is inside a DLL to unload the DLL? I need to do this so I can make sure the DLL is not in use, then write to the DLL\'s file.

4条回答
  •  误落风尘
    2020-12-03 16:51

    If your asking if you can safely unload/unmap a DLL loaded in a process from code in the DLL itself, the answer is no - there isn't really a safe way to do this.

    Think about it this way: Unloading a DLL is done by decrementing it's reference count using FreeLibrary(). The problem of course is that once the reference count of the DLL hits zero, the module is unmapped. Which means that the code in the DLL that called FreeLibrary() is gone.

    Even if you could do this, you'd still need to ensure that there are no other threads executing any exported functions from the DLL.

提交回复
热议问题