2014-03-11 20:25:46 +0000 2014-03-11 20:25:46 +0000
50
50
Advertisement

从命令行关闭程序(Windows)

Advertisement

从命令行关闭/退出程序的正确方法是什么?

Im试图在3个不同版本的Windows下关闭Chrome:win7 ultimate, win7 home, win7 home, winXP

在Ultimate和XP下:TSKILL chrome

在Home下:TASKKILL /IM chrome.exe

TSKILL chrome

(It closes chrome, no cmd errors, but chrome error restore when relaunch it)

TASKKILL /IM chrome.exe:

/F:

(It closes chrome, no chrome errors when relaunch it, but errors in cmd: “终止子进程(约4-5个),只能用&007强行终止)

如果重启chrome时没有显示错误,我应该忽略cmd子进程错误吗?

Advertisement
Advertisement

答案 (5)

45
45
45
2014-03-11 21:51:34 +0000

关闭/退出程序的正确方法最终取决于软件。但是,一般来说,最好的做法是Windows程序在收到WM/CLOSE消息时就关闭。正确地释放内存和关闭句柄。还有一些其他的消息可以提示程序的关闭,但是如何处理每一条消息都是由软件的作者来决定的。你可能也想使用/T选项来给子进程发出信号。 只有当你想强制终止进程时,才会使用/F选项。

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

更新到更新的问题

忽略,错误. Chrome会产生很多进程。当一个进程不承认TASKKILL发送的WM/CLOSE消息时,就会产生错误。只有有消息循环的进程才能接收到消息,因此,没有消息循环的进程会产生该错误。最有可能的是,这些进程是chrome的扩展和插件。

要隐藏错误捕获输出

taskkill /IM process.exe

总结:TASKKILL是通过命令行来关闭应用程序的正确方法,根据它的WM_CLOSE实现和我链接的Microsoft KB

9
9
9
2014-03-11 21:21:03 +0000

试试NirCmd (下载:x86 x64)

它有 “closeeprocess "命令,是为了优雅地关闭进程而设计的。根据它的文档,它不会终止应用程序,而是向目标进程的所有顶层窗口发送WM_CLOSE

使用示例:

nircmd closeprocess chrome.exe

如果这个命令不起作用,我敢打赌你的应用程序有一个不寻常的清理程序。我想看看里面的情况,所以请告诉我你的目标程序是什么。

4
Advertisement
4
4
2014-03-11 23:22:09 +0000
Advertisement
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?

这个问题的答案可以在 here (Microsoft link)中找到。许多窗口处理`WMCLOSE`_来提示用户保存文档。

有一个工具可以正确地做到这一点,那就是WM_CLOSE


我不知道如何在批处理中做到这一点,但你可以用vbscript来做。模拟Alt+F4键(相当于信号@Kill)。

运行,看看下面这个脚本的行为。

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}"

WM_CLOSE 是SendKeys的列表键名。

当你运行这个脚本时,notepad是打开的,写了一些字,然后发出一个关闭程序的信号,见下图。


**附加问题*

*可以用vbscript最小化启动程序,或者后台启动程序吗? *

是的。使用以下代码:

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

更多信息请查看 Here . **

**我想只把alt+f4发送到chrome上,而不是其他窗口。

3
3
3
2014-03-11 21:18:50 +0000

有一些命令行工具可以向程序的顶层窗口发送一个合适的WM_SYSCOMMAND消息(用SC_CLOSE作为命令)。我相信很快就会有人提到至少一个。然后会有人提到AutoIt)。然后就会有答案,说明如何用PowerShell和CloseMainWindow()来做)

JP软件公司的TCC中,作为Windows的命令解释器和命令脚本处理器的命令行工具,它是作为一个内置的命令,叫做TASKEND

2
Advertisement
2
2
2014-03-12 01:37:30 +0000
Advertisement

好吧,不骗你。我在 stackoverflow 上看到这个问题,觉得这是个很有挑战性的问题。于是我花了两个小时写了一些代码。就是这样………..

按照下面的步骤,你可以在点击 “开始 "后输入 "TaskClose notepad.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
for /f "tokens=10 delims=," %%F in ('tasklist /v /fi "imagename eq %1" /fo csv') do @echo %%~F >>result.txt

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

  • 收集可执行文件。
  • 从任务列表中查询可执行程序名称。
  • 然后执行滚动命令,不管是 "Alt+F4",甚至在极端情况下
  • Alt+F4
  • 激活保存
  • 自动生成文件名
  • 退出应用程序。ReturnAppList.bat:安装在 "C:\windowssystem32 "中。AND "C:\Users\YourUserName”
C:\windows\system32\wscript.exe c:\windows\system32\taskclose.vbs %1

TaskClose.vbs : install in “C:\windowssystem32\” “` 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(applicationnamearray, 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(applicationnamearray, 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 “`

这是一个很有趣的写法,我很高兴完成它比实际显示答案更多。祝你有一个愉快的一周!

Advertisement

相关问题

3
19
10
28
7
Advertisement
Advertisement