【问题标题】:AllocConsole not printing when in Visual Studio在 Visual Studio 中时 AllocConsole 不打印
【发布时间】:2013-03-22 19:38:16
【问题描述】:

我想在调试我的程序时创建一个控制台窗口并在上面打印一些信息。 VS 2010 没有让我选择为我的程序设置不同的输出类型,具体取决于它是处于调试模式还是发布模式,所以我求助于手动创建一个控制台窗口,如下所示:

[DllImport("kernel32.dll")]
public static extern Int32 AllocConsole();

static void Main()
{
#if DEBUG
    AllocConsole();
#endif
....

这会弹出一个控制台窗口,但没有写入任何内容。我尝试了一堆其他的 pinvoke(AttachConsole 等),但什么也没做。然后我终于尝试在 Visual Studio 之外运行应用程序,并且控制台窗口工作。显然 Visual Studio 正在吃掉我所有的 Console.WriteLines!

我该如何解决这个问题?

【问题讨论】:

  • 应该没问题。查看 VS 输出窗口。并且通常支持 Debug.Print() 在那里显示调试输出。
  • 或者Trace.Write...或者等等,现在我想:VS会自动连接TraceListeners吗?嗯...*clattety-clack-clack* - 是的,确实如此。
  • 好吧,我还是希望它显示在控制台窗口上,而不是 VS 的输出窗口中。

标签: c# visual-studio-2010 console


【解决方案1】:

遇到同样的问题,这里有一些代码在调用AllocConsole 后为我恢复控制台输出:

    private static void OverrideRedirection()
    {
        var hOut = GetStdHandle(STD_OUTPUT_HANDLE);
        var hRealOut = CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FileShare.Write, IntPtr.Zero, FileMode.OpenOrCreate, 0, IntPtr.Zero);
        if (hRealOut != hOut)
        {
            SetStdHandle(STD_OUTPUT_HANDLE, hRealOut);
            Console.SetOut(new StreamWriter(Console.OpenStandardOutput(), Console.OutputEncoding) { AutoFlush = true });
        }
    }

P/调用如下:

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern IntPtr GetStdHandle(int nStdHandle);

    [DllImport("kernel32.dll", SetLastError = true)]
    public static extern bool SetStdHandle(int nStdHandle, IntPtr hHandle);

    public const int STD_OUTPUT_HANDLE = -11;
    public const int STD_INPUT_HANDLE  = -10;
    public const int STD_ERROR_HANDLE  = -12;

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern IntPtr CreateFile([MarshalAs(UnmanagedType.LPTStr)] string         filename,
                                           [MarshalAs(UnmanagedType.U4)]     uint           access,
                                           [MarshalAs(UnmanagedType.U4)]     FileShare      share,
                                                                             IntPtr         securityAttributes,
                                           [MarshalAs(UnmanagedType.U4)]     FileMode       creationDisposition,
                                           [MarshalAs(UnmanagedType.U4)]     FileAttributes flagsAndAttributes,
                                                                             IntPtr         templateFile);

    public const uint GENERIC_WRITE = 0x40000000;
    public const uint GENERIC_READ  = 0x80000000;

【讨论】:

  • 我不能对这个回复给予足够的支持;神奇的CONOUT$CONIN$ 文件是Windows 中文档记录最差的部分之一,确实应​​该得到更多关注。可以在此链接中找到有关它们的 MSDN 小文档:msdn.microsoft.com/en-us/library/windows/desktop/…
  • 您不需要if (hRealOut != hOut)...CreateFile 将始终返回一个唯一句柄,即使两者都引用同一个对象,该句柄也将不等于GetStdHandle() 的返回值。
【解决方案2】:

我遇到了同样的问题。事实证明,只有在托管进程中进行调试时,才能在 Visual Studio 中写入控制台。转到项目属性 -> 调试 -> 启用调试器并确保选中“启用 Visual Studio 托管进程”。

【讨论】:

  • 我还在VS2013中发现启用“Sql Server Debugging”会阻止控制台工作,可能是由于ghord提到的原因。
【解决方案3】:

正如我已经说过的here,您可以尝试以管理员身份运行 VS。这对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 2010-10-10
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多