Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use custom console for Visual Studio Console Application Debugging

Is it possible to set Visual Studio to use a non-standard console when debugging a Console Application?

I'm not sure what the default console is, it looks just like cmd.exe. I would really love my Console Application to run in ConEmu when I debug.

To be clear, I want to click "Start Debugging" and the process should happen exactly as usual, but instead of bringing up a cmd.exe console, it should bring up a ConEmu console (or whatever).

I'm using Visual Studio 2010 Pro

Closely related to this (unanswered) question: Use Console2 for Visual Studio debugging?

like image 813
Pieter Müller Avatar asked Sep 26 '12 13:09

Pieter Müller


People also ask

How do I Debug a console application in Visual Studio?

Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu. Enter a string in the console window when the program prompts for a name, and then press Enter . Program execution stops when it reaches the breakpoint and before the Console.

Can you use Debug on console?

Debug Console REPL# Expressions can be evaluated with the Debug Console REPL (Read-Eval-Print Loop) feature. To open the Debug Console, use the Debug Console action at the top of the Debug pane or use the View: Debug Console command (Ctrl+Shift+Y).


2 Answers

You've mix up terms. The "Windows Console" is not a "cmd.exe", but special "service" which implemented, for example of Win7, with "conhost.exe".

When you start any console application (does not matter cmd, powershell, or your own app) windows starts it in special environment, which may have visible console window. But it is always internal Windows console.

But! Console emulators may grab this window, hide real console and display their own emulated surface. For example, you may start ConEmu with special switches (described on SU, link in comment) and its done.

Default terminal replacement

ConEmu has a feature named Default Terminal. If you enable this feature you will get seamless starting up your application from Visual Studio in the ConEmu terminal. The idea is hooking CreateProcess in source application (explorer.exe, vcexpress.exe and so on, delimit them with | in the settings). Read more about that feature in the project wiki.

You may choose to use existing ConEmu instance or to run new window for your application. And ConEmu can show Press Enter or Esc to close console... message on the console after your application exits (the Always radio). No need to add readline at the end of your program anymore to see the output.

ConEmu DefTerm settings page

Changing your application code

Because it is your own program, you may add, for example, following lines to the head of your main function

C++ example

#ifdef _DEBUG if (IsDebuggerPresent()) {   STARTUPINFO si = {sizeof(si)}; PROCESS_INFORMATION pi = {};   if (CreateProcess(NULL,         _T("\"C:\\Program Files\\ConEmu\\ConEmu\\ConEmuC.exe\" /AUTOATTACH"),         NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &si, &pi))   { CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } } #endif 

C# example

#if DEBUG ProcessStartInfo pi = new ProcessStartInfo(@"C:\Program Files\ConEmu\ConEmu\ConEmuC.exe", "/AUTOATTACH"); pi.CreateNoWindow = false; pi.UseShellExecute = false; Console.WriteLine("Press Enter after attach succeeded"); Process.Start(pi); Console.ReadLine(); #endif 
like image 95
Maximus Avatar answered Sep 21 '22 13:09

Maximus


Some ideas:

  1. Simply start your program in ConEmu directly, and then choose Attach to Process from the Debug menu in Visual Studio. You can add a delay in the beginning of your program to give you time to attach. This is kind of a pain if you have to do this over and over, but it's often useful for this type of situation.

  2. Add a __debugbreak(); statement to the beginning of your program (perhaps inside an #ifndef NDEBUG block). Start your program from ConEmu directly. Use the JIT feature of Visual Studio to connect when the debug break hits, and continue debugging from there.

like image 27
Adrian McCarthy Avatar answered Sep 20 '22 13:09

Adrian McCarthy