Article View: borland.public.cppbuilder.nativeapi
Article #8017Console Window for GUI application
From: Richard Ulrich
Date: Tue, 07 Dec 2004 16:23
Date: Tue, 07 Dec 2004 16:23
88 lines
3748 bytes
3748 bytes
I recently converted a commandline application to a GUI application.
Now it would be nice to display all the cout's in a commandline window
for debugging purposes.
I found some code to do just that. The code is more or less the same on
different sources in the internet, and they all say it runs in MSVC. I
don't see any reason why it souldn't work in Broland.
It opens that commandline window, and I don't get any errormessages, but
it doesnt' print out any of the cout's. Also printf doesn't write to
that commandline window.
Any hints?
Richard
And here's the code:
class DebugConsole
{
public:
DebugConsole();
~DebugConsole();
private:
bool m_exists;
};
using namespace std;
typedef long intptr_t;
//---------------------------------------------------------------------------
DebugConsole::DebugConsole()
{
int ret;
m_exists = ::AllocConsole() != 0;
if(!m_exists)
::MessageBox(0, "Couldn't create debug console!", "Error",
MB_ICONERROR | MB_OK);
//sync stdin, stdout, stderr fds up with the new console
//original source: http://www.halcyon.com/ast/dload/guicon.htm
//changes: removed all warnings by changing types and casting
// set the screen buffer to be big enough to let us scroll text
CONSOLE_SCREEN_BUFFER_INFO coninfo;
::GetConsoleScreenBufferInfo(::GetStdHandle(STD_OUTPUT_HANDLE), &coninfo);
coninfo.dwSize.Y = 500;
::SetConsoleScreenBufferSize(::GetStdHandle(STD_OUTPUT_HANDLE),
coninfo.dwSize);
// redirect unbuffered STDOUT to the console
intptr_t pStdHandle =
reinterpret_cast<intptr_t>(::GetStdHandle(STD_OUTPUT_HANDLE));
int hConHandle = _open_osfhandle(pStdHandle, _O_TEXT);
FILE* pFile = _fdopen(hConHandle, "w");
*stdout = *pFile;
ret = setvbuf(stdout, NULL, _IONBF, 0);
// redirect unbuffered STDIN to the console
pStdHandle = reinterpret_cast<intptr_t>(::GetStdHandle(STD_INPUT_HANDLE));
hConHandle = _open_osfhandle(pStdHandle, _O_TEXT);
pFile = _fdopen(hConHandle, "r");
*stdin = *pFile;
ret = setvbuf(stdin, NULL, _IONBF, 0);
// redirect unbuffered STDERR to the console
pStdHandle = reinterpret_cast<intptr_t>(::GetStdHandle(STD_ERROR_HANDLE));
hConHandle = _open_osfhandle(pStdHandle, _O_TEXT);
pFile = _fdopen(hConHandle, "w");
*stderr = *pFile;
ret = setvbuf(stderr, NULL, _IONBF, 0);
// make cout, wcout, cin, wcin, wcerr, cerr, wclog and clog
// point to console as well
ios::sync_with_stdio(true);
ret = ret;
}
//---------------------------------------------------------------------------
DebugConsole::~DebugConsole()
{
if(m_exists)
{
::FreeConsole();
m_exists = false;
}
}
Technical Details
Message-ID:
<41b5d8dc@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news2.euro.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,29bc3d9d57e77864,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Tue, 07 Dec 2004 17:23:14 +0100 From: Richard Ulrich <ulrich_tcginf@bluewin.ch> User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax) X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: borland.public.cppbuilder.nativeapi Subject: Console Window for GUI application Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 212.4.80.215 Message-ID: <41b5d8dc@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1102436572 212.4.80.215 (7 Dec 2004 08:22:52 -0800) Lines: 89 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!news2.euro.net!216.196.110.149.MISMATCH!border2.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!pe2.news.blueyonder.co.uk!blueyonder!news-peer-test!btnet!newsfeed.bt.es!caladan.landsraad.net!newsgroups.borland.com!not-for-mail