From b15c499227f807a2eb4a7a7ede5a8ac33c902b7e Mon Sep 17 00:00:00 2001 From: matlabbe Date: Thu, 22 May 2014 15:35:34 +0000 Subject: [PATCH] Handling Ctrl-C on Windows git-svn-id: http://find-object.googlecode.com/svn/trunk/find_object@282 620bd6b2-0a58-f614-fd9a-1bd335dccda9 --- app/main.cpp | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/app/main.cpp b/app/main.cpp index 8605e5d3..341b0d6b 100644 --- a/app/main.cpp +++ b/app/main.cpp @@ -4,12 +4,25 @@ #include "MainWindow.h" #include "Settings.h" +#ifdef WIN32 +#include +BOOL WINAPI my_handler(DWORD signal) +{ + if (signal == CTRL_C_EVENT) + { + printf("\nCtrl-C caught! Quitting application...\n"); + QApplication::quit(); + } + return TRUE; +} +#else #include - -void my_handler(int s){ +void my_handler(int s) +{ printf("\nCtrl-C caught! Quitting application...\n"); QApplication::quit(); } +#endif void showUsage() { @@ -135,11 +148,19 @@ int main(int argc, char* argv[]) if(!guiMode) { // Catch ctrl-c to close the gui +#ifdef WIN32 + if (!SetConsoleCtrlHandler(my_handler, TRUE)) + { + printf("\nERROR: Could not set control handler"); + return 1; + } +#else struct sigaction sigIntHandler; sigIntHandler.sa_handler = my_handler; sigemptyset(&sigIntHandler.sa_mask); sigIntHandler.sa_flags = 0; sigaction(SIGINT, &sigIntHandler, NULL); +#endif } return app.exec();