Catch Windows terminal closing on running process

前端 未结 3 1342
旧巷少年郎
旧巷少年郎 2020-12-20 03:26

In a command prompt window, I have a running process. While the process is still executing, I click the (red) \'X\' in the corner of the command prompt window. The command p

3条回答
  •  盖世英雄少女心
    2020-12-20 03:57

    This cannot be done directly from command prompt. You would need a secondary anything (vbs, powershell or custom MyApp.exe) to catch cmd.exe when it is closed and react accordingly.

    For instance, a VBS WMI monitor.

    strComputer = "."
    Set objSWbemServices = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!" & "\\" & strComputer & "\root\cimv2")
    Set objEventSource = objSWbemServices.ExecNotificationQuery( "SELECT * FROM __InstanceDeletionEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_Process' AND TargetInstance.Name = 'cmd.exe'")
    Set objEventObject = objEventSource.NextEvent()
    Wscript.Echo "CMD.exe closed"
    

提交回复
热议问题