Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot set SetConsoleCtrlHandler #10647

Open
blacklightpy opened this issue Jul 13, 2021 · 7 comments
Open

Cannot set SetConsoleCtrlHandler #10647

blacklightpy opened this issue Jul 13, 2021 · 7 comments
Labels
Area-DefApp Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Priority-1 A description (P1) Product-Terminal The new Windows Terminal.
Milestone

Comments

@blacklightpy
Copy link

blacklightpy commented Jul 13, 2021

Windows Terminal version (or Windows build number)

1.9.1523.0

Other Software

I wrote a console application in C++ and wanted to handle CTRL_CLOSE_EVENT.
I set my default terminal as Windows Terminal, so the app launched in it by default.
I wasn't sure why the method wasn't working and after a while I decided to try out running the program in conhost and it worked.

Here's the code:

BOOL WINAPI CtrlHandler(DWORD fdwCtrlType)
{
    switch (fdwCtrlType)
    {
    case CTRL_C_EVENT:
        printf("Ctrl-C event\n\n");
        Beep(750, 300);
        return TRUE;

    case CTRL_CLOSE_EVENT:
        Beep(600, 200);
        printf("Ctrl-Close event\n\n");
        return TRUE;

    case CTRL_BREAK_EVENT:
        Beep(900, 200);
        printf("Ctrl-Break event\n\n");
        return FALSE;

    case CTRL_LOGOFF_EVENT:
        Beep(1000, 200);
        printf("Ctrl-Logoff event\n\n");
        return FALSE;

    case CTRL_SHUTDOWN_EVENT:
        Beep(750, 500);
        printf("Ctrl-Shutdown event\n\n");
        return FALSE;

    default:
        return FALSE;
    }
}
int main()
{
    SetConsoleCtrlHandler((CtrlHandler, TRUE);
    while(1)
    {
        std::cin.get();
    }
}

I guessed this could be because Windows Terminal uses Win32 windows, but this could cause compatibility issues with existing console apps.

Steps to reproduce

Run the program and try to trigger a CTRL_C_EVENT

Expected Behavior

CtrlHandler handles the event

Actual Behavior

CtrlHandler is not invoked

@ghost ghost added Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting Needs-Tag-Fix Doesn't match tag requirements labels Jul 13, 2021
@zadjii-msft zadjii-msft added Area-DefApp Product-Terminal The new Windows Terminal. labels Jul 20, 2021
@miniksa
Copy link
Member

miniksa commented Jul 23, 2021

Very high chance this is resolved with #10415

@miniksa miniksa added Issue-Bug It either shouldn't be doing this or needs an investigation. and removed Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Jul 23, 2021
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Jul 23, 2021
@DHowett
Copy link
Member

DHowett commented Jul 23, 2021

@blacklightpy Would you mind testing on Windows 22000.65 or higher with Windows Terminal Preview 1.10?

@DHowett DHowett added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Jul 23, 2021
@ghost ghost added the No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. label Jul 27, 2021
@ghost
Copy link

ghost commented Jul 27, 2021

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

@blacklightpy
Copy link
Author

blacklightpy commented Jul 28, 2021

@DHowett Sorry for the delay, I was sick for a while.
I'm now on 22000.100 and on Terminal Preview 1.10.1933.0
Still, the same result.

I noticed that I can hear a beep sound when I'm closing the window, meaning CTRL_CLOSE_EVENT is triggered.
But CTRL_C_EVENT and CTRL_BREAK_EVENT are still not triggered in the terminal. I didn't try the logoff and shutdown events.

The beep sound wasn't working the last time, but it wasn't working in conhost too then. Now it's working on both.

CTRL_CLOSE_EVENT too is working differently from what's expected. In conhost, the program exists only after the beep is performed. In Terminal however, the tab is close immediately and the beep is heard afterwards. So if there are any print statements in the control handler, they won't be seen. I tried setting 2 beeps and a print statement between them, and both beeps were still executed.

@ghost ghost added Needs-Attention The core contributors need to come back around and look at this ASAP. and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. labels Jul 28, 2021
@miniksa miniksa self-assigned this Aug 12, 2021
@miniksa
Copy link
Member

miniksa commented Aug 12, 2021

I'll look into this when I get a chance.

@miniksa miniksa added this to the Terminal v1.11 milestone Aug 17, 2021
@miniksa
Copy link
Member

miniksa commented Aug 20, 2021

Okay. So far...

CTRL_BREAK_EVENT is coming in as a break if started within the Terminal, but as a CTRL_C if started from DefApp. So that one is DefApp related.

CTRL_CLOSE_EVENT seems to behave the same for both and makes the noise after the tab goes away. That slightly tracks with how tab handing works and we'd likely have to introduce a delay/ordering of sorts there to stop that one and is likely NOT DefApp related.

CTRL_C_EVENT just works in both.

Now trying to figure out how to do CTRL_LOGOFF_EVENT and CTRL_SHUTDOWN_EVENT as I think those are weird/difficult.

@miniksa
Copy link
Member

miniksa commented Aug 20, 2021

There's another issue with CTRL_CLOSE_EVENT related to CloseTest.exe (from our tools folder). It is not propagating to all the children...

@zadjii-msft zadjii-msft added the Priority-1 A description (P1) label Jan 4, 2022
@zadjii-msft zadjii-msft modified the milestones: Terminal v2.0, 22H1 Jan 4, 2022
@zadjii-msft zadjii-msft modified the milestones: 22H1, Terminal v1.14 Feb 2, 2022
@miniksa miniksa removed their assignment Mar 31, 2022
@zadjii-msft zadjii-msft modified the milestones: Terminal v1.14, 22H2 Apr 28, 2022
@zadjii-msft zadjii-msft modified the milestones: 22H2, Backlog Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-DefApp Issue-Bug It either shouldn't be doing this or needs an investigation. Needs-Attention The core contributors need to come back around and look at this ASAP. Priority-1 A description (P1) Product-Terminal The new Windows Terminal.
Projects
None yet
Development

No branches or pull requests

4 participants