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

Applies to:
Visual Studio Community Edition 2015 (C++)
Windows 10
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?

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:
Attach to Running Processes with the Visual Studio Debugger (2015)
How to: Attach to a Running Process (2010)
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:
Can one executable be both a console and GUI application?
Using the console in a GUI app in windows, only if its run from a console
How to write to the console in a GUI application

Related

How to run C/C++ code using integrated terminal in Visual Studio

How to launch a C/C++ console application using the integrated terminal in Visual Studio instead of launching a separate terminal window?
To clarify more:
When I press the run button right now, this is what happens:
But what I want is something like this (but it must happen when I press the run button), where the output is directed to a terminal inside the visual studio window, without launching a separate window, this will make it easier when debugging because I won't have to switch between multiple windows:
There is a handy VsConsoleOutput extension for Visual Studio 2019 / 2022 that redirects program output into Output window inside of Visual Studio.
However when installing it i've got an exception complaining about incorrect value of InstalledByMsi value somewhere in manifest. The workaround is to manually open downloaded .vsix package (which seems to be a .zip archive) using WinRAR or something, adjust one line in extension.vsixmanifest file and save updated archive.
<Installation AllUsers="true" InstalledByMsi="false">

How to put out a Dos screen once you build in Visual studio?

I'm pretty new to programming and I have built my program a few times. However I can't figure out how to make a Command Window or DOS window show up, even after the build has finished in Visual Studio on Windows.
Suppose you have built your program as a 'Console app' or 'Command-Line app' in Visual Studio. These were choices you made when you first set up your project. Then, running your program from Visual Studio will cause a Command Window (DOS like) to display, and your program will start within this window.
The 'Debug' menu item has 'Start Debugging' and 'Start Without Debugging' - either choice will run your program.
Depending on your version of Visual Studio or your configuration options, Visual Studio may close the console window as soon as the program completes. This is not the default behavior on recent Visual Studio options, but you may miss the Console Window popping up and going away if it is happening to you. Likewise, it may be hidden if the Console Window is not displayed as the top-most Window.

Is there a method of having the output of a local windows debugger to be integrated within the interface of visual studio?

I have been coding in visual studio for a long time but I don't know how to integrate the command prompt window for the local windows debugger, with the visual studio interface itself.
I have seen there are other programs where the output is displayed within the interface of the program itself.
Programs such as NetBeans.
Is this a possible thing in c++?, as I am getting tired of going back and forth in the output executioner file for the local windows debugger.

How to display Visual Studio Console Application output inside the IDE and not in command prompt?

I recently started C++ programming on Visual Studio and I noticed that it always gives me the console output in the command prompt (CMD).
I am a java programmer and I'm used to working with Eclipse and Netbeans. With those IDEs, I was able to see the console inside the IDE and not in a different separate window.
Is there a way to display the console output inside Visual Studio, like Eclipse and Netbeans do?
It's infuriating. I spent hours looking for this. Visual Studio doesn't have command prompt inside the IDE.
They have it for Visual Studio code - Intergrated command prompt. But not for visual studio 2015 Enterprise. So, in other words, microsoft has command prompt terminal inside the free version of visual studio but not for the paid version
Unfortunately, the answer seems to be no.
In Visual Studio, console applications are displayed on the command prompt and not inside Visual Studio itself. Meaning that Console.WriteLine method and similar ones write your output to the console window because your application type is Console Application.
You are able to write output to Visual Studio itself by using System.Diagnostics.Debug.WriteLine as mentioned on MSDN. That will cause the debug output to appear in the Output Window inside Visual Studio. In case you don't see that view, you can choose to show it by Debug => Windows => Output.
You should bare in mind that this is not what you asked for.
This "solution" is helpful just in case you want to debug parts of your code and don't want to open the command prompt but just see the relevant output inside the IDE.
In addition, you won't be able to give input back in this output view.
The most important thing, you will not be able to execute your application correctly outside of your coding environment. So, it will work only on the IDE but you won't be able to see this output when the application is on its own (as it meant to be as a console application).
Another solution, that you might like, is to work with Eclipse.
You said that you are familiar with Eclipse as a Java developer and now you work with C++ and don't get along with Visual Studio so far.
So, you can download Eclipse IDE for C/C++ Developers.
In there you will be familiar with the IDE and you will be able to display your output inside Eclipse without any weird and unnecessary workarounds.

Redirect command prompt output to output window in Visual studio 2012

How can I redirect everything printed onto the command prompt to be printed onto the output window in Visual studio 2012? I am looking for a console as shown in eclipse.
I am developing a console application in visual C++.
I tried "using namespace System::Diagnostic" but this way I don't see any Debug.WriteLine in suggestions. What other alternative do I have?
This question is answered here. Anyway you should try work different approach, you may be used to Eclipse a I believe, you will get used to Visual Studio soon. I never needed to redirect the console output to Output Window.
These two types of outputs has different purposes. Console Output is intended for the application itself (customer) and one Output Window is intended for debugging purposes. You don't want to release debug messages to customer. Right? But you don't want to delete or comment out debug messages every time you release the software.