Using a Console in a GUI application

Hey, I have a windowed application, in this application I am using a lot of math. I need to make sure that I got my formulas correct, but it is difficult to output text in a GUI, so I used AllocConsole to output my data to. The problem is that even though the console is created things like cout and printf dont print to it. So i wanted to know how to get a functional console in a GUI application.
use label to output you text--std::cout it'll only in msvc++ OR use the fltk library.
A few of the expert C++'ers here probably know how to connect std::cout to the new application console, but I wonder why you find troublesome to output data to say, a multiline edit box. You simply use SetWindowText() with the HWND of the edit box and the string to set. Why do you find this difficult? The only "catch" would be to remember that you must accumulate the string yourself. You can easily do this by maintaining a std::string or std::wstring object.
If it's just for some quick debugging, then I don't see anything wrong with using a console. I use a tweaked version of this code.

http://dslweb.nwnexus.com/~ast/dload/guicon.htm
Adding Console I/O to a Win32 GUI App
Windows Developer Journal, December 1997

As also mentioned in this cplusplus.com thread:
Open Console in win32 API program?
http://www.cplusplus.com/forum/beginner/57678/

Andy
PS If you want to write to a file at the same time as the console, you can use an ostream tee.

This one come from daniweb.com: Tee command?
http://www.daniweb.com/software-development/cpp/threads/326447

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once

// tee.h

#include <iostream>

template < typename CHAR_TYPE,
           typename TRAITS_TYPE = std::char_traits<CHAR_TYPE> >
struct basic_teebuf : public std::basic_streambuf< CHAR_TYPE, TRAITS_TYPE >
{
    typedef std::basic_streambuf< CHAR_TYPE, TRAITS_TYPE > streambuf_type ;
    typedef typename TRAITS_TYPE::int_type int_type ;

    basic_teebuf( streambuf_type* buff_a, streambuf_type* buff_b )
            : first(buff_a), second(buff_b) {}

    protected:
        virtual int_type overflow( int_type c )
        {
            const int_type eof = TRAITS_TYPE::eof() ;
            if( TRAITS_TYPE::eq_int_type( c, eof ) )
                return TRAITS_TYPE::not_eof(c) ;
            else
            {
                const CHAR_TYPE ch = TRAITS_TYPE::to_char_type(c) ;
                if( TRAITS_TYPE::eq_int_type( first->sputc(ch), eof ) ||
                    TRAITS_TYPE::eq_int_type( second->sputc(ch), eof ) )
                        return eof ;
                else return c ;
            }
        }

        virtual int sync()
        { return !first->pubsync() && !second->pubsync() ? 0 : -1 ; }

    private:
        streambuf_type* first ;
        streambuf_type* second ;
};

template < typename CHAR_TYPE,
           typename TRAITS_TYPE = std::char_traits<CHAR_TYPE> >
struct basic_teestream : public std::basic_ostream< CHAR_TYPE, TRAITS_TYPE >
{
    typedef std::basic_ostream< CHAR_TYPE, TRAITS_TYPE > stream_type ;
    typedef basic_teebuf< CHAR_TYPE, TRAITS_TYPE > streambuff_type ;

    basic_teestream( stream_type& first, stream_type& second )
         : stream_type( &stmbuf ), stmbuf( first.rdbuf(), second.rdbuf() ) {}

    private: streambuff_type stmbuf ;
};

typedef basic_teebuf<char> teebuf ;
typedef basic_teestream<char> teestream ;


Where is can be used like this (the above site has a different example)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <fstream>
#include "tee.h"

int main( int argc, const char** argv )
{
    if( 2 == argc )
    {
        std::ofstream file( argv[1] ) ;
        teestream teeout( std::cout, file ) ;
        teeout << "Testing\n"
               << "Testing\n"
               << "Testing 1 2 3" << std::endl;
    }
    else
    {
        std::cerr << "This program needs a log file path" << std::endl;
    }

    return 0;
}
Last edited on
closed account (DSLq5Di1)
If it's just for some quick debugging
http://msdn.microsoft.com/en-us/library/windows/desktop/aa363362

If you don't have any experience using a debugger, now is as good a time as any to learn!
http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-2010-A-Beginn
Hey, andywestkin, your first link worked perfectly, thank you. I was going to do what WebJose said and just use a rich edit box before I saw your link thanks.
Last edited on
I think edit boxes are good in some places. And you can't escape from actually debugging when you need to investigate things in detail. But I think that the more tools you have at your displosal, the better.

One tool I've been using more than usual recently is DebugView
http://technet.microsoft.com/en-us/sysinternals/bb896647

This tool can monitor the debug output from all processes which are not already attached to a debugger. I'm using it to monitor a plugin, as it's less intrusive than a full debug session. This is the kind of place I sometimes use the AllocConsole + guicon approach.
Topic archived. No new replies allowed.