Ctrl+Break does not send 0x03 in raw mode #7150

Open
opened 2026-01-31 00:56:28 +00:00 by claunia · 0 comments
Owner

Originally created by @alexrp on GitHub (Mar 26, 2020).

Environment

Windows build number: Microsoft Windows [Version 10.0.19041.153]

Steps to reproduce

using System;
using System.Threading;
using Vanara.PInvoke;
using static Vanara.PInvoke.Kernel32;

namespace Test
{
    static class Program
    {
        static HandlerRoutine _handler;

        unsafe static void Main()
        {
            _handler = (e) =>
            {
                Console.WriteLine(e);
                return true;
            };

            SetConsoleCtrlHandler(_handler, true);

            var stdin = GetStdHandle(StdHandleType.STD_INPUT_HANDLE);

            GetConsoleMode(stdin, out CONSOLE_INPUT_MODE m);
            m |= CONSOLE_INPUT_MODE.ENABLE_VIRTUAL_TERMINAL_INPUT;
            m &= ~(CONSOLE_INPUT_MODE.ENABLE_PROCESSED_INPUT |
                   CONSOLE_INPUT_MODE.ENABLE_LINE_INPUT);
            SetConsoleMode(stdin, m);

            var buf = new byte[4096];

            fixed (byte* p = buf)
                Kernel32.ReadFile(stdin, (IntPtr)p, (uint)buf.Length, out _, IntPtr.Zero);

            Console.WriteLine(Win32Error.GetLastError());
            Console.WriteLine("0x{0:x2}", buf[0]);
            Thread.Sleep(2500);
        }
    }
}

Expected behavior

ERROR_SUCCESS
0x03

Actual behavior

CTRL_BREAK_EVENT
ERROR_SUCCESS
0x00

Notes

  • If the SetConsoleCtrlHandler call is removed, no output is printed at all.
  • Using ReadConsole instead makes no difference (except that ERROR_OPERATION_ABORTED is printed).
  • On Linux, if the terminal is in raw mode, both Ctrl+C and Ctrl+Break will return 0x03.
Originally created by @alexrp on GitHub (Mar 26, 2020). # Environment ```none Windows build number: Microsoft Windows [Version 10.0.19041.153] ``` # Steps to reproduce ```csharp using System; using System.Threading; using Vanara.PInvoke; using static Vanara.PInvoke.Kernel32; namespace Test { static class Program { static HandlerRoutine _handler; unsafe static void Main() { _handler = (e) => { Console.WriteLine(e); return true; }; SetConsoleCtrlHandler(_handler, true); var stdin = GetStdHandle(StdHandleType.STD_INPUT_HANDLE); GetConsoleMode(stdin, out CONSOLE_INPUT_MODE m); m |= CONSOLE_INPUT_MODE.ENABLE_VIRTUAL_TERMINAL_INPUT; m &= ~(CONSOLE_INPUT_MODE.ENABLE_PROCESSED_INPUT | CONSOLE_INPUT_MODE.ENABLE_LINE_INPUT); SetConsoleMode(stdin, m); var buf = new byte[4096]; fixed (byte* p = buf) Kernel32.ReadFile(stdin, (IntPtr)p, (uint)buf.Length, out _, IntPtr.Zero); Console.WriteLine(Win32Error.GetLastError()); Console.WriteLine("0x{0:x2}", buf[0]); Thread.Sleep(2500); } } } ``` # Expected behavior ``` ERROR_SUCCESS 0x03 ``` # Actual behavior ``` CTRL_BREAK_EVENT ERROR_SUCCESS 0x00 ``` # Notes * If the `SetConsoleCtrlHandler` call is removed, no output is printed at all. * Using `ReadConsole` instead makes no difference (except that `ERROR_OPERATION_ABORTED` is printed). * On Linux, if the terminal is in raw mode, both Ctrl+C and Ctrl+Break will return `0x03`.
claunia added the Needs-TriageNeeds-Tag-Fix labels 2026-01-31 00:56:28 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#7150