News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

You can't prevent a console from being closed

Started by jj2007, December 16, 2021, 10:50:07 PM

Previous topic - Next topic

jj2007

While working on the SSD saver, 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

rsala

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
EC coder

jj2007

Misunderstanding, Ramon: I don't want to exit...

rsala

Oh, I'm sorry. I really misunderstood your post (sometimes I read too fast :sad:)
EC coder