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

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

问题描述

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



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



要清楚,我想单击开始调试,过程应该像往常一样,但是不要打开一个 cmd.exe 控制台,它应该启动一个ConEmu控制台(或任何)。



我正在使用Visual Studio 2010 Pro



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

解决方案

你已经混合了条款。 Windows控制台不是一个cmd.exe,而是特殊的服务,例如Win7使用conhost.exe。



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



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



默认终端替换



ConEmu有一个名为默认终端。如果启用此功能,您可以从ConEmu终端中的Visual Studio无缝启动应用程序。这个想法是在源应用程序( explorer.exe vcexpress.exe 等等中挂起CreateProcess,将它们与 | 在设置)。详细了解项目wiki 中的该功能。



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





更改应用程序代码



由于这是您自己的程序,您可以添加以下行: main 函数



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:\程序文件\ConEmu\ConEmu\ConEmuC.exe,/ AUTOATTACH);
pi.CreateNoWindow = false;
pi.UseShellExecute = false;
Console.WriteLine(附加成功后按Enter);
Process.Start(pi);
Console.ReadLine();
#endif


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?

解决方案

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.

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

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

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