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

前端 未结 17 2683
甜味超标
甜味超标 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: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);
                }
            }
    

提交回复
热议问题