简体   繁体   English

AllocConsole在Visual Studio中不打印

[英]AllocConsole not printing when in Visual Studio

I want to create a console window and print some info on it when debugging my program. 我想在调试程序时创建一个控制台窗口并在其上打印一些信息。 VS 2010 does not give me the option of setting different Output types for my program depending on whether its in debug or release mode, so I resorted to creating a Console window manually like so: VS 2010没有为我提供为程序设置不同输出类型的选项,具体取决于它是处于调试模式还是发布模式,因此我采用手动创建控制台窗口,如下所示:

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

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

That pops open a console window, but nothing gets written to it. 弹出窗口打开一个控制台窗口,但没有任何内容写入它。 I tried a bunch of other pinvoke (AttachConsole etc...) that did nothing. 我尝试了一堆其他的pinvoke(AttachConsole等......)什么也没做。 Then I finally tried running the application outside of Visual Studio, and the Console Window worked. 然后我终于尝试在Visual Studio之外运行应用程序,并且控制台窗口工作。 Apparently Visual Studio is eating up all my Console.WriteLines! 显然Visual Studio正在吃掉我所有的Console.WriteLines!

How can I fix this? 我怎样才能解决这个问题?

Having encountered the same issue, here is some code that appears to restore console output for me after the call to AllocConsole : 遇到同样的问题,这里有一些代码,在调用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/Invokes as follows: P / Invokes如下:

    [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;

I got the same issue. 我遇到了同样的问题。 Turns out writing to the console works in visual studio only when debugging in hosted process. 只有在托管进程中进行调试时,才能在visual studio中编写控件。 Go to Project Properties -> Debug -> Enable Debuggers and make sure 'Enable Visual Studio hosting process' is checked. 转到项目属性 - >调试 - >启用调试器,并确保选中“启用Visual Studio主机进程”。

As I have already said here , you can try to run the VS as administrator . 正如我在此处所述,您可以尝试以管理员身份运行VS. That worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即时打印 Visual Studio 2013 C# - Instant printing Visual Studio 2013 C# 在Visual Studio 2010中的Crystal Reports中打印 - Printing in Crystal Reports in Visual Studio 2010 使用AllocConsole和目标体系结构x86时没有控制台输出 - No console output when using AllocConsole and target architecture x86 Visual Studio 2010没有在工具箱中显示“打印”选项卡的Crystal Report Viewer? - Visual Studio 2010 not showing Crystal Report Viewer of Printing Tab in Toolbox? 我如何在打印时制作这个文档(Visual Studio) - How do I make this one document in printing (Visual Studio) 使用C#在Visual Studio中的GUI标签中打印数组 - Printing an array in a GUI label in visual studio with C# 使用Visual Studio 2012在C#中的RichTextBox中打印换行符 - Printing newline in richtextbox in c# using Visual Studio 2012 为什么 Visual Studio 代码可以将 C# 正确打印到终端,但没有输出? - Why is visual studio code printing C# to terminal correctly, but not output? 在Visual Studio调试器中打印漂亮的对象图 - Printing nice looking object graphs in the Visual Studio Debugger Visual Studio:调试时重置用户设置 - Visual Studio: reset user settings when debugging
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM