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

  • Home
  • SEARCH
  • 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 8245279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:07:47+00:00 2026-06-07T22:07:47+00:00

I made a WinForms application running on the .NET Framework 4.0. It writes on

  • 0

I made a WinForms application running on the .NET Framework 4.0. It writes on the parent console using the Console.WriteLine() method, after calling (once, at startup) the Win32 API function AttachConsole(-1).

It works flawlessly as long as I just need to show the output on screen. Unfortunately when I use a batch with a pipe redirection operator like this:

application.exe > output.txt

it just creates an empty file. Maybe there’s some problem related to the actual pipe being addressed when I use AttachConsole? Why the command prompt can’t catch the data and put it on the file? Does anyone know about any problems related to such a scenario?

  • 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-07T22:07:48+00:00Added an answer on June 7, 2026 at 10:07 pm

    Console.Out is initialised lazily. The first time you reference it the runtime calls GetStdHandle(STD_OUTPUT_HANDLE) to get the standard output handle. If this call occurs before the call to AttachConsole you get the handle to the file for redirection. If this call occurs afterwards you get the console output handle.

    The following class fixes up the standard output and error handles. If you launch your application from a console you’ll notice that any output appears after the next prompt. You can avoid this with start /wait.

    using System;
    using System.Runtime.InteropServices;
    
    namespace SomeProject
    {
        class GuiRedirect
        {
            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern bool AttachConsole(int dwProcessId);
            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern IntPtr GetStdHandle(StandardHandle nStdHandle);
            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern bool SetStdHandle(StandardHandle nStdHandle, IntPtr handle);
            [DllImport("kernel32.dll", SetLastError = true)]
            private static extern FileType GetFileType(IntPtr handle);
    
            private enum StandardHandle : uint
            {
                Input = unchecked((uint)-10),
                Output = unchecked((uint)-11),
                Error = unchecked((uint)-12)
            }
    
            private enum FileType : uint
            {
                Unknown = 0x0000,
                Disk = 0x0001,
                Char = 0x0002,
                Pipe = 0x0003
            }
    
            private static bool IsRedirected(IntPtr handle)
            {
                FileType fileType = GetFileType(handle);
    
                return (fileType == FileType.Disk) || (fileType == FileType.Pipe);
            }
    
            public static void Redirect()
            {
                if (IsRedirected(GetStdHandle(StandardHandle.Output)))
                {
                    var initialiseOut = Console.Out;
                }
    
                bool errorRedirected = IsRedirected(GetStdHandle(StandardHandle.Error));
                if (errorRedirected)
                {
                    var initialiseError = Console.Error;
                }
    
                AttachConsole(-1);
    
                if (!errorRedirected)
                    SetStdHandle(StandardHandle.Error, GetStdHandle(StandardHandle.Output));
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I made a small winforms application to monitor a certain folder for new pdf
I have made a simple test application for the issue, two winforms each containing
I've made a simple C# WinForms app, which makes a screen-capture using System; using
So I made a Winforms GUI in Visual Studio using C#, but for the
I have a Winforms application which is working fine.. using a BackgroundWorkerThread to manage
We have already shipped a client (.NET WinForms) application which sends customer data to
Here is the spec: Multiple clients using a WPF winforms application on their local
I have made a .Net win-forms application in C#, on a 32 bit OS
basically, i made an application on the console that sends and receives information. it
I made a Winforms application, which acccesses folders on another server (in another active

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.