delphi - 如何复制/替换 DLL?

标签 delphi dll delphi-xe2 access-denied file-copying

我有一个实用程序,可以通过简单地复制/替换可执行文件来更新应用程序。现在,我有一些 DLL 文件也需要更新。然而,有时 Windows 不会让我替换它,因为有东西正在使用它,有时有太多东西使用该 DLL,我无法保证它会被解锁以供我替换它。

目前,我唯一的解决方法是首先重新命名现有的 DLL,然后我可以将新的 DLL 复制到其位置。但随后旧的 DLL 会被留下,并且文件名已更改。

在这种情况下如何以编程方式替换 DLL?

最佳答案

你的方法很好 - 只需重命名该文件并将新的 DLL 复制到正确的位置即可。完成后,您可以使用Windows API函数MoveFileEx注册旧文件以便在下次机器重新启动时删除。来自 MSDN 文档:

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.

所以你会想做这样的事情:

MoveFileEx(szSrcFile, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);

我没有太多使用 Delphi 的工作。想必您可以导入正确的 Windows API 函数并直接从 Delphi 进行此调用,或者编写一个可以调用的小型 C++ 程序来为您处理此问题。

关于delphi - 如何复制/替换 DLL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14530252/

相关文章:

delphi - 从 TGridPanel 中删除控件

delphi - TImageList.ShareImages-如何准确使用而不必复制内容?

delphi - 如何检查包含我使用的 ActiveX 库的 dll 是否使用我的证书签名?

delphi - TIdHTTP : Check for valid cookies before sending request?

delphi - FireMonkey 有类似 GetSystemMetrics 的东西吗?

multithreading - Delphi并行处理字符串完全可用CPU使用率

wcf - 无法加载 DLL 'mqrt.dll'

c - FreeLibrary API 调用失败怎么办?

delphi - 如何在Delphi XE2中解释.otares文件?

multithreading - 为什么 WaitForMultipleObjects 在使用多个线程句柄时会失败?