Use custom console for Visual Studio Console Application Debugging
Asked Answered
D

2

52

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?

Debenture answered 26/9, 2012 at 13:4 Comment(2)
Similar Q on SuperUser superuser.com/q/445394/139371Leeway
The methods given in answer below work fine, however, I wish Visual Studio had built-in support for choosing a third party Console Emulator for displaying all console application output. (Still not sure if I'm getting my terminology right here, @Maximus! :-) )Subaxillary
L
43

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
Leeway answered 26/9, 2012 at 16:11 Comment(6)
Thanks. The Attach To option in ConEmu works great, as described in the SuperUser article link you commented on my post. I have not been able to try the pre-processor directive you provide here, since I'm working in C# and I don't currently know how to implement the directive you provided for the C# preprocessor. Thanks for an excellent application!Subaxillary
Thanks @Maximus. What is the expected behaviour of the AUTOATTACH switch in this scenario?Subaxillary
it must start new ConEmu GUI (if does not started yet) and attach your console app into new tab.Leeway
Update 3. If you check new feature "Default terminal" for Visual Studio - there is no need to insert additional lines of code into your console application. It will starts in ConEmu "from the box".Leeway
For anyone having trouble getting this to work with VS2015, I found that devenv.exe launches msvsmon.exe which then launches my application. Adding msvsmon.exe to the list of hooked exes makes it work for me.Weatherworn
Setting "default terminal" makes Visual Studio 2015 spawn 3 windows in ConEmu for me, all with the same contents. Any ideas why?Allegiance
M
3

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.

Michalmichalak answered 26/9, 2012 at 16:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.