Sean Gugler
Sean Gugler

Reputation: 779

Why does Visual Studio treat ANSI Escape codes differently when debugging?

Applies to:

Visual Studio has two ways to run a C++ program for Win32 Console: "Start Without Debugging (Ctrl+F5)" and "Start Debugging (F5)". Both will launch a separate console window for the program. If the program sends ANSI escape codes via cout, the first window works as expected, but the second will show the codes as characters, with unprintable codes such as ESC replaced by a question mark in a box.

Why is it different? Is there a way to get the ANSI escape codes to behave normally while debugging?

Upvotes: 2

Views: 1754

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54583

The 2015 documentation does not say that there is a restriction (earlir versions required the paid version).

With Visual Studio, you can use the debugger to attach to a running process, which would avoid the problem — provided that your program can initialize and wait for you to do this.

As for why it is different, that is probably because the debugger is intercepting the input/output of the program running in the console window (and preventing it from changing the I/O modes).

Further reading:

From followup comments, @Sean-Gugler realized that

  • the executable's ANSI codes were not interpreted when it is run natively (e.g., opening from the File Explorer),
  • but worked when run normally from Visual Studio.

On being reminded that Windows 10 console window interprets ANSI escape sequences,

  • he verified that the executable ran as expected in a console window, and
  • surmised that Visual Studio was running the executable directly (without a console window) when debugging (F5), but did run it in a console window when running the executable normally (ctrlF5).

One of the problems in starting a console application from a GUI (such as Visual Studio) is that the application would have to do some extra work to allocate a console.

Further reading:

Upvotes: 2

Related Questions