Attaching a console or writing output to console Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Attaching a console or writing output to console

Post by purplex88 »

Is it possible to have/attach/create a console window just like we can have a frame window for output without setting the preference

Code: Select all

Console (/SUBSYSTEM:CONSOLE)
in Visual Studio?
User avatar
doublemax
Moderator
Moderator
Posts: 19158
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Attaching a console or writing output to console

Post by doublemax »

If you just want a window for logging information, use wxLogWindow:
http://docs.wxwidgets.org/trunk/classwx_log_window.html

If you need this to create a "hybrid" application that works both as GUI and console application, there is no easy way. Maybe this helps:
http://comp.soft-sys.wxwindows.narkive. ... th-console
Use the source, Luke!
purplex88
Filthy Rich wx Solver
Filthy Rich wx Solver
Posts: 247
Joined: Mon Feb 24, 2014 3:14 pm

Re: Attaching a console or writing output to console

Post by purplex88 »

Can it be used as main or top window of program something like this:

Code: Select all

// For compilers that support precompilation, includes "wx/wx.h".
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

// Declare the application class
// It derives from wxApp class.
class MyApp: public wxApp
{
	public:
	 // Called on application startup
	virtual bool OnInit();
};

// Declare our main frame class
class MyFrame: public wxFrame
{

public:
	// Constructor
	MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);

private:
};

// Give wxWidgets the means to create a MyApp object.
// The use of this allows wxWidgets to dynamically create an instance of the
// application object at the appropriate point in wxWidgets initialization.
wxIMPLEMENT_APP(MyApp);

// A wxWidgets application does not have a main procedure; the equivalent is the
// wxApp::OnInit member defined for a class derived from wxApp.
bool MyApp::OnInit()
{
	wxLogWindow *log_window = new wxLogWindow(NULL, "Log Window", TRUE, TRUE);

	// Doesn't work
	log_window->LogText("Hello World\n");
	
	// Start the event loop
	return true;
}

MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
	
}
It has a few problems:
  • The process doesn't end after I click to close log window.
    It doesn't add the text to log window
    It just disappears after I take any action with it.
PB
Part Of The Furniture
Part Of The Furniture
Posts: 4204
Joined: Sun Jan 03, 2010 5:45 pm

Re: Attaching a console or writing output to console

Post by PB »

I think you might have misunderstood how to use wxWidgets logging framework (i.e. use wxLog* function family, not methods of wxLog classes directly). I would recommend reading wxWidgets Logging Overview first, to see how it is supposed to be used and if it does what you need.

If you can't make wxLogWindow work as the main and only frame of your application (it is supposed to be a background window in an application with a main frame), you can use wxLogTextCtrl with any wxTextCtrl control embedded in a frame of your choice, e.g. something like this

Code: Select all

#include <wx/wx.h>

class MyLogFrame : public wxFrame
{
public:
    MyLogFrame()
        : wxFrame(NULL, wxID_ANY, _("Test"))
    {        
        wxBoxSizer* sizer = new wxBoxSizer(wxVERTICAL);

        wxTextCtrl* logText = new wxTextCtrl(this, wxID_ANY, wxEmptyString, 
            wxDefaultPosition, wxSize(600, 300), wxTE_MULTILINE|wxTE_READONLY);
        
        sizer->Add(logText, 1, wxEXPAND, 5);        
        SetSizer(sizer);
        
        wxLog::SetActiveTarget(new wxLogTextCtrl(logText));
    }	
};


class MyApp : public wxApp
{
public:	
	virtual bool OnInit()
	{
        (new MyLogFrame())->Show();

        wxLogWarning("warning");
        wxLogMessage("message");
        wxLogError("error");        

        return true;
	}
};
IMPLEMENT_APP(MyApp)
Or perhaps you can utilize wxStreamToTextRedirector instead of wxLogTextCtrl and use cout instead of wxLog* for the "console output" just as you would do in a bare C++ console app.
pete_b
Knows some wx things
Knows some wx things
Posts: 41
Joined: Fri Jan 05, 2007 9:52 am

Re: Attaching a console or writing output to console

Post by pete_b »

I know this is an old thread but I was looking for a way to output to the console when registering COM automation interface under Windows.
Use attachConsole before writing to stdout/stderr. Its not ideal - the prompt disappears because the console does not wait for the process to finish (but pressing 'enter' shows it).
Probably the only real way to do this is to have a wrapper executable with subsystem:console.

Code: Select all

void App::attachConsole()
{
	if (attached_console_) {
		return;
	}
#ifdef _WIN32
	if (AttachConsole(ATTACH_PARENT_PROCESS)) {
		attached_console_ = true;
		freopen("CONOUT$", "w", stdout);
		freopen("CONOUT$", "w", stderr);
	}
#endif
}
paddle
Knows some wx things
Knows some wx things
Posts: 43
Joined: Sat Oct 12, 2019 4:57 pm

Re: Attaching a console or writing output to console

Post by paddle »

I know this is a very old thread. But for reference for future readers.

If using Code::Blocks it's possible to have the console along the app.
Go to :
project/properties/Build Targets
There is an dropdown option for "type" if you select console application and rebuild, you will have the app run with the console attached.
Post Reply