Author Topic: You can't prevent a console from being closed  (Read 577 times)

jj2007

  • Member
  • *****
  • Posts: 12487
  • Assembler is fun ;-)
    • MasmBasic
You can't prevent a console from being closed
« on: December 16, 2021, 10:50:07 PM »
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.

Code: [Select]
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

  • Member
  • ***
  • Posts: 348
    • Easy Code
Re: You can't prevent a console from being closed
« Reply #1 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
EC coder

jj2007

  • Member
  • *****
  • Posts: 12487
  • Assembler is fun ;-)
    • MasmBasic
Re: You can't prevent a console from being closed
« Reply #2 on: December 17, 2021, 08:42:52 PM »
Misunderstanding, Ramon: I don't want to exit...

rsala

  • Member
  • ***
  • Posts: 348
    • Easy Code
Re: You can't prevent a console from being closed
« Reply #3 on: December 18, 2021, 11:32:42 PM »
Oh, I'm sorry. I really misunderstood your post (sometimes I read too fast :sad:)
EC coder