Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

AttachConsole() - Console.WriteLine() doesn't work sometimes

$
0
0

I know, that this question has been stated before - but I didn't find any answer.

Let's take this simple example:


    static class Program
    {
        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool AttachConsole([MarshalAs(UnmanagedType.U4)] int dwProcessId);

        [STAThread]
        static void Main()
        {
            if (AttachConsole(-1))
                Console.WriteLine(@"AttachConsole() succeeded!");
            else
                MessageBox.Show(@"AttachConsole() failed!");
        }
    }

When I start cmd.exe (For example by Run) - it works - I get "AttachConsole() succeeded".

When I start it in any named window (e.g. Developer Command Prompt) - I get nothing. No console output, no message box. Moreover - when I run Marshal.GetLastWin32Error() - I get 2 (File not found).

Is there any way to lose this problem?


Viewing all articles
Browse latest Browse all 8156

Trending Articles