


SetStdHandle(STD_ERROR_HANDLE, hStdErrDup) If (GetFileInformationByHandle(GetStdHandle(STD_ERROR_HANDLE), out bhfi)) SetStdHandle(STD_OUTPUT_HANDLE, hStdOut) SetStdHandle(STD_OUTPUT_HANDLE, hStdOutDup) If (GetFileInformationByHandle(GetStdHandle(STD_OUTPUT_HANDLE), out bhfi)) Attach to console window – this may modify the standard handles Duplicate Stderr handle to save initial valueĭuplicateHandle(hProcess, hStdErr, hProcess, out hStdErrDup, Duplicate Stdout handle to save initial valueĭuplicateHandle(hProcess, hStdOut, hProcess, out hStdOutDup, IntPtr hProcess = Process.GetCurrentProcess().Handle HStdErr = GetStdHandle(STD_ERROR_HANDLE) HStdOut = GetStdHandle(STD_OUTPUT_HANDLE) Private struct BY_HANDLE_FILE_INFORMATION Static SafeFileHandle hStdOut, hStdErr, hStdOutDup, hStdErrDup Private const UInt32 DUPLICATE_SAME_ACCESS = 2 Private const UInt32 STD_ERROR_HANDLE = 0xFFFFFFF4 Private const UInt32 STD_OUTPUT_HANDLE = 0xFFFFFFF5 Private const UInt32 ATTACH_PARENT_PROCESS = 0xFFFFFFFF Out SafeFileHandle lpTargetHandle, UInt32 dwDesiredAccess, Boolean bInheritHandle, UInt32 dwOptions) Private static extern bool DuplicateHandle(IntPtr hSourceProcessHandle, SafeFileHandle hSourceHandle, IntPtr hTargetProcessHandle, Private static extern bool SetStdHandle(UInt32 nStdHandle, SafeFileHandle hHandle) Private static extern SafeFileHandle GetStdHandle(UInt32 nStdHandle) Private static extern bool GetFileInformationByHandle(SafeFileHandle hFile, out BY_HANDLE_FILE_INFORMATION lpFileInformation) Static extern bool AttachConsole(UInt32 dwProcessId) #region Stuff used to attach a command window to the win forms exe Int argCount = args = null ? 0 : args.Length Ĭonsole.WriteLine( "nYou specified ”) event needs to be fired. to demonstrate where the console output is going must be before any calls to Console.WriteLine() redirect console output to parent process Private const int ATTACH_PARENT_PROCESS = -1 Static extern bool AttachConsole( int dwProcessId ) Simple Exampleįollowing is a simple WinForms application that redirects its output to the console window that launched it: using System The special parameter ATTACH_PARENT_PROCESS attaches to the parent process, which in this case is the console window that launched the WinForms application. AttachConsole attaches the current process to the console window of another process.
#CONSOLE APP VISUAL STUDIO 2015 SEE RESULT WINDOWS#
So to redirect output from a WinForms application to the console window that launched it, use the AttachConsole Win32 method introduced in Windows XP. That’s because the console window that launched your WinForms application belongs to the cmd.exe process, which is separate from your WinForms application process. Unfortunately Console.WriteLine()–the standard method of writing to the console window–by default will not work from a WinForms application. And when it does, you probably want to send output messages to the console window that launched your WinForms application. You may wish to enable your WinForms application to run from a console window or command line.
