 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
iwl Guest
|
Posted: Mon Apr 16, 2007 2:57 pm Post subject: stdout OutputConsole for WindowsApplikation |
|
|
Hi,
know somebody an example for Opening an Console and redirecting stdout
from an Borland VCL GUI App?
I've found the appended on Borland pages but this seems not to
redirect all layers of output (CRT) and also makes sometimes problems
being the first console opened (no output then).
//---------------------------------------------------------------------------
#ifndef ConsoleH
#define ConsoleH
/
*****************************************************************************
class
TConsole
==============================================================================
GUI application's console for Windows32 applications.
Redirects stdout, stderr and stdin as well as std::cin, std::cout,
std::cerr
and std::clog to the GUI application's console window.
Open the console by calling CreateApplicationConsole();
Close the console by calling CloseApplicationConsole();
These two functions use reference-counting. That is, first call to
CreateApplicationConsole() will open the console. Each subsequent call
will
increment internal reference counter. CloseApplicationConsole() will
decrement
internal reference counter. If it drops to zero, it will close the
console.
The console will be closed when application closes normaly in any
case.
Behind the curtians ( implementation in the cpp file ) static
reference
instance of the TConsole class is used. Therefore, console object will
be opened at last on first call to any of functions below. It will be
destroyed
somewhere in the sequence of destroying globals.
If you want to use console for as much time as possible, define macro
#define CONSOLE_USE_SINGELTON
and recompile. In that case, the console class will be created on the
heap
and never destroyed ( wanted memory leak ) .
--------------------------
Implementation in
Console.cpp
--------------------------
Documentation in
<na>
--------------------------
Namespace
<>
--------------------------
Required and used files:
<>
--------------------------
Notes
1)
In the cpp file:
Standard C++ headers are included the old way <*.h> for compatibilty
with
older compilers. You may safely change them to <*> format and
redeclare
all related variables to std::*.
2) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
IMPORTANT NOTES if used with CodeGuard in Borland C++ Builder:
1) This class may hang your application if CodeGuard is enabled.
To run your application with CodeGuard enabled, disable function
tracking
for al least these functions:
_open_osfhandle
_read
scanf
setbuf
setvbuf
vfprintf
You may experience some truble with other functions too. Disable
tracking as
needed or recompile in separate module and disable CodeGuard tracking
for this module.
2) It is possible that console window will close and reapear if any IO
is
called in the destructor of the main Form's class of later especially
if this sourec is either placed in or called from static or dynamic
libraray.
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
--------------------------
Version
1.01
--------------------------
Portability
Windows 32
--------------------------
Author
Goran Bervar,
--------------------------
Copyleft
If you ask me, you may do whatever you want with this code. However
most
parts of the code are based on examples from Microsoft (r) support
site.
You shuold read MS Licence agreement than I suppose.
I don't take any responsibility at all. For nothing.
Creation Date
june 2000
--------------------------
History:
==============================================================================
*****************************************************************************/
#include <windows.h> /* HWND */
//CONSOLE CREATING FLAGS, or-ed value can be used with
CreateApplicationConsole()
#define CONSOLE_TOPMOST 0x01 //Places the console window above all
non-topmost windows
#define CONSOLE_VERBOSE 0x02 //Print out debug messages
#define CONSOLE_KEY_ON_CLOSE 0x04 //wait for user to press button
before closing the console
#define CONSOLE_REDIRECT_STDIN 0x08 //redirect stdin too ( not by
default )
//this flag is IGNORED if console is
//already opened when calling
//CreateApplicationConsole()
//Create GUI application's console.
//If reference counter is 0, it will actually open the console window
using
//flags.
//Than, reference counter is incremented.
//If reference counter is greater than 0
//( the application already has a console opened )
//this function will only set new flags
DWORD CreateApplicationConsole(const char* Caption = 0, short flags =
0);
//Close GUI application's console. If no console is opened call is
ignored
//Fisrt, reference couner is decremented.
//If reference count is less or equal to 0, console is actually closed
int CloseApplicationConsole();
//Position GUI application's console to x, y position.
//If no console is opened call is ignored
void PositionConsole(int x, int y);
//Retrun GUI application's console window handle
//If no console is opened it return NULL
HWND ConsoleWindowHandle();
//Bring GUI application's console window to front
//(Places the window at the top of the Z order)
void ConsoleBringToFront();
//Make GUI application's console window topmost ( on_top == true)
//or not-topmost ( on_top == false )
void ConsoleOnTop( bool on_top = true);
//---------------------------------------------------------------------------
#endif |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|