Preventing console window close

Just starting out? Need help? Post your questions and find answers here.
mouse
New User
New User
Posts: 9
Joined: Sun Mar 05, 2006 3:25 pm
Location: Isle of Wight, UK
Contact:

Preventing console window close

Post by mouse »

Hi, is there any way to prevent a user from closing the console window while a program is running? I'm still very new to Windows programming, so I guess I'm missing something basic here! After reading various threads and articles I've tried the following two methods, neither of which work. Any help or hints would be welcome :)

Attempt 1
Trap the close window event.
I've found out how to do something before the window closes, but can't figure out how to ignore the close event. The window still closes after console_handler() is called

Code: Select all

EnableExplicit

Procedure console_handler(my_event.l) 
  If my_event = #CTRL_CLOSE_EVENT 
    PrintN("Close in 3 seconds... Can't stop it!") 
    Delay(3000) 
  EndIf 
EndProcedure 

OpenConsole()
SetConsoleCtrlHandler_(@console_handler(), 1) 
PrintN("Try and stop the console closing...Close the window now")
Input()
CloseConsole()
Attempt 2
Disable the close button
If the close button and system menu option are disabled the user can't easily close the window. The code below does not work.
con_handle shown as 7
system_menu shown as 0

Code: Select all

EnableExplicit

Global con_handle.l
Global system_menu.l

OpenConsole()

con_handle = GetStdHandle_(#STD_OUTPUT_HANDLE)
Debug "con_handle = " + Str(con_handle)

system_menu = GetSystemMenu_(con_handle, #False)
Debug "system_menu = " + Str(system_menu)

EnableMenuItem_(system_menu, #SC_CLOSE, #MF_BYCOMMAND | #MF_DISABLED) 
DrawMenuBar_(con_handle) 

PrintN("Close window should be disabled but isn't")
 
Input()
CloseConsole()
Win XP Pro 2002 SP2 : 1Gb memory : Sempron 3400+ : GeForce 6600 GT
Out of Chocolate Error - Reboot Universe.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

If you return #True to the event callback, it should do the trick:

Code: Select all

Procedure console_handler(my_event.l)
  If my_event = #CTRL_CLOSE_EVENT
    PrintN("Close in 3 seconds... Can't stop it!")
    ;Delay(3000)
  EndIf
  ProcedureReturn #True
EndProcedure

OpenConsole()
SetConsoleCtrlHandler_(@console_handler(), 1)
PrintN("Try and stop the console closing...Close the window now")
Input()
CloseConsole()
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

yes - but it's funny because the IDE with debugger enabled do not catch the end of the program.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Because it will skip our own handler ;).
mouse
New User
New User
Posts: 9
Joined: Sun Mar 05, 2006 3:25 pm
Location: Isle of Wight, UK
Contact:

Post by mouse »

Fred wrote:If you return #True to the event callback, it should do the trick:
Thanks Fred, getting closer, but not quite there. Returning #True doesn't close the window, but Windows gives a dialog asking to [End Now] or [Cancel].

"Windows cannot end this program. It may need more time to complete an operation..."
Win XP Pro 2002 SP2 : 1Gb memory : Sempron 3400+ : GeForce 6600 GT
Out of Chocolate Error - Reboot Universe.
mouse
New User
New User
Posts: 9
Joined: Sun Mar 05, 2006 3:25 pm
Location: Isle of Wight, UK
Contact:

Post by mouse »

If it helps, the follwing illustrates that the #CTRL_C_EVENT and #CTRL_BREAK_EVENT work ok (as suggested by Fred), but the #CTRL_CLOSE_EVENT causes windows to think the program is trying to close but timed out. I don't know if this is a bug or a coding problem.

Don't know if the console handler should return a long, word or byte (.l .w .c) - I've tried all 3 with the same results.

Code: Select all

EnableExplicit

Global counter.l

Procedure.l console_handler(my_event.c)

Protected my_return.l

  my_return = #False
	
  Select my_event
	
    Case  #CTRL_C_EVENT
      PrintN("CTRL_C_EVENT : count = " + Str(counter)) 
      counter = counter + 1
      my_return = #True
			
    Case  #CTRL_BREAK_EVENT
      PrintN("CTRL_BREAK_EVENT : count = " + Str(counter))
      counter = counter + 1
      my_return = #True
			
    Case  #CTRL_CLOSE_EVENT
      PrintN("CTRL_CLOSE_EVENT : count = " + Str(counter))
      counter = counter + 1
      my_return = #True
			
  EndSelect
	
  ProcedureReturn my_return

EndProcedure 

counter = 1

OpenConsole()

SetConsoleCtrlHandler_(@console_handler(), 1)

PrintN("Try to close the window, CTRL-C or CTRL-BREAK 5 times...")

While counter <= 5
  Delay(100)
Wend

CloseConsole()
(I'm compiling this on PB4 B11 without debug)
Win XP Pro 2002 SP2 : 1Gb memory : Sempron 3400+ : GeForce 6600 GT
Out of Chocolate Error - Reboot Universe.
Sparkie
PureBatMan Forever
PureBatMan Forever
Posts: 2307
Joined: Tue Feb 10, 2004 3:07 am
Location: Ohio, USA

Post by Sparkie »

This quick conversion from http://support.microsoft.com/default.as ... -us;818361 disables the Console close button and removes the X Close from the system menu.

Code: Select all

If OpenConsole() 
  consoleTitle$ = "MyConsole"
  ConsoleTitle(consoleTitle$)
  con_handle = FindWindow_(0, @consoleTitle$)
  If con_handle
    system_menu = GetSystemMenu_(con_handle, #False) 
    DeleteMenu_(system_menu, 6, #MF_BYPOSITION)
    DrawMenuBar_(con_handle) 
    PrintN("Close window (X) should now be disabled. Press [Enter] to close.")
  Else
    PrintN("Unable to disable Close button (X).")
  EndIf
  Input() 
  CloseConsole() 
EndIf
End
What goes around comes around.

PB 5.21 LTS (x86) - Windows 8.1
mouse
New User
New User
Posts: 9
Joined: Sun Mar 05, 2006 3:25 pm
Location: Isle of Wight, UK
Contact:

Post by mouse »

Thanks for that Sparkie - appreciated!
Win XP Pro 2002 SP2 : 1Gb memory : Sempron 3400+ : GeForce 6600 GT
Out of Chocolate Error - Reboot Universe.
User avatar
a_carignan
User
User
Posts: 98
Joined: Sat Feb 21, 2009 2:01 am
Location: Canada

Re: Preventing console window close

Post by a_carignan »

Hello, no example prevents the console from closing. I am with Windows 11.
Post Reply