3 "John Buckman" <john@lyris.com>
5 I have not looked to see what you're using the signal handler for, but
6 just FYI, in our own Unix/Windows command line applications, we have a
7 Windows handler for ctrl-c and ctrl-break, which we use as substitutes
8 for signal handling on Windows. I can give you source code for doing
9 this if you like, if you think it is a useful substitute.
11 There are two functions you need. A handler routine and a routine
12 which registers that handler. The SetConsoleCtrlHandler() Windows
13 function set the handler, and then they handler just receives a signal
14 and returns either a true or false to ban on whether it was handled or
17 Note that is only works in a console mode application and not in a
18 graphical application. The code below is copied directly out of
19 production source code working for several years, so there should not
23 bool PlatformSpecificInitialize() {
25 LYRIS_PROFILE("PlatformSpecificInitialize");
28 retval = SetConsoleCtrlHandler(handler_routine, TRUE);
30 trace("Note: SetConsoleCtrlHandler() did not succeed.");
33 retval = SetConsoleTitle(APPLICATION_NAME.c_str());
35 trace("Note: setConsoleTitle() did not succeed.");
41 BOOL WINAPI handler_routine(DWORD signal) {
43 LYRIS_PROFILE("handler_routine");
45 static unsigned char handles_to_use = 3;
46 static bool handled_already = false;
47 if ((signal == CTRL_CLOSE_EVENT) || (signal == CTRL_SHUTDOWN_EVENT)) {
48 // if we receive a Windows signal to shutdown, we should exit
49 immediately, and cleanly
50 if (handled_already == false) {
51 handled_already = true;
52 //lyris_Thread::ExitApplicationNow();
53 DisplayMessage("Shutting down as requested");
54 // create shutdown thread so that signal handler can return
56 lyris_Thread aShutDown(ShutDownNow, NULL, "Shut Down
65 else if (signal == CTRL_C_EVENT) {
66 // if we receive a Windows signal to shutdown, we should exit
67 immediately, and cleanly
68 if (handles_to_use == 3) {
70 //lyris_Thread::ExitApplicationNow();
71 DisplayMessage("Shutting down as requested");
72 // create shutdown thread so that signal handler can return
74 lyris_Thread aShutDown(ShutDownNow, NULL, "Shut Down
79 else if (handles_to_use > 0) {
80 DisplayMessage("Currently shutting down: press Ctrl-C " +
81 ULong2String(handles_to_use) + " more times to shut down immediately.");
89 else if (signal == CTRL_BREAK_EVENT) {
90 if (APPLICATION_NAME == "Lyris") {
91 if (ShouldDisplayDebugMessages() == 0) {
92 SetShouldDisplayDebugMessages(1);
95 SetShouldDisplayDebugMessages(0);
97 DisplayMessage("Debug mode is now: " +
98 Bool2String(ShouldDisplayDebugMessages()));
100 else if (APPLICATION_NAME == "MailShield") {
101 specific::setReloadConfig(lyris_yes);
109 lyr_notify("Unknown Windows signal passed to handler: " +
110 ULong2String(signal));