Home > Software engineering >  How to remove yourself when DLL_PROCESS_DETACH LoadLibrary DLL?
How to remove yourself when DLL_PROCESS_DETACH LoadLibrary DLL?

Time:11-02

I wrote a PythonRuntime. DLL, the Python Embedded runtime library, or call to streamline the green version of Python, use 7 zip package as resources Embedded in DLL,

In Python released when DLL_PROCESS_ATTACH. 7 z to the temporary directory PyTemp, remove the temporary directory: when DLL_PROCESS_DETACH cleanly exits,

Otherwise an own python scripts, put it to use Cython into MyExt. Pyd, then in PythonRuntime. Dll MyDllFun () function is derived using the python C - API adjustable MyExt. Pyd, in this way, the python interpreter will adjust LoadLibrary (hMyExt. Dll),

All this running very well, but by the DLL_PROCESS_ATTACH delete temporary directory PyTemp, there is only one file delete not to drop, I own extension MyExt pyd, added FreeLibrary (hMyExt. Pyd) also return a success, but is can't delete, wait a few seconds and then delete also not line, infinite loop FreeLibrary dozens of times also not line, of course FreeLibrary (), and delete operations are Py_Finalize (),

Although Python PEP 3121 inside speak "Currently, the extension modules are initialized usually once and then" live "forever.", but when I was in DLL_PROCESS_ATTACH perform release of Python. 7 z, call MyExt. DLL, Py_Finalize (), FreeLibray (hMyExt. DLL). After a series of operations that can be deleted MyExt pyd,

Refer to the inside of the "Windows core programming", DLL Detach don't guarantee the order, but my own MyExt. Pyd should always in Kernal32. DLL before? Why can't FreeLibrary after many times or remove?

Finally make a temporary solution: require exe exit calls ExitProcess (), when I was in DLL_PROCESS_ATTACH hook their exe ExitProcess (), after the hook to the first FreeLibrary (hMyExt. Pyd), then adjust Kernal32. DLL ExitProcess (), so that it can be deleted, also considered on its own PythonRuntime. DLL add a clear function inside, but not as good as the above scheme,


note 1: the operating environment: Python 3.5.1 track of, VS2015 out, Win10

CodePudding user response:

MoveFileEx

CodePudding user response:

reference 1st floor zhao4zhong1 response:
MoveFileEx

MOVEFILE_WRITE_THROUGH returns TRUE, on the disk file name is changed, but still delete not to drop, adjustable manually delete or delete not to drop, also after a file is by XXX. Exe (my own DLL). The caller is used

CodePudding user response:

Correction: the fourth paragraph, "but the DLL_PROCESS_ATTACH delete temporary directory" should be changed to "but by the DLL_PROCESS_DETACH delete temporary directory" "

CodePudding user response:

Only for reference: http://bbs.csdn.net/topics/390968755
  • Related