且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为Visual Studio控制台应用程序调试使用自定义控制台

更新时间:2023-11-22 09:21:52

你混淆了术语。 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