Results 1 to 2 of 2

Thread: Intercept Ctrl-c from the command line

  1. #1
    Join Date
    Feb 2006
    Posts
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Intercept Ctrl-c from the command line

    Hi

    I have a Qt GUI application which I can run from the Microsoft command line window. One of the options on the command line is for the application is to do some work (a batch job) and then quit. During the time the application is running I can stop the application by pressing ctrl-c in the command line window. The application will stop but not gracefully, it dies somewhere in Qt (qpaint something). How can I intercept Ctrl-c or hook into Qt/application shutting down so I can clean up properly?

    I've looked at QSessionManager. I've tried using

    Qt Code:
    1. if( QSysInfo::WindowsVersion >= QSysInfo::WV_XP )
    2. {
    3. // type of the pointer function to retrieve from the library
    4. typedef BOOL (WINAPI* PFN_AttachConsole)(DWORD);
    5.  
    6. // get the module
    7. HMODULE hModule= ::GetModuleHandleA( "kernel32.dll" );
    8. // get the pointer to the function
    9. PFN_AttachConsole fpAttachConsole = (BOOL (_stdcall *)(DWORD)) GetProcAddress( hModule, "AttachConsole" );
    10.  
    11. // check if the pointer is valid and retrieve the parent console
    12. if( (NULL != fpAttachConsole) && (0 != fpAttachConsole( (DWORD)-1 )) )
    13. {
    14. // Process escape characters
    15. SetConsoleMode( hModule, ENABLE_PROCESSED_OUTPUT | ENABLE_PROCESSED_INPUT );
    16. m_hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
    17. m_bSuccess = true;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

    I think a problem with the above it is not attached to a 'true' console and I so can't switch off ctrl-c (unless the above code is wrong?)

    I have also tried windows functions

    Qt Code:
    1. SetConsoleCtrlHandler( (PHANDLER_ROUTINE) gFnConsoleCtrlHandler, TRUE )
    2.  
    3. Using my function
    4. BOOL gFnConsoleCtrlHandler( DWORD fdwCtrlType )
    5. {
    6. switch( fdwCtrlType )
    7. {
    8. // Handle the CTRL-C signal.
    9. case CTRL_C_EVENT:
    10. printf( "Ctrl-C event\n\n" );
    11. //gFnApp_TidyUpOnExit(); Idea was to call something like this
    12. return( TRUE );
    13.  
    14. // CTRL-CLOSE: confirm that the user wants to exit.
    15. case CTRL_CLOSE_EVENT:
    16. printf( "Ctrl-Close event\n\n" );
    17. return( TRUE );
    18.  
    19. // Pass other signals to the next handler.
    20. case CTRL_BREAK_EVENT:
    21. printf( "Ctrl-Break event\n\n" );
    22. return FALSE;
    23.  
    24. case CTRL_LOGOFF_EVENT:
    25. printf( "Ctrl-Logoff event\n\n" );
    26. return FALSE;
    27.  
    28. case CTRL_SHUTDOWN_EVENT:
    29. printf( "Ctrl-Shutdown event\n\n" );
    30. return FALSE;
    31.  
    32. default:
    33. return FALSE;
    34. }
    35. }
    To copy to clipboard, switch view to plain text mode 

    but again this gets ignored

    Ideally I would like to intercept the ctrl-c key, visit my tidy up function then let the app quit gracefull and keep Qt happy.

    Any suggestions welcome

  2. #2
    Join Date
    Jul 2007
    Location
    New York
    Posts
    45
    Thanks
    5
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Intercept Ctrl-c from the command line

    in linux you use

    #include <signal.h>

    sighandler(int)
    {
    //do something here.
    }

    int main(int argc, char *argv[])
    {
    signal(SIGINT,sighandler);
    }


    there should be a windows equivalent

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.