为Visual Studio控制台应用程序调试使用自定义控制台 [英] Use custom console for Visual Studio Console Application Debugging

查看:1066
本文介绍了为Visual Studio控制台应用程序调试使用自定义控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以设置Visual Studio在调试控制台应用程序时使用非标准控制台?

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

我不知道默认控制台是什么,它看起来就像 cmd.exe 。我真的很喜欢我的控制台应用程序在ConEmu中运行时,我调试。

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.

要清楚,我想点击开始调试 ,但是不是打开一个 cmd.exe 控制台,它应该会打开一个ConEmu控制台(或其他)。

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).

我正在使用Visual Studio 2010 Pro

I'm using Visual Studio 2010 Pro

与此(未回答的)问题密切相关:使用Console2进行Visual Studio调试

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

推荐答案

你混淆了术语。 Windows控制台不是cmd.exe,而是用conhost.exe实现的特殊服务,例如Win7。

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".

您启动任何控制台应用程序(无论cmd,powershell或您自己的应用程序)窗口在特殊环境中启动它,可能有可见的控制台窗口。但它总是内部Windows控制台。

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.

但是!控制台仿真器可以抓取此窗口,隐藏真实控制台并显示它们自己的仿真表面。
例如,您可以使用特殊开关(在SU上描述,在注释中链接)及其完成来启动ConEmu。

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.

默认终端替换

ConEmu具有默认终端。如果启用此功能,您将从ConEmu终端中的Visual Studio无缝启动您的应用程序。这个想法是在源应用程序中挂钩CreateProcess( explorer.exe vcexpress.exe 等等, code> | )。请在项目wiki 中了解有关该功能的详情。

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.

您可以选择使用现有的ConEmu实例或为应用程序运行新窗口。在应用程序退出后,ConEmu可以在控制台上显示按Enter或Esc关闭控制台... 消息( Always 无线电)。不需要在程序结束处添加 readline 即可查看输出。

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.

更改应用程序代码

因为它是你自己的程序,所以你可以在 main function

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

C ++示例

#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#示例

#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

这篇关于为Visual Studio控制台应用程序调试使用自定义控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆