

Private static extern bool SetStdHandle(StandardHandle nStdHandle, IntPtr handle) Private static extern IntPtr GetStdHandle(StandardHandle nStdHandle) Var d = new StringArgReturningVoidDelegate(SetText) If these threads are different, it returns true. calling thread to the thread ID of the creating thread. InvokeRequired required compares the thread ID of the Write to your UI object in thread safe way: Public override Encoding Encoding => Encoding.ASCII Public override void WriteLine(string value) Public override void WriteLine(char value) Public RichTextBoxWriter(RichTextBox richTexttbox) Private readonly RichTextBox _richTextBox Public class RichTextBoxWriter : TextWriter Public delegate void StringArgReturningVoidDelegate(string text) Private void yourRichTextBox_TextChanged(object sender, EventArgs e) To ensure that your RichTextBox object is scrolled down when its text is From your application set the Console to write to your RichTextkBoxĬonsole.SetOut(new RichTextBoxWriter(yourRichTextBox)) _stdOutWriter = new StreamWriter(stdout) Var stdout = Console.OpenStandardOutput()

I guess it probably does write somewhere, but nowhere I can find out about If the output is not redirected we still get a valid stream but it doesn't appear to write anywhere this needs to happen before attachconsole. this must be called early in the program Private const int ATTACH_PARENT_PROCESS = -1 Private static extern bool AttachConsole(int dwProcessId) Public class GUIConsoleWriter : IConsoleWriter write to the console) but it doesn't seem possible. It would be better to do the relevant thing (eg write to the redirected file if there is one, otherwise This always writes to the parent console window and also to a redirected stdout if there is one. This does mean that the output is sent to the console window and to the pipe but its the best solution I could find. This only works if the output is piped, so if you want to handle both of the scenarios you need to open the standard output and write to it and attach to the console window. To make this work you can call Console.OpenStandardOutput() to get a handle to the stream that the output should be piped to. This is when someone pipes the output from your program somewhere else, eg.Īttaching to a console window in this case effectively ignores the piping. You can use start with the /wait parameter to handle this I think. One gotcha to this approach is that the program returns control to the console window immediately, and then carries on writing to it, so the user can also type away in the console window. Once attached to the console window Console.WriteLine() etc works as expected. It is possible for a winforms program to attach itself to the console window that created it (or to a different console window, or indeed to a new console window if desired). There are basically two things that can happen here. I thought is was worthy to post it here as well. String output = typeOfBuild + Assembly.GetExecutingAssembly() MessageBox.Show("Attach the debugger now") / The main entry point for the application.

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) 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 Private static extern bool DuplicateHandle( Private static extern bool SetStdHandle(UInt32 nStdHandle, SafeFileHandle hHandle) Private static extern SafeFileHandle GetStdHandle(UInt32 nStdHandle) Out BY_HANDLE_FILE_INFORMATION lpFileInformation Private static extern bool GetFileInformationByHandle( Static extern bool AttachConsole(UInt32 dwProcessId)
Windows 10 does not show sharp printers code#
This worked for me, to pipe the output to a file.Ĭmd /c "C:\path\to\your\application.exe" > myfile.txtĪdd this code to your application.
