2

I have a C# application which calls a native DLL. The native DLL calls AllocConsole to create a new console then WriteConsole to write to it. This behavior works fine when the application is run without a debugger attached. But when it is run with Visual Studio's managed debugger attached, the new console window still pops up when AllocConsole is called but nothing is written to it when I call WriteConsole.

It would seem like the debugger is intercepting the console commands and sending them to VS, but the text doesn't appear in any VS window that I can see (Output or Intermediate.) I can write to the VS windows intentionally by calling OutputDebugString, but I would really like to be able to write to the console made with AllocConsole, since I use functions like SetConsoleCursorPosition to let me overwrite the current console line. At the moment my work-around is something like

if(debuggerAttached) OutputDebugString(...) else WriteConsole(...)

Which is fairly inelegant and doesn't give me the functionality I want. Any ideas on how to get the VS debugger to not intercept the C++ console events? This problem doesn't occur when I'm in a purely native environment.

Matt Fisher
  • 213
  • 1
  • 9

1 Answers1

0

Try using this piece of code before writing anything to console.

//AllocConsole() is called
StreamWriter standardOutput = new StreamWriter(Console.OpenStandardOutput());
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);

Here is the question I asked recently, which may be of use to you, there is also an answer, which explains the code above.

Community
  • 1
  • 1
Dmitrii Erokhin
  • 1,347
  • 13
  • 31
  • Unfortunately this doesn't seem to have any effect, but I will keep experimenting. I tried calling FreeConsole() first in case the managed debugger is automatically attaching it to a different console, but this also doesn't fix the problem. – Matt Fisher Nov 25 '11 at 19:38