110,538
社区成员
发帖
与我相关
我的任务
分享
[DllImport("kernel32.dll")]
static extern bool GenerateConsoleCtrlEvent(int dwCtrlEvent, int dwProcessGroupId);
[DllImport("kernel32.dll")]
static extern bool SetConsoleCtrlHandler(IntPtr handlerRoutine, bool add);
[DllImport("kernel32.dll")]
static extern bool AttachConsole(int dwProcessId);
[DllImport("kernel32.dll")]
static extern bool FreeConsole();
// 使用时
AttachConsole(processId); // 附加到目标进程的console
SetConsoleCtrlHandler(IntPtr.Zero, true); // 设置自己的ctrl+c处理,防止自己被终止
GenerateConsoleCtrlEvent(0, 0); // 发送ctrl+c(注意:这是向所有共享该console的进程发送)
FreeConsole(); // 脱离目标console