Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I copy/replace a DLL?

I have a utility which updates applications by simply copying/replacing the executables. Now, I have some DLL files which need to be updated as well. However, sometimes Windows will not let me replace it because something is using it, and sometimes there's so many things using the DLL that I cannot guarantee it will be unlocked for me to replace it.

Currently, my only work-around is to re-name the existing DLL first, and then I can copy the new one in its place. But then the old DLL gets left behind with an altered file name.

How can I replace a DLL programatically in this situation?

like image 777
Jerry Dodge Avatar asked Jan 25 '13 20:01

Jerry Dodge


People also ask

Can I replace DLL files?

It is possible to replace a DLL that is in use. The method you use to replace DLLs that are in use depends on the operating system you are using.


1 Answers

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

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

So you would want to do something like:

MoveFileEx(szSrcFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);

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

like image 53
Justin Ethier Avatar answered Oct 28 '22 10:10

Justin Ethier