[Win32utils-devel] Ctrl handler makes Ruby crash

Daniel Berger djberg96 at gmail.com
Sun Jan 20 09:05:37 EST 2008


Hi all,

Grab the latest windows-pr from CVS and try this program. It 
works...once. Then the interpreter crashes. Why? And what can we do 
about it?

The code is based on this: 
http://msdn2.microsoft.com/en-us/library/ms685049(VS.85).aspx

require 'windows/console'
require 'windows/sound'
include Windows::Console
include Windows::Sound

CtrlHandler = Win32::API::Callback.new('L', 'I'){ |ctrl_type|
    case ctrl_type
       when CTRL_C_EVENT
          puts "Ctrl-C event"
          Beep(750, 300)
          return true
       when CTRL_CLOSE_EVENT
          Beep(600, 200)
          puts "Ctrl-Close event"
          return true
       when CTRL_BREAK_EVENT
          Beep(900, 200)
          puts "Ctrl-Break event"
          return false
       when CTRL_LOGOFF_EVENT
          Beep(1000, 200)
          puts "Ctrl-Logoff event"
          return false
       when CTRL_SHUTDOWN_EVENT
          Beep(750, 500)
          puts("Ctrl-Shutdown event")
          return false
       else
          return false
    end
}

if SetConsoleCtrlHandler(CtrlHandler, TRUE)
    printf( "\nThe Control Handler is installed.\n" )
    printf( "\n -- Now try pressing Ctrl+C or Ctrl+Break, or" )
    printf( "\n    try logging off or closing the console...\n" )
    printf( "\n(...waiting in a loop for events...)\n\n" )

    while true
    end
else
    printf( "\nERROR: Could not set control handler")
end

Thanks,

Dan


More information about the win32utils-devel mailing list