-
August 24th, 2005, 11:11 AM
#1
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...
-
August 24th, 2005, 11:16 AM
#2
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()?
-
August 24th, 2005, 11:32 AM
#3
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?
-
August 24th, 2005, 11:46 AM
#4
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?
-
August 24th, 2005, 01:12 PM
#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
-
August 24th, 2005, 01:15 PM
#6
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...
-
August 24th, 2005, 01:24 PM
#7
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?
-
August 25th, 2005, 05:29 PM
#8
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.
-
August 26th, 2005, 06:27 AM
#9
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.
-
August 26th, 2005, 06:48 AM
#10
Re: System::Console::WriteLine not working in Release Build
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.
-
August 29th, 2005, 04:57 PM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|