如何将 Ctrl+C 信号发送到使用 CreateNoWindow 标志创建的控制台进程

发布于 2024-11-28 15:07:08 字数 294 浏览 1 评论 0原文

如果我使用 Process.Kill(),该进程将被终止。但是,我想终止它。

我尝试使用 GenerateConsoleCtrlEvent(ConsoleCtrlEvent.CTRL_C, Process.Id) API,但没有成功。

如果我将 False 设置为 CreateNoWindow 标志,当我从键盘发送 Ctrl+C 时,程序会显示“捕获信号:2;终止”。因此它等待“2”信号来终止。

我怎样才能做到这一点?

If I use Process.Kill(), the process is killed. However, I would like to terminate it.

I tried with GenerateConsoleCtrlEvent(ConsoleCtrlEvent.CTRL_C, Process.Id) API, without success.

If I set False to CreateNoWindow flag, when I send Ctrl+C from keyboard, the program says "Caught signal: 2; Terminating". So it wait a "2" signal to terminate.

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

知你几分 2024-12-05 15:07:08

有一个解决方案。我将尝试为您描述它:

当您编写包装整个控制台的应用程序时,控制台由于某种原因无法接收控制代码(问题去微软),但控制台仍然可以接收这些事件。如何?来自外部应用程序。

这是 cas.exe 的代码

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleAppStopper
{
    class cas
    {
        [STAThread]
        static void Main( string[] args )
        {
            if (args.Length < 2)
            {
                Help ();
                return;
            }
            int processId = int.Parse (args[0]);
            ConsoleCtrlEvent CtrlEvent = (ConsoleCtrlEvent)int.Parse(args[1]);

            FreeConsole ();
            AttachConsole (processId);
            GenerateConsoleCtrlEvent (CtrlEvent, 0);
        }

        static void Help()
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine ("Console Application Eventer(Stopper)");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine ("cas.exe ProcessId ControlEvent");
            Console.WriteLine ("Events:");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine ("\tCTRL_C - 0");
            Console.WriteLine ("\tCTRL_BREAK - 1");
            Console.WriteLine ("\tCTRL_LOGOFF - 5");
            Console.ResetColor ();
        }

        public enum ConsoleCtrlEvent
        {
            CTRL_C = 0, // From wincom.h
            CTRL_BREAK = 1,
            CTRL_CLOSE = 2,
            CTRL_LOGOFF = 5,
            CTRL_SHUTDOWN = 6
        }
        [DllImport ("kernel32.dll")]
        static extern bool GenerateConsoleCtrlEvent( ConsoleCtrlEvent sigevent,
        int dwProcessGroupId );

        [DllImport ("kernel32.dll")]
        static extern bool FreeConsole();

        [DllImport ("kernel32.dll")]
        static extern bool AttachConsole( int dwProcessId ); 
    }
}

以及如何使用它:

public void SendConsoleEvent( ConsoleCtrlEvent ev )
{
    if (!Running)
        return;

    try
    {
        String current_dir = System.Environment.CurrentDirectory;
        String stopper = "cas.exe";
        String args = pr.Id + " " + (int)ev;

        CommandExecutor ex = new CommandExecutor (null, null);
        ex.Start (current_dir, stopper, args);
        // sometimes stop prevent CAS do work. just throw cas and forget about
        //Timer.DelayCall (TimeSpan.FromSeconds (10), ex.Stop);
        //ex.Stop ();
    }
    catch (Exception e)
    {
        Log ("SendConsoleEvent: " + e.ToString ());
    }
}

这里,CommandExecutor 是我围绕 Process 的线程包装器。
pr.Id 是之前开始使用 Process 控制台的 ID(我们需要在其中发送 CTRL_C 或其他事件)

There is a solution. I will try to describe it for you:

When you write the application which wraps the entire console - console into can't receive control codes for some reason (question going to Microsoft), but the console can still receive those events. How? From an external app.

This is the code for cas.exe

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleAppStopper
{
    class cas
    {
        [STAThread]
        static void Main( string[] args )
        {
            if (args.Length < 2)
            {
                Help ();
                return;
            }
            int processId = int.Parse (args[0]);
            ConsoleCtrlEvent CtrlEvent = (ConsoleCtrlEvent)int.Parse(args[1]);

            FreeConsole ();
            AttachConsole (processId);
            GenerateConsoleCtrlEvent (CtrlEvent, 0);
        }

        static void Help()
        {
            Console.BackgroundColor = ConsoleColor.Black;
            Console.ForegroundColor = ConsoleColor.DarkYellow;
            Console.WriteLine ("Console Application Eventer(Stopper)");
            Console.ForegroundColor = ConsoleColor.White;
            Console.WriteLine ("cas.exe ProcessId ControlEvent");
            Console.WriteLine ("Events:");
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine ("\tCTRL_C - 0");
            Console.WriteLine ("\tCTRL_BREAK - 1");
            Console.WriteLine ("\tCTRL_LOGOFF - 5");
            Console.ResetColor ();
        }

        public enum ConsoleCtrlEvent
        {
            CTRL_C = 0, // From wincom.h
            CTRL_BREAK = 1,
            CTRL_CLOSE = 2,
            CTRL_LOGOFF = 5,
            CTRL_SHUTDOWN = 6
        }
        [DllImport ("kernel32.dll")]
        static extern bool GenerateConsoleCtrlEvent( ConsoleCtrlEvent sigevent,
        int dwProcessGroupId );

        [DllImport ("kernel32.dll")]
        static extern bool FreeConsole();

        [DllImport ("kernel32.dll")]
        static extern bool AttachConsole( int dwProcessId ); 
    }
}

and how to use it:

public void SendConsoleEvent( ConsoleCtrlEvent ev )
{
    if (!Running)
        return;

    try
    {
        String current_dir = System.Environment.CurrentDirectory;
        String stopper = "cas.exe";
        String args = pr.Id + " " + (int)ev;

        CommandExecutor ex = new CommandExecutor (null, null);
        ex.Start (current_dir, stopper, args);
        // sometimes stop prevent CAS do work. just throw cas and forget about
        //Timer.DelayCall (TimeSpan.FromSeconds (10), ex.Stop);
        //ex.Stop ();
    }
    catch (Exception e)
    {
        Log ("SendConsoleEvent: " + e.ToString ());
    }
}

Here, CommandExecutor is my threaded wrapper around Process.
pr.Id is the ID of previously started using Process console (where we need to send our CTRL_C or other events

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文