echocon sample does not work #2567

Closed
opened 2026-01-30 22:58:34 +00:00 by claunia · 6 comments
Owner

Originally created by @amoldeshpande on GitHub (Jul 4, 2019).

Environment

Windows build number: 18362.207
Windows Terminal version (if applicable):

Any other software? VS2019 with Windows 10 SDK 17763

Steps to reproduce

Launch the sample

Expected behavior

it should work

Actual behavior

The call to SetConsoleMode fails with ERROR_INVALID_PARAMETER. If I ignore this error, the sample works as written. No idea if it would still work with an actual VT100 client.

ETA: The ReadFile in PipeListener thread never returns while the child process is happily printing to the console window it was spawned with.

So, I don't really understand how this thing illustrates anything about ConPTY.

Originally created by @amoldeshpande on GitHub (Jul 4, 2019). <!-- 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 I ACKNOWLEDGE THE FOLLOWING BEFORE PROCEEDING: 1. If I delete this entire template and go my own path, the core team may close my issue without further explanation or engagement. 2. If I list multiple bugs/concerns in this one issue, the core team may close my issue without further explanation or engagement. 3. If I write an issue that has many duplicates, the core team may close my issue without further explanation or engagement (and without necessarily spending time to find the exact duplicate ID number). 4. If I leave the title incomplete when filing the issue, the core team may close my issue without further explanation or engagement. 5. If I file something completely blank in the body, the core team may close my issue without further explanation or engagement. All good? Then proceed! --> <!-- This bug tracker is monitored by Windows Terminal development team and other technical folks. **Important: When reporting BSODs or security issues, DO NOT attach memory dumps, logs, or traces to Github issues**. Instead, send dumps/traces to secure@microsoft.com, referencing this GitHub issue. Please use this form and describe your issue, concisely but precisely, with as much detail as possible. --> # Environment ```none Windows build number: 18362.207 Windows Terminal version (if applicable): Any other software? VS2019 with Windows 10 SDK 17763 ``` # Steps to reproduce Launch the sample # Expected behavior it should work # Actual behavior The call to SetConsoleMode fails with ERROR_INVALID_PARAMETER. If I ignore this error, the sample works as written. No idea if it would still work with an actual VT100 client. ETA: The ReadFile in PipeListener thread never returns while the child process is happily printing to the console window it was spawned with. So, I don't really understand how this thing illustrates anything about ConPTY.
Author
Owner

@amoldeshpande commented on GitHub (Jul 9, 2019):

hi @DHowett-MSFT, I see you tagged it with "help-wanted", and I would be happy to fix the sample if there was a way.
As far as I can tell, the sample is using the relevant APIs as documented in MSDN and used in various Microsoft-authored articles and code snippets since ConPTY was announced.

However, things simply do not work (the SetConsoleMode call is a distraction in the sample and can be removed with no consequences to anything it demonstrates). There is a conhost.exe spawned with the child process, but the output is not redirected to the pipes.

So, I am really at a loss in understanding the correct way of using the ConPTY API. I tried downloading the Terminal source but gave up on using it to troubleshoot EchoCon, when I realized Terminal won't run out of Visual Studio but needs to be packaged and has other hoops.

Also, looking at the Terminal source where it creates ConPTYs , it is identical to the EchoCon sample, so I'm back to square one in that respect.

TL:DR; I think there is more here than just a sample issue.

@amoldeshpande commented on GitHub (Jul 9, 2019): hi @DHowett-MSFT, I see you tagged it with "help-wanted", and I would be happy to fix the sample if there was a way. As far as I can tell, the sample is using the relevant APIs as documented in MSDN and used in various Microsoft-authored articles and code snippets since ConPTY was announced. However, things simply do not work (the SetConsoleMode call is a distraction in the sample and can be removed with no consequences to anything it demonstrates). There is a conhost.exe spawned with the child process, but the output is not redirected to the pipes. So, I am really at a loss in understanding the correct way of using the ConPTY API. I tried downloading the Terminal source but gave up on using it to troubleshoot EchoCon, when I realized Terminal won't run out of Visual Studio but needs to be packaged and has other hoops. Also, looking at the Terminal source where it creates ConPTYs , it is identical to the EchoCon sample, so I'm back to square one in that respect. TL:DR; I think there is more here than just a sample issue.
Author
Owner

@DHowett-MSFT commented on GitHub (Jul 10, 2019):

Hey @amoldeshpande,
Thanks for the comprehensive report! I'm actually not able to reproduce this.

I've modified the code to replace the escape character with # only when it receives it on the output pipe

diff --git a/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp b/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp
index 428f1e3e..2cc12131 100644
--- a/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp
+++ b/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp
@@ -183,6 +183,8 @@ void __cdecl PipeListener(LPVOID pipe)
         // Write received text to the Console
         // Note: Write to the Console using WriteFile(hConsole...), not printf()/puts() to
         // prevent partially-read VT sequences from corrupting output
+       for(int i = 0; i < dwBytesRead; ++i)
+               if(szBuffer[i] == '\x1b') szBuffer[i] = '#';
         WriteFile(hConsole, szBuffer, dwBytesRead, &dwBytesWritten, NULL);

     } while (fRead && dwBytesRead >= 0);

This is what I'm getting (which illustrates that the output from ping is being received on the output pipe):

#[25l#[2J#[m#[H#]0;C:\WINDOWS\SYSTEM32\ping.exe#[?25h
Pinging DHOWETT-DEV [::1] with 32 bytes of data:
Reply#[70X from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms
Reply from ::1: time<1ms
#[25X
Ping statistics for ::1:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:#[10X
    Minimu#[46Xm = 0ms, Maximum = 0ms, Average = 0ms

Using ENABLE_VIRTUAL_TERMINAL_PROCESSING and passing the output pipe's contents directly to standard out makes the sample look exactly like running ping.exe in conhost, you're right.

@DHowett-MSFT commented on GitHub (Jul 10, 2019): Hey @amoldeshpande, Thanks for the comprehensive report! I'm actually not able to reproduce this. I've modified the code to replace the escape character with `#` _only when it receives it on the output pipe_ ```patch diff --git a/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp b/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp index 428f1e3e..2cc12131 100644 --- a/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp +++ b/samples/ConPTY/EchoCon/EchoCon/EchoCon.cpp @@ -183,6 +183,8 @@ void __cdecl PipeListener(LPVOID pipe) // Write received text to the Console // Note: Write to the Console using WriteFile(hConsole...), not printf()/puts() to // prevent partially-read VT sequences from corrupting output + for(int i = 0; i < dwBytesRead; ++i) + if(szBuffer[i] == '\x1b') szBuffer[i] = '#'; WriteFile(hConsole, szBuffer, dwBytesRead, &dwBytesWritten, NULL); } while (fRead && dwBytesRead >= 0); ``` This is what I'm getting (which illustrates that the output from `ping` is being received on the output pipe): ``` #[25l#[2J#[m#[H#]0;C:\WINDOWS\SYSTEM32\ping.exe#[?25h Pinging DHOWETT-DEV [::1] with 32 bytes of data: Reply#[70X from ::1: time<1ms Reply from ::1: time<1ms Reply from ::1: time<1ms Reply from ::1: time<1ms #[25X Ping statistics for ::1: Packets: Sent = 4, Received = 4, Lost = 0 (0% loss), Approximate round trip times in milli-seconds:#[10X Minimu#[46Xm = 0ms, Maximum = 0ms, Average = 0ms ``` Using `ENABLE_VIRTUAL_TERMINAL_PROCESSING` and passing the output pipe's contents _directly to standard out_ makes the sample look exactly like running ping.exe in conhost, you're right.
Author
Owner

@amoldeshpande commented on GitHub (Jul 10, 2019):

hmm, that really makes no sense to me. Are you running 18362 or some internal build ? I also updated my sdk to 18362 (though that should not matter)

I get error 87 from SetConsoleMode failing and never hit the breakpoint I put in your for loop above.

I see separate windows for ping.exe and echocon.exe (and in the debugger I can see conhost.exe launched as well)

what else can I do to troubleshoot ?

@amoldeshpande commented on GitHub (Jul 10, 2019): hmm, that really makes no sense to me. Are you running 18362 or some internal build ? I also updated my sdk to 18362 (though that should not matter) I get error 87 from SetConsoleMode failing and never hit the breakpoint I put in your for loop above. I see separate windows for ping.exe and echocon.exe (and in the debugger I can see conhost.exe launched as well) what else can I do to troubleshoot ?
Author
Owner

@DHowett-MSFT commented on GitHub (Jul 10, 2019):

Hold up, do you have the "Use Legacy Console" option turned on here? I think ENABLE_VIRTUAL_TERMINAL_PROCESSING won't work in the legacy console, and that's why you're seeing ping spawn its own console window.

image

@DHowett-MSFT commented on GitHub (Jul 10, 2019): Hold up, do you have the "Use Legacy Console" option turned on here? I think `ENABLE_VIRTUAL_TERMINAL_PROCESSING` won't work in the legacy console, and that's why you're seeing ping spawn its own console window. ![image](https://user-images.githubusercontent.com/14316954/60933407-f44c6a00-a276-11e9-8362-120dae731581.png)
Author
Owner

@amoldeshpande commented on GitHub (Jul 10, 2019):

Yes ! that was it ! I didn't turn it on, so it must default to that mode.

I feel appropriately silly now. Though in my defense, the blog articles with code snippets do not mention this or have calls to SetConsoleMode :-)

@amoldeshpande commented on GitHub (Jul 10, 2019): Yes ! that was it ! I didn't turn it on, so it must default to that mode. I feel appropriately silly now. Though in my defense, the blog articles with code snippets do not mention this or have calls to SetConsoleMode :-)
Author
Owner

@DHowett-MSFT commented on GitHub (Jul 10, 2019):

Nah, you're totally right that this is poorly documented. We actually just discovered it ourselves! #1838 😄

@DHowett-MSFT commented on GitHub (Jul 10, 2019): Nah, you're totally right that this is poorly documented. We actually just discovered it ourselves! #1838 :smile:
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#2567