Page 1 of 1

Preventing console window close

Posted: Fri Apr 21, 2006 11:27 pm
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()

Posted: Fri Apr 21, 2006 11:36 pm
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()

Posted: Fri Apr 21, 2006 11:56 pm
by Flype
yes - but it's funny because the IDE with debugger enabled do not catch the end of the program.

Posted: Fri Apr 21, 2006 11:58 pm
by Fred
Because it will skip our own handler ;).

Posted: Sat Apr 22, 2006 12:35 am
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..."

Posted: Sat Apr 22, 2006 1:48 am
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)

Posted: Sun Apr 23, 2006 1:40 am
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

Posted: Sat Apr 29, 2006 7:22 pm
by mouse
Thanks for that Sparkie - appreciated!

Re: Preventing console window close

Posted: Thu Feb 16, 2023 3:52 am
by a_carignan
Hello, no example prevents the console from closing. I am with Windows 11.