在 Windows 上重命名正在运行的进程的文件映像 [英] Renaming A Running Process' File Image On Windows

查看:35
本文介绍了在 Windows 上重命名正在运行的进程的文件映像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Vista SP1 上有一个 Windows 服务应用程序,我发现用户正在重命名其可执行文件(在它运行时)然后重新启动,从而导致它在下次启动时无法启动,因为服务管理器无法再启动找到重命名的 exe 文件.

I have a Windows service application on Vista SP1 and I've found that users are renaming its executable file (while it's running) and then rebooting, thus causing it to fail to start on next bootup because the service manager can no longer find the exe file since it's been renamed.

我似乎记得在旧版本的 Windows 中你不能这样做,因为操作系统锁定了文件.即使使用 Vista SP1,我仍然无法在运行时复制现有文件 - Windows 报告该文件正在使用 - 是有道理的.那么为什么要允许我重命名它呢?如果 Windows 需要从 exe 调入新的代码页,但该文件自启动后已被重命名,会发生什么情况?我在重命名 exe 文件等时运行了 Process Monitor,但 Process Mon 没有报告任何奇怪的事情,只是像任何其他文件一样记录更改文件名.

I seem to recall that with older versions of Windows you couldn't do this because the OS placed a lock on the file. Even with Vista SP1 I still cannot copy over the existing file when it's running - Windows reports that the file is in use - makes sense. So why should I be allowed to rename it? What happens if Windows needs to page in a new code page from the exe but the file has been renamed since it was started? I ran Process Monitor while renaming the exe file, etc, but Process Mon didn't report anything strange and just logged changing the filename like any other file.

有人知道幕后发生了什么吗?Windows 允许更改正在运行的进程的文件名(或其依赖的 DLL),这似乎违反直觉.我在这里错过了什么?

Does anyone know what's going on here behind the scenes? It's seem counter intuitive that Windows would allow a running process' filename (or its dependent DLLs) to be changed. What am I missing here?

推荐答案

你的概念是错误的...文件名不是 file-io 世界的中心...打开文件的句柄是.当您重命名文件时,该文件不会移动到磁盘的不同部分,它仍然在同一个地方,并且磁盘的一部分,打开文件的内部数据结构仍然指向同一个地方.底线是你的观察是正确的.您可以重命名正在运行的程序而不会引起问题.重命名后,您可以创建一个与正在运行的程序同名的新文件.如果您想在软件运行时更新软件,这实际上是很有用的行为.

your concept is wrong ... the filename is not the center of the file-io universe ... the handle to the open file is. the file is not moved to a different section of disk when you rename it, it's still in the same place and the part of the disk the internal data structure for the open file is still pointing to the same place. bottom line is that your observations are correct. you can rename a running program without causing problems. you can create a new file with the same name as the running program once you've renamed it. this is actually useful behavior if you want to update software while the software is running.

这篇关于在 Windows 上重命名正在运行的进程的文件映像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆