C++ disable console close button.

Home built / K9a2 cf-f
August 15, 2009 at 06:11:15
Specs: Windows XP, Athlon xp x2 64 5600 / 2gb
I'm writing a win32 (well 2000 and above) console app and want
to disable the close button, the reason being that I want
to be sure all data entered is saved before exit. I'm using

The below code is what is giving me trouble, it disables the
x on the top corner but the alt+spacebar/click on top left hand
corner menu reactivates it along with having close on the
menu it's self active:

#define _WIN32_WINNT 0x0500
#include <windows.h>
int main() {
    EnableMenuItem(GetSystemMenu(GetConsoleWindow(), FALSE), SC_CLOSE , MF_GRAYED);
    system ("pause");

Simply changing "EnableMenuItem" to "RemoveMenu" does work but
since it's a console app I want to restore the default menus,
size, colour, ect. on exit.

GetSystemMenu(GetConsoleWindow(), TRUE);

Comes close to restoring it but it's not the default console
menu, just a generic menu.

Can anyone please point me to either a way to disable close
properly or even restore the default console menu when I'm

See More: C++ disable console close button.

Report •

August 17, 2009 at 20:55:18
Have you tried just calling FreeConsole() and AllocConsole() to spawn your own console window? That way, you don't have to worry about resetting the menu, nor hooking the console.

Report •

August 18, 2009 at 01:01:14
Thanks Razor2.3, I didn't even know about FreeConsole() and AllocConsole(). It works perfectly.

Now I can just remove the close command as their will always be some sort of a custom field based on getch() that detects esc or alt+f4.

Thanks again.

Report •

Related Solutions

Ask Question