 |
|
|
 |
|
 |
I've always wanted to do this, a single Google search resulted in this article. Thanks!
//Johannes
|
|
|
|
 |
|
 |
Folks,
Using ::EnableMenuItem ( ::GetSystemMenu ( GetConsoleWindow (), FALSE), SC_CLOSE, MF_BYCOMMAND | MF_GRAYED ); is only graying out the Close button, but the user can right click on the Console & via the edit
menu closing the Console ... so in order to "truely" disable the Close button (including the Close menu) one can use
::RemoveMenu(::GetSystemMenu ( GetConsoleWindow (), FALSE),SC_CLOSE,MF_BYCOMMAND);
For handling the CTRL+C signal , one can use the ::SetConsoleCtrlHandler (HandlerRoutine,TRUE) and providing handler similiar to this
BOOL WINAPI HandlerRoutine(__in DWORD dwCtrlType)
{
switch( dwCtrlType )
{
case CTRL_C_EVENT :
case CTRL_BREAK_EVENT :
return TRUE;
}
return FALSE;
}
|
|
|
|
 |
|
 |
needs updating mention: EnableMenuItem ( GetSystemMenu ( GetConsoleWindow (), FALSE), SC_CLOSE, MF_BYCOMMAND | MF_GRAYED ); Mention the close button issues (and a explanations why) regarding creating the console before loading the GUI framework and possibly offer it bundled in a class that would avoid problems and guarantee a clean exit...
|
|
|
|
 |
|
 |
Hello,
I have a dialog where pressing a button calls to AllocConsole()
showing a console a pressing in another button on my dialog close the console calling FreeConsole(). Everything works fine.
But if the console is closed using its close button then the next error appears:
"user breakpoint called from code at 0x7c901230".
So a possible solution should be disable the close button at the console.
Is anybody knows what to do to avoid the error message when closing the console via close button (x). Or how to disable such a button.
Thanks in advance.
My best regards.
|
|
|
|
 |
|
 |
What about using MFC's TRACE macros and redirect them onto a window when no debugger is present? Paul DiLasca developed such a handy solution with just a simple #include.
(http://pobox.com/~dilascia).
By the way, the code proposed by the previous contributer Michael A. Cornelius was published in the Windows Dev.Journal in Dec. 1997 by Andrew Tucker. I think he sould have at least granted him a credit.
Fran
|
|
|
|
 |
|
 |
I was trying this method for my debug output as well, but there is a problem. When you close the console window by clicking on the close (X) button, it also closes you aplication, calls ExitInstance(). Do you know how to diable this behaviour so that only console window closes.
I tried 'SetConsoleCtrlHandler' to set my handle to trap the CLOSE event but with no avail.
thanks,
Louis
|
|
|
|
 |
|
 |
Hi Louis,
I'm sorry but I don't know. I've tried as way to find a way to avoid of it, but no success. If you should find out how to do it, please be so kind and share it here.
Matthia
|
|
|
|
 |
|
 |
You probably already found a solution, but this is mine:
HWND GetConsoleHwnd()
{
#define MY_BUFSIZE 1024 HWND hwndFound; char pszNewWindowTitle[MY_BUFSIZE]; char pszOldWindowTitle[MY_BUFSIZE];
GetConsoleTitle(pszOldWindowTitle, MY_BUFSIZE);
wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
SetConsoleTitle(pszNewWindowTitle);
Sleep(40);
hwndFound = FindWindow(NULL, pszNewWindowTitle);
SetConsoleTitle(pszOldWindowTitle);
return(hwndFound);
}
BOOL DisableConsoleClose()
{
return RemoveMenu(GetSystemMenu(GetConsoleHwnd(), FALSE),
SC_CLOSE, MF_BYCOMMAND);
}
You may simply call DisableConsoleClose() to disable closing the console window.
I have also developed a solution that allows to use standard methods of writing output and reading input (printf, cin, cout, ...). instead of calling AllocConsole() alone, try this:
BOOL InitConsole()
{
BOOL bRet;
bRet = AllocConsole();
if (bRet)
{
#ifdef _INC_STDIO
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
#endif
#ifdef _INC_IOSTREAM
cin.sync_with_stdio();
cout.sync_with_stdio();
cerr.sync_with_stdio();
clog.sync_with_stdio();
#endif
}
return bRet;
}
Obvoiusly you have to include the right headers (printf should be available without any additional #include).
Cheers,
Paolo
|
|
|
|
 |
|
 |
Hi Paolo,
Your solution is not quite what I am looking for.
I want to be able to close the console window, by clicking on the close button. The problem is that when the console is closed in this manner it will call ExisInstance()thus closing the whole application.
I need a way to trap that message, which can be done with a call to 'SetConsoleCtrlHandler', but I did not have any success with it, just crashes the application if I return TRUE from handler for CLOSE event ????
BTW, Disabling the close button will just prevent clicking on the close button, it will not prevent from typing CTRL+C or CTRL+BREAK which will still close the console.
Louis
|
|
|
|
 |
|
 |
Hi Louis,
I tried myself to catch console messages, with a handler function that just returns TRUE.
I had no sort of problems with both Win2k and Win95.
I could send you a little sample project if you wish.
I do not call FreeConsole() in my ExitInstance override, because I guess it is automatically destroyed by the system when the process terminates.
Cheers,
Paolo
|
|
|
|
 |
|
 |
I would appreciate if you send me your code.
What I made is a simple dialog based test app.
One button to create the console and one buttone to close it. I did not override ExitInstance()
This is my handler:
if( dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT )
{
FreeConsole(); //Close console for these events
return( TRUE );
}
if( dwCtrlType == CTRL_CLOSE_EVENT )
return( TRUE );
return( FALSE );
}
Thanks,
Louis.
|
|
|
|
 |
|
 |
It's very strange... I replaced my handler with yours, but I had no problems.
Pressing CTRL+C or CTRL+BREAK closes the console, as expected.
Trying to close the console window causes the system to pop up a dialog box asking if you really want to close, as stated in MSDN.
I don't know what could be wrong...
Paolo.
|
|
|
|
 |
|
 |
Have you tried using DebugView from www.sysinternals.com
|
|
|
|
 |
|
 |
But as far as I'm concerned this works only on Win 9x versions. In my entire life I've never developed an application on 9x platforms.
Matthia
|
|
|
|
 |
|
|
 |
|
 |
DebugView works on Windows 95, Windows 98, Windows Me, Windows NT 4, Windows 2000 and Whistler Beta 1.
|
|
|
|
 |
|
 |
This allows you to use stdin, stdout, and stderr with the console window with a single function call to RedirectIOToConsole(); You still need to free it at the end, however.
So you can use the standard printf or cout and cin with the pop up console window.
Nice job, though.
#include < process.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <iostream.h>
#include <fstream.h>
#define MAX_CONSOLE_LINES 250;
HANDLE g_hConsoleOut;
void RedirectIOToConsole();
void RedirectIOToConsole()
{
int hConHandle;
long lStdHandle;
CONSOLE_SCREEN_BUFFER_INFO coninfo;
FILE *fp;
AllocConsole();
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE),
&coninfo);
coninfo.dwSize.Y = MAX_CONSOLE_LINES; SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE),
coninfo.dwSize);
g_hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stdout = *fp;
setvbuf( stdout, NULL, _IONBF, 0 );
lStdHandle = (long)GetStdHandle(STD_INPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "r" );
*stdin = *fp;
setvbuf( stdin, NULL, _IONBF, 0 );
lStdHandle = (long)GetStdHandle(STD_ERROR_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );
*stderr = *fp;
setvbuf( stderr, NULL, _IONBF, 0 );
SetConsoleTitle("The Console Titlebar Text");
}
|
|
|
|
 |
|
 |
I haven't seen this possibility. Nice one!
Matthia
|
|
|
|
 |
|
 |
Thanks !
|
|
|
|
 |
|
 |
Don't forget to cite your sources.
Windows Developer Journal, December 1997
Adding Console I/O to a Win32 GUI App
by Andrew Tucker (no relation)
|
|
|
|
 |