如何修复我的“自我删除”问题.exe 会提前删除自身吗?
为了测试和个人概念验证,我有一个 .exe 文件,它仅输出一个简单的字符串,然后调用系统暂停(在 C++ 中字面意思是 system("pause")
)。
我有一个简单的 Python 脚本,正在 Windows XP 虚拟机上测试,该脚本在出现混乱时执行这些操作:
subprocess.call(r'echo Nothing special. > c:\blank.txt', shell=True)
subprocess.call(r'type pause.exe > c:\blank.txt:ads.exe', shell=True)
subprocess.call(r'start c:\blank.txt:ads.exe', shell=True)
subprocess.call(r'del c:\blank.txt', shell=True)
显然,这些命令单独在命令行上都可以正常工作,为什么在通过 Python 调用时它们不能正常工作?
我收到此弹出错误消息:
blank.txt:ads.exe 遇到了 问题并需要关闭。我们是 很抱歉给您带来不便。
如果你正处于 某事,你的信息 正在进行的工作可能会丢失。
该文件也确实被删除了。看来系统暂停只是被删除命令压垮了,当我期望弹出.exe时,等我按回车键,然后脚本将继续并删除文件。
For the sake of testing and personal proof of concept, I have a .exe file that only outputs a simple string and then calls a system pause (literally system("pause")
in C++).
I have a simple Python script I'm testing on a Windows XP VM that does these operations when messing up:
subprocess.call(r'echo Nothing special. > c:\blank.txt', shell=True)
subprocess.call(r'type pause.exe > c:\blank.txt:ads.exe', shell=True)
subprocess.call(r'start c:\blank.txt:ads.exe', shell=True)
subprocess.call(r'del c:\blank.txt', shell=True)
Obviously, those commands all work fine alone on the command line, why don't they work fine when called through Python?
I receive this pop-up error message:
blank.txt:ads.exe has encountered a
problem and needs to close. We are
sorry for the inconvenience.If you were in the middle of
something, the information you were
working on might be lost.
The file is indeed deleted, also. It seems that the system pause is just crushed by the delete command, when I expect the .exe to pop up, wait for me to push enter, and then the script will continue and delete the file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鉴于这些症状,我的理解是,只有在该可执行文件完成初始化后,您才能安全地删除此特定设置中的可执行文件(Windows XP,可能在特定补丁级别,当可执行文件来自备用流时)。如果在加载时删除可执行文件,程序就会崩溃。
当您在提示符处键入这些命令时,运行
start c:\blank.txt:ads.exe
和del c:\blank.txt
之间会经过一段时间,给出程序有足够的时间完成加载。当您从脚本运行它们时,两者之间的间隔要短得多(start
分支出一个新进程,并且新程序的初始化异步发生)。初始化和删除之间存在竞争条件;哪一个获胜取决于执行删除的时间。尝试在删除文件之前尝试延迟:
Given the symptoms, my understanding is that you can only safely delete an executable in this particular setting (Windows XP, perhaps at a particular patch level, when the executable is from an alternate stream) once this executable has finished initializing. If you delete an executable while it's loading, the program crashes.
When you type these commands at a prompt, some time elapses between running
start c:\blank.txt:ads.exe
anddel c:\blank.txt
, giving the program enough time to finish loading. When you're running them from a script, the interval between the two is a lot shorter (start
branches off a new process, and the new program's initialization happens asynchronously). There's race condition between the initialization and the deletion; which one wins depends on how soon the deletion is performed.Try experimenting with a delay before deleting the file:
发生的情况是,每个 subprocess.call(..., shell=True) 都会立即返回,因为它们只是告诉 shell 执行命令。在命令行上,使用
start
命令调用 exe 运行仍会立即返回,即使 exe 尚未终止。start
立即返回,需要告诉它等待:What happens is that each
subprocess.call(..., shell=True)
is immediately returning because they're just telling the shell to execute a command. On the commandline, calling an exe to run using thestart
command will still immediately return even though the exe has not terminated. Thestart
immediately returned and it needs to be told to wait:Windows shell
start
命令将在新的 shell 中启动程序,然后继续。要等待它完成,请使用call
:The windows shell
start
command will launch the program in a new shell and then just continue. To wait for it to complete usecall
: