How to copy / replace dll?

I have a utility that updates applications by simply copying / replacing executable files. Now I have some DLL files that also need to be updated. However, sometimes Windows will not allow me to replace it, because something uses it, and sometimes so many things using DLLs that I cannot guarantee will be unlocked for me to replace it.

Currently, my only job is to rename the existing DLL first, and then I can copy the new one in its place. But then the old DLL is left behind with the changed file name.

How can I replace the DLL programmatically in this situation?

+8
dll delphi delphi-xe2 access-denied file-copying
source share
1 answer

Your method is fine - just rename the file and copy the new DLL to the desired location. Once this is done, you can use the Windows API MoveFileEx function to register the old file for deletion the next time the machine starts. From the MSDN documentation:

If dwFlags specifies MOVEFILE_DELAY_UNTIL_REBOOT and lpNewFileName is NULL, MoveFileEx registers the lpExistingFileName file, which will be deleted when the system restarts. If lpExistingFileName refers to a directory, the system deletes the directory upon restart only if the directory is empty.

So you would like to do something like:

MoveFileEx(szSrcFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT); 

I did not work much with Delphi. Presumably, you can either import the proper Windows API functions, or make this call directly from Delphi, or write a small C ++ program that you can call to take care of this for you.

+14
source share

All Articles