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

查看:38
本文介绍了使用自定义控制台进行 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",而是特殊的服务",例如 Win7 中的conhost.exe".

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 会在特殊环境中启动它,该环境可能具有可见的控制台窗口.但它始终是内部 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.

但是!控制台模拟器可能会抓住这个窗口,隐藏真正的控制台并显示他们自己的模拟表面.例如,您可以使用特殊开关启动 ConEmu(在 SU 中描述,评论中的链接)并完成.

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 有一个名为 Default Terminal 的功能.如果启用此功能,您将在 ConEmu 终端中从 Visual Studio 无缝启动您的应用程序.这个想法是在源应用程序中挂钩 CreateProcess(explorer.exevcexpress.exe 等,在设置中用 | 分隔它们).在 项目 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 可以在应用程序退出后在控制台上显示 Press 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 函数的头部添加例如以下行

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 FilesConEmuConEmuConEmuC.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天全站免登陆