The MASM Forum

General => The Workshop => Topic started by: jj2007 on December 16, 2021, 10:50:07 PM

Title: You can't prevent a console from being closed
Post by: jj2007 on December 16, 2021, 10:50:07 PM
While working on the SSD saver (http://masm32.com/board/index.php?topic=9704.0), I stumbled over this:

You can prevent a user from closing the console window by pressing Ctrl C, but you cannot prevent closing the console by clicking into the close box. This is by design - thank you, Redmond. Google for CTRL_CLOSE_EVENT SPI_GETHUNGAPPTIMEOUT to understand why.

MyHandler proc cType ; user presses Ctrl C in console
  .if cType==CTRL_C_EVENT || cType==CTRL_CLOSE_EVENT
print cfm$("\nTrying to close the console, right?")
invoke Sleep, 60000 ; one minute - in theory!
or eax, -1
  .endif
  ret
MyHandler endp

invoke SetConsoleCtrlHandler, MyHandler, 1 ; delays console closing by 5 seconds
Title: Re: You can't prevent a console from being closed
Post by: rsala on December 17, 2021, 07:58:58 PM
Hi,

Try to put the following code before exiting the process:

.Data

szBuffer  DB  "Press <Enter> to exit", 0

.Code
   Invoke GetStdHandle, STD_INPUT_HANDLE
   Mov Rcx, Rax
   Invoke ReadConsole, Rcx, Addr szBuffer, 21, Addr lpOuput, NULL ; 21 = length of szBuffer
Title: Re: You can't prevent a console from being closed
Post by: jj2007 on December 17, 2021, 08:42:52 PM
Misunderstanding, Ramon: I don't want to exit...
Title: Re: You can't prevent a console from being closed
Post by: rsala on December 18, 2021, 11:32:42 PM
Oh, I'm sorry. I really misunderstood your post (sometimes I read too fast :sad:)