CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Aug 2005
    Posts
    5

    System::Console::WriteLine not working in Release Build

    I have a Windows Forms app that is compiled using /subsystem:windows, and thus does not have a natural console.

    In Form1.cpp, between
    System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA;
    Application::Run(new Form1());

    I create a console using
    AllocConsole()
    which completes without error, and
    then i added
    System::Console::WriteLine("Hello, World!");

    This works in Debug Mode (started via the Debugger, or via Windows Explorer), and in Release Mode when started via the Debugger (i.e. press F5), but not when Release Mode is started via the Windows Explorer?

    Whats up with this? Is this a bug?

    I thought it might be buffering, but adding calls to
    System::Console::OpenStandardOutput()->Flush();
    didn't make any difference.

    I'm stumped here. I can use GetStdHandle(STD_OUTPUT_HANDLE), and _open_osfhandle/ _fdopen to use fprintf() instead, but I'd prefer to work through the framework...

  2. #2
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: System::Console::WriteLine not working in Release Build

    If you are under windows subsystem why would you need to write to a console? Why not just do logging to a text file? Why can also use 'SetOut' static member of System::Console to redirect all System::Console outputs to a specified TextWriter*. So you can easily create a TextWriter which write's to file and combine it with the Standard Output. So you don't have to change all code lines using console output.

    BTW: Are you using WinAPI function AllocConsole()?
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  3. #3
    Join Date
    Aug 2005
    Posts
    5

    Re: System::Console::WriteLine not working in Release Build

    Yes, I am using AllocConsole().

    I can redirect to a file, and have done this.

    I've also used the console handle to write to the console using fprintf().

    I'm just baffled as to why System::Console::WriteLine() would work with Release build started from the debugger, and not from explorer. Any ideas?

  4. #4
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: System::Console::WriteLine not working in Release Build

    At first you cannot use a handle returned by a Windows API function like AllocConsole along with C style functions like fprintf. fprintf require a FILE* object previously created by fopen(). And there is no need to call AllocConsole because the .NET framework should do that for you.

    Why would you like to use a console when you use a SubSystem:Windows application?

    I have not created an example application: At first I try to use SubSystem:Windows and then normal Console methods. At first no console window was created. But when it comes for Console::WriteLine() a new console window was created for both configurations. Then I tried to log to a file using TextWriter*. Both worked just fine.

    So I ask you, what actually is going wrong?
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  5. #5
    Join Date
    Aug 2005
    Posts
    5

    Re: System::Console::WriteLine not working in Release Build

    Okay, if I *don't* call AllocConsole(), then I definitely don't get a console window appearing at all - the framework does not create one for me.

    What goes wrong is that in release build, started from Explorer or a CMD prompt, the console window is created but System::Console::WriteLine() doesn't output anything to it.

    In release build started from the IDE, System::Console::WriteLine() works, as it does for debug build started in any fashion.

    I'm using VStudio Pro 2003 V7.1.3088, .NET Framework 1.1.4322 SP1

  6. #6
    Join Date
    Aug 2005
    Posts
    5

    Re: System::Console::WriteLine not working in Release Build

    BTW, as I mentioned, I used a combination GetStdHandle(STD_OUTPUT_HANDLE), and _open_osfhandle/ _fdopen to use fprintf() instead, I *know* I can't just use a HANDLE to fprintf() as the FILE* stream...

  7. #7
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: System::Console::WriteLine not working in Release Build

    Why would you need a console window from a Subsystem:Windows? To input something? Why not using Forms and a textbox? To output something? Why not logging to a file or create a simple Form with a textbox and write the log there?
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  8. #8
    Join Date
    Feb 2005
    Posts
    106

    Re: System::Console::WriteLine not working in Release Build

    I'm just baffled as to why System::Console::WriteLine() would work with Release build started from the debugger, and not from explorer. Any ideas?
    That would be because the debugger creates a console and attaches it to the process automatically.
    Whats up with this? Is this a bug?
    Nope. That's how win32 applications work...

    You will need to call AllocConsole() or use some other mechanism to gain access to a console because the framework will not allocate one for you.

    Regards.

  9. #9
    Join Date
    Aug 2005
    Posts
    5

    Re: System::Console::WriteLine not working in Release Build

    I know the framework won't allocate me one, so I am explicitly calling AllocConsle();

    I then call System::Console::WriteLine("blah"); but it doesn't work in release build started from explorer.

    It works in release build started from debugger, and in debug build started anyway you like. This is my problem.

  10. #10
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: System::Console::WriteLine not working in Release Build

    Quote Originally Posted by capt_jerry5
    I know the framework won't allocate me one, so I am explicitly calling AllocConsle();

    I then call System::Console::WriteLine("blah"); but it doesn't work in release build started from explorer.

    It works in release build started from debugger, and in debug build started anyway you like. This is my problem.
    Once again, why would you need a console window in a SubSystem:Windows application? If you need a console window call ComSpec and/or use SubSystem:Console.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  11. #11
    Join Date
    Feb 2005
    Posts
    106

    Re: System::Console::WriteLine not working in Release Build

    Greetings again.

    I then call System::Console::WriteLine("blah"); but it doesn't work in release build started from explorer.

    It works in release build started from debugger, and in debug build started anyway you like. This is my problem.
    Again, I want to stress that the framework does not allocate a console for an application built with the windows subsystem. Your call to Console::WriteLine() is having the application use a console handle that is not valid (there's no internal allocation of a console, so the handle is NULL). In order to use a console that is allocated to your application manually through the use of AllocConsole() or other native call, you must also use a native call to write/read from it (such as WriteConsole(), ReadConsole()).

    Hope this helps.

    Regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured