从命令行关闭程序(Windows)


49

从命令行关闭/退出程序的正确方法是什么,类似于按下窗口角的“ X”关闭按钮?

我试图在3种不同版本的Windows下关闭Chrome:Win7 Ultimate,Win7 Home,WinXP

在Ultimate和XP下: TSKILL chrome

在首页下TASKKILL /IM chrome.exe


TSKILL chrome

(它关闭chrome,没有cmd错误,
但是重新启动时chrome错误恢复


TASKKILL /IM chrome.exe

(它会关闭chrome,重新启动时不会出现chrome错误,
但会在cmd中显示错误:“无法强制终止子进程(大约4-5),只能通过强制使用/F“)。


如果在重新启动chrome时我没有显示任何错误,我应该忽略cmd子错误?


@MosheKatz:您能给我看看一些退出代码的例子吗?我想知道我什么时候打的X按钮会发生什么..
neoDev

6
除非指定了/ F(强制终止),否则Taskkill会将WM_CLOSE消息发送到目标程序,以使程序有机会自行清理。使用记事本进行测试-打开记事本,然后在其中键入内容,然后输入“ taskkill / im notepad.exe”。您将看到提示以保存数据。
Scott Rhee 2014年

@ScottRhee:我已经尝试在没有/ F的情况下运行它,但是似乎关闭不当...我错了吗?
neoDev 2014年

@ScottRhee:我也尝试使用Chrome!有时候,当我重新启动它说,在类似这样的URL下一条黄线“恢复选项卡”:backroom.bostonproductions.com/wp-content/uploads/2012/07/...
neoDev

@Roger这很奇怪,因为我没有通过Taskkill / IM chrome.exe收到该消息。无论如何,我相信您做对了事。我在下面写了一个答案,所以请尝试尝试。
Scott Rhee 2014年

Answers:


43

关闭/退出程序的正确方法最终取决于软件。但是,通常,最佳实践是Windows程序在收到WM_CLOSE消息时将其关闭。正确释放内存并关闭手柄。还有其他消息可以表明应用程序已关闭,但是软件的作者取决于如何处理每个消息。

taskkill /IM process.exe

taskkill发送WM_CLOSE消息,然后由应用程序决定是否正确关闭。您可能还希望使用该/T选项来发信号通知子进程。

仅在/F要强制终止过程时才使用该选项。

其他选项包括使用PowerShell或第三方应用程序发送Alt + F4密钥。

更新到更新的问题

忽略错误。 Chrome会生成许多进程。当进程不确认TASKKILL发送的WM_CLOSE消息时,将导致错误。只有具有消息循环的进程才能接收消息,因此,没有消息循环的进程将生成该错误。这些过程很可能是chrome扩展程序和插件。

隐藏错误以捕获输出

taskkill /IM chrome.exe >nul

摘要: TASKKILL是通过命令行根据其WM_CLOSE实现和我链接Microsoft KB关闭应用程序的正确方法。


我更新了我的问题,请查看一下
neoDev 2014年

@Roger更新了我的答案。
大卫·鲁曼

> nul是什么意思?
neoDev 2014年

10
> cmd.exe 的重定向运算符,并且nul是空间,永远不会再听到任何声音。
大卫·鲁曼

9

试试NirCmd(下载:x86 x64

它具有“ closeprocess”命令,该命令旨在优雅地关闭进程。根据其文档,它不会终止应用程序,但会发送WM_CLOSE到目标进程的所有顶级窗口。

用法示例:

nircmd closeprocess chrome.exe

如果这不起作用,我敢打赌您的应用程序具有异常的清理程序。我想看看里面发生了什么,所以请让我知道您的目标应用程序是什么。


我更新了我的问题,请查看一下
neoDev 2014年

我发现这更好用:nircmd win close标题“我的进程标题”
user1495323

4
What is the proper way to close/exit programs from command line,
similar to pressing the "X" close button in the corner of the window?

可以找到该问题的答案hereMicrosoft链接)。

您可以将WM_CLOSE消息发送到要关闭的任何窗口。许多窗口WM_CLOSE会提示用户保存文档。

正确执行此操作的工具是@Kill。还要查看SendMsg


我不知道如何批量执行此操作,但是您可以为此使用vbscript。模拟Alt+ F4键(等于signal WM_CLOSE)。

运行并在下面查看此脚本的行为。

Set WshShell = WScript.CreateObject("WScript.Shell")

' Start Notepad.exe
WshShell.Run "%windir%\notepad.exe"

' Select, or bring Focus to a window named `Notepad`
WshShell.AppActivate "Notepad"

' Wait for 4 seconds
WScript.Sleep 4000

WshShell.SendKeys "Foo"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "Bar"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys "{CAPSLOCK}"
WshShell.SendKeys "baz"
WshShell.SendKeys "%{F4}"

Here 是SendKeys的列表键名称。

运行脚本时,记事本会打开,会写一些单词,然后会发出关闭程序的信号,请参见下图。


其他问题

我可以启动最小化的程序,还是可以使用vbscript启动背景?

是。使用以下代码:

WshShell.Run "%windir%\notepad.exe", 2

有关更多信息,请检查Run Method

chrome可以访问vbscript中的某些网址吗?

Dim iURL  
Dim objShell iURL = "www.google.com"
set objShell = CreateObject("Shell.Application") 
objShell.ShellExecute "chrome.exe", iURL, "", "", 1

如果默认为chrome,请使用:

set objShell = CreateObject("WScript.Shell")
objShell.run(iURL)

Source

有没有一种方法可以将焦点转移到特定的应用程序(chrome.exe)?

Here 在一个例子中。

我想只将alt + f4发送到chrome,独立于我正在处理其他Windows。

以下代码在Windows 8上有效

Dim objShell

iURL = "www.google.com.br"

Set WshShell = WScript.CreateObject("WScript.Shell")

WshShell.Run(iURL)

' Select, or bring Focus to Google Chrome
WshShell.AppActivate "Google Chrome"

' Wait for 5 seconds
WScript.Sleep 5000

WshShell.SendKeys "%{F4}"

我可以启动最小化的程序,还是可以使用vbscript启动背景?
neoDev 2014年

1
@Roger如果有兴趣,请参阅此处关于如何将Batch + JScript组合到单个文件中的答案。JScript可以用VBScript可以做的大多数事情,并且可以使用结构更清晰的C样式。
大卫·鲁曼

chrome可以在vbscript中转到某些URL吗?
neoDev 2014年

有没有一种方法可以将焦点转移到特定应用程序(chrome.exe)?
neoDev 2014年

是的,我正在尝试使用它,但是如果经过一段时间的延迟(WScript.Sleep 5000)我有(WshShell.AppActivate“ google chrome”),如果在此期间我单击其他某个窗口,则google chrome不会出现在前面,我可以看到它的焦点只在工具栏上....
neoDev

3

有一些命令行实用程序可以将合适的WM_SYSCOMMAND消息(SC_CLOSE作为命令发送)到程序的顶级窗口。我敢肯定,至少会在短期内提及。(然后有人会提到AutoIt。然后会有一个答案,显示如何使用PowerShell和进行操作CloseMainWindow()。)

作为JP Software的TCC(Windows的命令解释器和命令脚本处理器)中的内置命令提供的命令行实用程序称为TASKEND


我更新了我的问题,请查看一下
neoDev 2014年

2

好吧,不要说谎。我在stackoverflow上看到了这一点,并认为这是一个具有挑战性的问题。如此,我刚刚花了最后两个小时来编写一些代码。这里是...

按照以下步骤操作后,您可以在单击“开始”后键入“ TaskClose notepad.exe”,它将自动将所有未记录的记事本文件保存到桌面。它将自动关闭chrome.exe并保存恢复设置。

您可以在if条件下为其他应用程序添加和删除其他设置。例如:

If InStr(SINGLECLOSEAPPS, WScript.arguments(0)) Then
    SmartSendKeys strOutput,"bypass","%{F4}"

ElseIf InStr(AUTOCLOSEAPPS, WScript.arguments(0)) Then
    SmartSendKeys strOutput,"","%{F4}|%s|{autoname}.txt|%s"

'Example 1: =============================================
ElseIf InStr(WScript.arguments(0), "outlook") Then
    SmartSendKeys strOutput,"","%{F4}" 'Activate Alt+4
'========================================================

'Example 2: =============================================
ElseIf InStr(WScript.arguments(0), "notepad") Then 'I know, already in my autoapps
    SmartSendKeys strOutput,"","%{F4}|%s|{autosave}.txt" 'Activate Alt+4 + Save File + {AutoSave} = autoincrement filename
'========================================================

Else
    SmartSendKeys strOutput,"bypass","%{F4}"
End If

vbs和批处理文件执行以下过程:

  • 收集可执行文件。
  • 从任务列表中查询可执行应用程序的名称。
  • 执行“ Alt + TAB(x)”过程,直到确认窗口已打开。
  • 然后执行滚动命令,无论是“ Alt + F4”还是在极端情况下
  • Alt + F4
  • 激活保存
  • 自动提示文件名
  • 退出申请。

ReturnAppList.bat:安装在“ C:\ windows \ system32 \”中

for /f "tokens=10 delims=," %%F in ('tasklist /v /fi "imagename eq %1" /fo csv') do @echo %%~F >>result.txt

TaskClose.bat:安装在“ C:\ windows \ system32 \”和“ C:\ Users \ YourUserName \”中

C:\windows\system32\wscript.exe c:\windows\system32\taskclose.vbs %1

TaskClose.vbs:安装在“ C:\ windows \ system32 \”中

Set WshShell = CreateObject("WScript.Shell")
Const SINGLECLOSEAPPS = "chrome.exe|iexplore.exe|firefox.exe"
Dim DEFAULTSAVELOCATION : DEFAULTSAVELOCATION = wshshell.SpecialFolders("Desktop")
Const AUTOCLOSEAPPS = "notepad.exe"

Const WshFinished = 1
Const WshFailed = 2
strCommand = "returnapplist.bat "
Set WshShellExec = WshShell.Exec(strCommand & WScript.Arguments(0))

WScript.sleep 2000

Select Case WshShellExec.Status
    Case WshFinished
    strOutput = LoadStringFromFile("result.txt")
    Case WshFailed
    strOutput = LoadStringFromFile("result.txt")
End Select


'SmartSendKeys(application_name_array, bypassclause, additionalcommands)
'========================
If InStr(SINGLECLOSEAPPS, WScript.arguments(0)) Then
    SmartSendKeys strOutput,"bypass","%{F4}"
ElseIf InStr(AUTOCLOSEAPPS, WScript.arguments(0)) Then
    SmartSendKeys strOutput,"","%{F4}|%s|{autoname}.txt|%s"
Else
    SmartSendKeys strOutput,"bypass","%{F4}"
End If




'SmartSendKeys(application_name_array, bypassclause, additionalcommands)
'========================
Function SmartSendkeys(fArr, LoopCount, RollCommands) 
    Dim x 
    Dim splt : splt = Split(farr, vbCrLf)
    If loopcount = "bypass" Then 
        x = 0
    Else
        x = UBound(splt)
    End If
    a = 0
    For s=0 To x
        If Len(splt(s)) > 1 Then
            Set objShell = WScript.CreateObject("WScript.Shell")
            c = 1 : tabs = ""
            Success = False
            Do Until Success = True
                Success = objShell.AppActivate(Trim(splt(s)))
                If success <> True Then
                    If c = 1 Then 
                        tabs = "{TAB}"
                    Else
                        tabs = "{TAB " & c & "}"
                    End If
                    'wscript.echo "Activating: " & "%" & tabs
                    WshShell.SendKeys "%" & tabs
                    WScript.Sleep 5000
                    c = c + 1
                    If c = 100 Then 
                        WScript.echo "App not found"
                        Exit Function
                    End If
                End If
            Loop
            Dim cmds : cmds = Split(rollcommands, "|")

            For Each cm In cmds
                If InStr(cm, "{autoname}") Then
                    Dim file_ext : file_ext = Split(cm, ".") 
                    cm = DEFAULTSAVELOCATION & "autosave" & a & "." & file_ext(1)
                    a = a + 1
                End If
                WshShell.SendKeys cm
                WScript.sleep 500

            Next
        End If
    Next
End Function

Function LoadStringFromFile(filename)
    Const fsoForReading = 1
    Const fsoForWriting = 2
    Dim fso, f
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set f = fso.OpenTextFile(filename, fsoForReading)
    Dim fulltext : fulltext = f.ReadAll
    LoadStringFromFile = fulltext
    f.Close
    fso.DeleteFile(filename)
End Function

这写起来很有趣,而且我对完成它比实际显示答案更高兴。祝你有美好的一周!

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.