Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9051061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T12:48:49+00:00 2026-06-16T12:48:49+00:00

I have a GUI C# app that uses a console created with AllocConsole .

  • 0

I have a GUI C# app that uses a console created with AllocConsole. It works under normal conditions, but when the app is started in debug mode under Visual Studio, all output ends up in the Visual Studio Output window. How do I stop that?

I am using C# 3.5 and Visual Studio Pro 2010.
The process hosting option is off.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T12:48:52+00:00Added an answer on June 16, 2026 at 12:48 pm

    My solution was to reset the standard streams to the newly created console myself. Here is the code:

    public static class ConsoleHelper
    {
        /// <summary>
        /// Allocates a console and resets the standard stream handles.
        /// </summary>
        public static void Alloc()
        {
            if (!AllocConsole())
                throw new Win32Exception();
            SetStdHandle(StdHandle.Output, GetConsoleStandardOutput());
            SetStdHandle(StdHandle.Input,  GetConsoleStandardInput());
        }
    
        private static IntPtr GetConsoleStandardInput()
        {
            var handle = CreateFile
                ( "CONIN$"
                , DesiredAccess.GenericRead | DesiredAccess.GenericWrite
                , FileShare.ReadWrite
                , IntPtr.Zero
                , FileMode.Open
                , FileAttributes.Normal
                , IntPtr.Zero
                );
            if (handle == InvalidHandleValue)
                throw new Win32Exception();
            return handle;
        }
    
        private static IntPtr GetConsoleStandardOutput()
        {
            var handle = CreateFile
                ( "CONOUT$"
                , DesiredAccess.GenericWrite | DesiredAccess.GenericWrite
                , FileShare.ReadWrite
                , IntPtr.Zero
                , FileMode.Open
                , FileAttributes.Normal
                , IntPtr.Zero
                );
            if (handle == InvalidHandleValue)
                throw new Win32Exception();
            return handle;
        }
    
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool AllocConsole();
    
        [DllImport("kernel32.dll")]
        private static extern bool SetStdHandle(StdHandle nStdHandle, IntPtr hHandle);
    
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern IntPtr CreateFile
            (                               string         lpFileName
            , [MarshalAs(UnmanagedType.U4)] DesiredAccess  dwDesiredAccess
            , [MarshalAs(UnmanagedType.U4)] FileShare      dwShareMode
            ,                               IntPtr         lpSecurityAttributes
            , [MarshalAs(UnmanagedType.U4)] FileMode       dwCreationDisposition
            , [MarshalAs(UnmanagedType.U4)] FileAttributes dwFlagsAndAttributes
            ,                               IntPtr         hTemplateFile
            );
    
        [Flags]
        enum DesiredAccess : uint
        {
            GenericRead    = 0x80000000,
            GenericWrite   = 0x40000000,
            GenericExecute = 0x20000000,
            GenericAll     = 0x10000000
        }
    
        private enum StdHandle : int
        {
            Input  = -10,
            Output = -11,
            Error  = -12
        }
    
        private static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an app that uses RavenDB. It has a WPF front-end GUI app.
I have a python GUI app that uses a long running function from a
I have a GUI app that is getting to be quite slow. I want
We (my colleagues) have a messy 12 y.o. mature app that is GUI-based, and
I have console applicaiotn that currently uses CAsyncSocket. I need to implement SSL so
I have a Cocoa app that does a number of things, but among them,
I have a Windows program that has a GUI which also uses a command
I have an application that uses Flash for the GUI and Python for the
I have a wxLua Gui app that has a Run button. Depending on selected
I have a simple console app that clients will install on their server which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.