Can I send a ctrl-C (SIGINT) to an application on Windows?

前端 未结 17 2350
甜味超标
甜味超标 2020-11-22 09:15

I have (in the past) written cross-platform (Windows/Unix) applications which, when started from the command line, handled a user-typed Ctrl-C combinat

相关标签:
17条回答
  • 2020-11-22 09:42

    A solution that I have found from here is pretty simple if you have python 3.x available in your command line. First, save a file (ctrl_c.py) with the contents:

    import ctypes
    import sys
    
    kernel = ctypes.windll.kernel32
    
    pid = int(sys.argv[1])
    kernel.FreeConsole()
    kernel.AttachConsole(pid)
    kernel.SetConsoleCtrlHandler(None, 1)
    kernel.GenerateConsoleCtrlEvent(0, 0)
    sys.exit(0)
    

    Then call:

    python ctrl_c.py 12345
    

    If that doesn't work, I recommend trying out the windows-kill project: https://github.com/alirdn/windows-kill

    0 讨论(0)
  • 2020-11-22 09:42

    Based on process id, we can send the signal to process to terminate forcefully or gracefully or any other signal.

    List all process :

    C:\>tasklist
    

    To kill the process:

    C:\>Taskkill /IM firefox.exe /F
    or
    C:\>Taskkill /PID 26356 /F
    

    Details:

    http://tweaks.com/windows/39559/kill-processes-from-command-prompt/

    0 讨论(0)
  • 2020-11-22 09:45

    It should be made crystal clear because at the moment it isn't. There is a modified and compiled version of SendSignal to send Ctrl-C (by default it only sends Ctrl+Break). Here are some binaries:

    (2014-3-7) : I built both 32-bit and 64-bit version with Ctrl-C, it's called SendSignalCtrlC.exe and you can download it at: https://dl.dropboxusercontent.com/u/49065779/sendsignalctrlc/x86/SendSignalCtrlC.exe https://dl.dropboxusercontent.com/u/49065779/sendsignalctrlc/x86_64/SendSignalCtrlC.exe -- Juraj Michalak

    I have also mirrored those files just in case:
    32-bit version: https://www.dropbox.com/s/r96jxglhkm4sjz2/SendSignalCtrlC.exe?dl=0
    64-bit version: https://www.dropbox.com/s/hhe0io7mcgcle1c/SendSignalCtrlC64.exe?dl=0

    Disclaimer: I didn't build those files. No modification was made to the compiled original files. The only platform tested is the 64-bit Windows 7. It is recommended to adapt the source available at http://www.latenighthacking.com/projects/2003/sendSignal/ and compile it yourself.

    0 讨论(0)
  • 2020-11-22 09:45

    A friend of mine suggested a complete different way of solving the problem and it worked for me. Use a vbscript like below. It starts and application, let it run for 7 seconds and close it using ctrl+c.

    'VBScript Example

    Set WshShell = WScript.CreateObject("WScript.Shell")
    
    WshShell.Run "notepad.exe"
    
    WshShell.AppActivate "notepad"
    
    WScript.Sleep 7000
    
    WshShell.SendKeys "^C"
    
    0 讨论(0)
  • 2020-11-22 09:46
            void SendSIGINT( HANDLE hProcess )
            {
                DWORD pid = GetProcessId(hProcess);
                FreeConsole();
                if (AttachConsole(pid))
                {
                    // Disable Ctrl-C handling for our program
                    SetConsoleCtrlHandler(NULL, true);
    
                    GenerateConsoleCtrlEvent(CTRL_C_EVENT, 0); // SIGINT
    
                    //Re-enable Ctrl-C handling or any subsequently started
                    //programs will inherit the disabled state.
                    SetConsoleCtrlHandler(NULL, false);
    
                    WaitForSingleObject(hProcess, 10000);
                }
            }
    
    0 讨论(0)
  • 2020-11-22 09:48

    Edit:

    For a GUI App, the "normal" way to handle this in Windows development would be to send a WM_CLOSE message to the process's main window.

    For a console app, you need to use SetConsoleCtrlHandler to add a CTRL_C_EVENT.

    If the application doesn't honor that, you could call TerminateProcess.

    0 讨论(0)
提交回复
热议问题