如何释放未运行的 exe 引用的 DLL?

发布于 2024-09-24 19:54:00 字数 255 浏览 6 评论 0原文

我有一个依赖 DLL 的 Windows 服务。无论服务是否正在运行,其可执行文件都会保留该 DLL 的句柄,这会阻止我自动更新它。

为什么会这样?如何以编程方式删除该句柄(最好通过 .Net)?

更新: 我知道服务的可执行文件是带有句柄的可执行文件,因为我在 dll 上运行了handle.exe,并且只出现了服务的 exe。当我在 exe 上运行它时,没有任何反应,并且我知道该服务没有运行,因为我检查了服务列表。我能想到的没有其他可以使用该 dll 的东西。

I've got a windows service that relies on a DLL. Whether the service is running or not, its executable is keeping a handle to that DLL, which is preventing me from updating it automatically.

Why is this, and how can I remove that handle programatically (preferably via .Net)?

Update:
I know that the service's executable is the one with the handle because I ran handle.exe on the dll and only the service's exe came up. Nothing cane up when I ran it on the exe and I know that the service isn't running because I checked the services list. There is nothing else that would be using the dll that I can think of.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一抹微笑 2024-10-01 19:54:00

这是不可能的。只有正在运行的进程才能拥有文件的句柄。

您可以使用 Process Explorer 找出哪个进程拥有该进程的句柄DLL 打开:

  1. 启动 Process Explorer
  2. 按 Ctrl+F 并搜索 DLL 名称
  3. 等待搜索完成
  4. 您将看到哪些进程拥有该 DLL 的打开句柄。

This is not possible. Only a running process can have a handle to a file.

You can use Process Explorer to find out which process has a handle to the DLL open:

  1. Start Process Explorer
  2. Press Ctrl+F and search for the DLL name
  3. Wait for the search to complete
  4. You will see which process(es) have an open handle to the DLL.
予囚 2024-10-01 19:54:00

DLL 被加载并映射到虚拟内存中。这会锁定文件。启动或停止服务不会改变这一点。对于服务 EXE 来说也是如此。

您必须动态加载和卸载 DLL 才能解决此问题。 LoadLibrary 和 FreeLibrary。但只有当它是非托管 DLL 时才有效。如果你使用pinvoke的话是非常危险的,因为CLR在你卸载DLL后不会自动再次加载它。

如果它是托管 DLL,则只有在另一个 AppDomain 中加载它时才能卸载它。也没有称心如意。

The DLL was loaded and mapped into virtual memory. That puts a lock on the file. Starting or stopping the service doesn't change that. The exact same is true for the service EXE.

You would have to dynamically load and unload the DLL to fix this. LoadLibrary and FreeLibrary. But that only works if it is an unmanaged DLL. And is very risky if you use pinvoke because the CLR won't automatically load the DLL again after you unloaded it.

If it is a managed DLL that you can only unload it if it was loaded in another AppDomain. No bed of roses either.

不如归去 2024-10-01 19:54:00

没有办法让 EXE 从另一个进程中删除 DLL 的句柄。它几乎肯定有句柄,因为该 DLL 正在被进程主动使用。唯一可用的选项是从服务中终止 EXE 进程。这可以通过 Process.Kill 方法来完成。

尽管在编程世界中杀死不属于您的其他进程通常是不好的行为。为什么你的程序比他们的好?

There is no way to have an EXE drop a handle to a DLL from another process. It almost certainly has the handle because the DLL is being actively used by the process. The only option available really is to kill the EXE process from the service. This can be done via the Process.Kill method.

Although killing other processes you don't own is generally speaking bad behavior in the programming world. Why is your program better than theirs?

紫南 2024-10-01 19:54:00

这是一个令人讨厌的小问题。该服务有两个线程,其中一个线程执行一个非常微妙的、不经常需要的功能,而且我没有注意到它在共享资源上陷入了死锁。因此,它无限期地挂起,这就是尽管服务已关闭,但我的可执行文件仍然处于运行状态的原因。解决该问题解决了我的文件处理问题。

谢谢大家的建议。

This was a nasty little problem. The service has two threads, and one of them performs a very subtle, not frequently needed function, and I did not notice that it was getting deadlocked over a shared resource. So it was hanging, indefinitely, which is what was keeping my executable engaged, despite the service being shut down. Fixing that issue resolved my file handle issue.

Thanks for the suggestions, all.

凉城已无爱 2024-10-01 19:54:00

当人们创建长时间运行的 Windows 服务进程,然后投入大量时间来解决它所产生的问题时,我总是会遇到这样的问题。

如果可能的话,您能考虑使用 Windows 任务计划程序吗?您可以在这里看到原因..http://pavangayakwad.blogspot .com/2010/09/windows-service-or-windows-task.html

I always here such problems when people create long running windows service process and then invest great time in resolving problems it create.

If possible, can you think of using Windows Task Scheduler for this? You see here why..http://pavangayakwad.blogspot.com/2010/09/windows-service-or-windows-task.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文