How can I copy/replace a DLL?
Asked Answered
S

1

8

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?

Skewbald answered 25/1, 2013 at 20:56 Comment(13)
Or I could require a restart and copy the files upon startup...Skewbald
Have you used Process Explorer to verify how the DLL is being "locked"?Feathers
https://mcmap.net/q/1325647/-how-to-delete-a-file-that-is-using-by-windows/62576 might be of use.Assassinate
Once no process has the DLL loaded, you can overwrite it. You got that part wrong.Cloudy
@DavidHeffernan Strange because I've always had this issue.Skewbald
You are mis-diagnosing it. Create an empty DLL and then run a process which calls LoadLibrary on that DLL. Test it out for yourself.Cloudy
@DavidHeffernan The issue is actually with Indy DLL's, libeay32 and ssleay32 become locked after using Indy with them. I've also had the issue with DLL's in the System32 folder.Skewbald
If all processes unload it, you can delete the DLL. It's just that simple.Cloudy
@DavidHeffernan Understood, but sometimes you have a DLL which is used so much by so many applications that you can never guarantee 100% of them would release it.Skewbald
I thought we were talking about your DLLs. Anyway, I've made my point. DLLs aren't locked when nothing has them loaded.Cloudy
@Jerry, to unload SSL libraries used by Indy, you can call the UnLoadOpenSSLLibrary procedure from the IdSSLOpenSSL.pas unit.Ivan
I modified my question to take this into account. I didn't mention Indy explicitly because it's not the only thing I have this problem with. I'm seeking a solution which will accommodate for any DLL with this scenario.Skewbald
@Jerry, I see. That was just a sidenote ;-)Ivan
C
14

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.

Chiaroscuro answered 25/1, 2013 at 21:7 Comment(1)
+1. This question might help regarding the Delphi component. It's Windows unit contains the declaration already; it's generally included in most apps either by default or by design because API calls are used.Assassinate

© 2022 - 2024 — McMap. All rights reserved.