Code::Blocks Forums

User forums => Help => Topic started by: cpprooky on August 02, 2006, 03:25:40 pm

Title: Small bug in Win GUI Project wizard
Post by: cpprooky on August 02, 2006, 03:25:40 pm
When I create à Win32 GUI project, in the Project properties, the build target type is set to "Console application" instead of "GUI application" ! This is not a very important bug, I agree, but that's boring to have to change that setting for every new project !
could you change that easily ?
Title: Re: Small bug in Win GUI Project wizard
Post by: MortenMacFly on August 02, 2006, 05:14:10 pm
could you change that easily ?
No, because it's not a bug but a feature. If you look into it, only the debug target is set to be a console project. This is on purpose as many debug information you issue within a GUI application are actually send to the console. This is a usual concept (have a look at several debug macros) and if you declare the debug target as GUI application you would not see the messages printed to the console.
So we won't chnage this because it's by design. Anyway, if you don't like this feature you can disable it in the wizard script easily. Just right-click on the script, edit it and change the debug target creation to how it's done in the release target:
Change the line:
Code
        target.SetTargetType(ttConsoleOnly); // ttConsoleOnly: console for debugging
into:
Code
        target.SetTargetType(ttExecutable); // ttExecutable: no console

With regards, Morten.
Title: Re: Small bug in Win GUI Project wizard
Post by: tiwag on August 02, 2006, 09:58:23 pm
you have to say additionally, that debug info sent to the console stdout is working in linux,
but not in windows, where this console is quite useless.
(there are existing some tricks, how you can redirect stdout to this console window,
but it is not standard and doesn't work with the supplied samples)
Title: Re: Small bug in Win GUI Project wizard
Post by: MortenMacFly on August 02, 2006, 10:18:49 pm
(there are existing some tricks, how you can redirect stdout to this console window,
but it is not standard and doesn't work with the supplied samples)
That's right, I for myself use a concept similar to http://www.codeproject.com/debug/debugcon.asp (http://www.codeproject.com/debug/debugcon.asp) for debugging to the console within a Win32 application but yes, within the Win32GUI example this is not done, indeed.

(Edit: What I mean hereby is that there are often other methods used within Win32 indeed.)
Title: Re: Small bug in Win GUI Project wizard
Post by: takeshimiya on August 03, 2006, 02:16:55 am
A solution to redirect output to the console in Windows is:

Code: cpp
freopen("CON", "wt", stdout); // redirects stdout
freopen("CON", "wt", stderr); // redirects stderr
Title: Re: Small bug in Win GUI Project wizard
Post by: tiwag on August 03, 2006, 04:35:31 am
(there are existing some tricks, how you can redirect stdout to this console window,
but it is not standard and doesn't work with the supplied samples)
That's right, I for myself use a concept similar to http://www.codeproject.com/debug/debugcon.asp (http://www.codeproject.com/debug/debugcon.asp) for debugging to the console within a Win32 application but yes, within the Win32GUI example this is not done, indeed.

(Edit: What I mean hereby is that there are often other methods used within Win32 indeed.)

what i've meant was, that in linux you can use a number of puts("debug message"); in your GUI program
and you'll see them printed in the console (if it exists or you start your program from a console)
and you can redirect them to a file for your debug purposes.

in windoze this does not work by default, if you doesn't create your own debug-console in your program,
you have to catch the already existing consoles PID and then you can redirect stdout to that window ...
then it "works"

i saw some code for this in the past somewhere, but i can't find it at the moment ... i have to look for again.
Title: Re: Small bug in Win GUI Project wizard
Post by: mandrav on August 03, 2006, 08:17:17 am
i saw some code for this in the past somewhere, but i can't find it at the moment ... i have to look for again.

Code: cpp
void CodeBlocksApp::InitDebugConsole()
{
#ifdef __WXMSW__
    #ifdef __CBDEBUG__
        // Remember to compile as a console application!
        AllocConsole();
        HANDLE myhandle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD co = {80,2000};
SetConsoleScreenBufferSize(myhandle, co);
fprintf(stdout,"CONSOLE DEBUG ACTIVATED\n");
// wxLogWindow *myerr = new wxLogWindow(NULL,"debug");
    #endif
#endif
}
Title: Re: Small bug in Win GUI Project wizard
Post by: tiwag on August 03, 2006, 08:25:31 am
@mandrav
your code produces a new console window in any case, isn't it ?

there exists another trick that you compile your program as normal GUI program,

then it checks, if it was started from a console window,
and if this is the case, it redirects stdout & stderr to this
already existing console window.

then you have a normal gui program,
and when started from a console, you get all your debug messages
which you simply send with puts() or printf()

Title: Re: Small bug in Win GUI Project wizard
Post by: cpprooky on August 03, 2006, 09:13:36 am
I see ! That's OK for me
I must say that your last messages goes widely over my knowledges !! So, I'll accept this is a good thing for debug target !!
Title: Re: Small bug in Win GUI Project wizard
Post by: tiwag on August 03, 2006, 11:12:16 am
ok i've found the solution in order to give Win32-GUI-apps a linux-like behaviour ( but it needs at least WinXP )

with AttachConsole()
Code
    AttachConsole(ATTACH_PARENT_PROCESS);
    freopen("CON", "wt", stdout); // redirects stdout
you can attach the GUI app to an existing console window, if there exists one,
with other words, if it was started from the commandline from a console window.

attached is a sample project which demonstrates this

run the Release build from a console window and enjoy the debug messages :D


the code which i can't find anymore did that AttachConsole-stuff also for older Windozes like Win98, ME, NT etc...
if you use MinGW-win32api-3.7 the headers already contain the needed defines,
otherwise i've added them to the code and you need to enable them.

[attachment deleted by admin]
Title: Re: Small bug in Win GUI Project wizard
Post by: hairy on November 10, 2006, 06:39:59 pm
can u teach me how to use this GUI application