Note that there are some explanatory texts on larger screens.

plurals
  1. POAttachConsole(-1), but Console.WriteLine won't output to parent command prompt?
    primarykey
    data
    text
    <p>If I have set my program to be a <code>Windows Application</code>, and used the <code>AttachConsole(-1)</code> API, how do I get <code>Console.WriteLine</code> to write to the console I launched the application from? It isn't working for me.</p> <p>In case it is relevant, I'm using Windows 7 x64, and I have UAC enabled. Elevating doesn't seem to solve the problem though, nor does using <code>start /wait</code>.</p> <p><strong>Update</strong></p> <p>Some additional background that might help:</p> <p>I've just discovered that if I go to the command prompt and type <code>cmd /c MyProgram.exe</code>, <strong>Then console output works</strong>. The same is true if I launch a command prompt, open a <code>cmd.exe</code> sub-process, and run the program from that sub-shell.</p> <p>I've also tried logging out and back in, running from a cmd.exe launched from the start menu (as opposed to right-click -> command prompt), and running from <a href="http://sourceforge.net/projects/console/" rel="nofollow noreferrer">a console2 instance</a>. None of those work.</p> <p><strong>Background</strong></p> <p>I've read on other sites and in several SO answers that I can call the win32 API <code>AttachConsole</code> to bind my Windows Application to the console that ran my program, so I can have something that is "both a console application, and a Windows application".</p> <p>For example, this question: <a href="https://stackoverflow.com/questions/6546455/is-it-possible-to-log-message-to-cmd-exe-in-c-net">Is it possible to log message to cmd.exe in C#/.Net?</a>.</p> <p>I've written a bunch of logic to make this work (using several other APIs), and I have gotten every other scenario to work (including redirection, which others have claimed won't work). The only scenario left is to get <code>Console.WriteLine</code> to write to the console I launched my program with. From everything I've read this is supposed to work if I use <code>AttachConsole</code>.</p> <p><strong>Repro</strong></p> <p>Here's a minimal sample - Note that the project is set to be a <code>Windows Application</code>:</p> <pre><code>using System; using System.ComponentModel; using System.Runtime.InteropServices; using System.Windows.Forms; class Program { [STAThread] static void Main(string[] args) { if (!AttachConsole(-1)) { MessageBox.Show( new Win32Exception(Marshal.GetLastWin32Error()) .ToString() ); } Console.WriteLine("Test"); } [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)] private static extern bool AttachConsole(int processId); } </code></pre> <ul> <li>When I run this from a command prompt, I don't get an error, but I don't get any console output either. <em>This is the problem</em></li> <li>If I add extra message boxes anywhere in the execution flow of the app, the message box gets displayed. I expect this, so all good here.</li> <li>When I run this from Visual Studio or by double clicking on it, a message box with an error is displayed. I expect this, so no worries here (will use <code>AllocConsole</code> in my real app).</li> </ul> <p>If I call <code>Marshal.GetLastWin32Error</code> after the call to <code>Console.WriteLine</code>, I get the error "System.ComponentModel.Win32Exception (0x80004005): The handle is invalid". I suspect that attaching to the console is causing <code>Console.Out</code> to get messed up, but I'm not sure how to fix it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload