How to catch CTRL+C?

Hi all,

I want to catch the CTRL+C command and then let one of my own methods handle it.
Since CTRL+C terminates or aborts the program I thought I'd use the signal() function from the signal.h library. Though I don't really get how to use the signal function.

I've tried some stuff but it doesn't work.

I thought it should be something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <signal.h>

using namespace std;

int main(int argc, char *argv[])
{
    signal(SIGABRT,myFunction);//If program aborts go to assigned function "myFunction".
    signal(SIGTERM,myFunction);//If program terminates go to assigned function "myFunction".
//Or should I use signal(SIGNINT,myFunction)
}
void myFunction()
{
    cout<<"Program was trying to abort or terminate."
}

Is this correct or am I doing something wrong?

Thanks in advance,
Rope.
Last edited on
closed account (z05DSL3A)
There was a recent topic about CTRL-C here:
http://www.cplusplus.com/forum/windows/494/
don't know if it would help.
I know this doesnt answer your question, but it is good coding in C++ to switch the stdio.h header(which is c) with the equivalent c++ header.
@ Grey Wolf. No sorry no help there.
@ Yogurt and what would that equivelant be?

Nobody knows the answer?
#include <iostream>

if you're going to use cout, but keep in mind that signal handlers can do very little.
cout'ing in a signal handler is a good way to deadlock your program.

closed account (z05DSL3A)
You could try the following and see what it does on your system:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <signal.h>

using namespace std;

bool forever = true;

void sighandler(int sig)
{
    cout<< "Signal " << sig << " caught..." << endl;

	forever = false;
}

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

	while(forever)
	{
	}
}


Out of interest, what OS are you targeting?

If you are only targeting Windows have a look at this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#define WIN32_LEAN_AND_MEAN   
#include <windows.h>
#include <tchar.h>
#include <iostream>
#include <signal.h>


BOOL WINAPI ConsoleHandler(
    DWORD dwCtrlType   //  control signal type
);

int main(int argc, char *argv[])
{
    if (SetConsoleCtrlHandler( (PHANDLER_ROUTINE)ConsoleHandler,TRUE)==FALSE)
    {
        // unable to install handler... 
        // display message to the user
        printf("Unable to install handler!\n");
        return -1;
    }


    while(true)
    {

    }
}

BOOL WINAPI ConsoleHandler(DWORD CEvent)
{
    char mesg[128];

    switch(CEvent)
    {
    case CTRL_C_EVENT:
        MessageBox(NULL,
            _T("CTRL+C received!"),_T("CEvent"),MB_OK);
        break;
    case CTRL_BREAK_EVENT:
        MessageBox(NULL,
            _T("CTRL+BREAK received!"),_T("CEvent"),MB_OK);
        break;
    case CTRL_CLOSE_EVENT:
        MessageBox(NULL,
            _T("Program being closed!"),_T("CEvent"),MB_OK);
        break;
    case CTRL_LOGOFF_EVENT:
        MessageBox(NULL,
            _T("User is logging off!"),_T("CEvent"),MB_OK);
        break;
    case CTRL_SHUTDOWN_EVENT:
        MessageBox(NULL,
            _T("User is logging off!"),_T("CEvent"),MB_OK);
        break;

    }
    return TRUE;
}
Last edited on
@ Grey Wolf. I'm targeting unix. But testing on windows at the moment (off course trying to avoid the mess that some c++ made on windows will not work on unix).

This is what I'm trying to do:

I have a school assignment for which I have to create a lexical analyzer. Though there are some specifications I have to follow. Such as the system is only allowed to start scanning the input-text when CTRL+C is pressed.

This is the part of the code where it goes wrong:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include "ConsoleReader.h"

bool inputEnd = false;
char c;

ConsoleReader::ConsoleReader()
{
}
void setInput(int sign)
{
     inputEnd = true;
}
/*Reads the input given per character, until the character is CTRL+C. Then returns the given input as a string.*/
string ConsoleReader::ReadConsole()
{
    string input="";
    signal( SIGINT,&setInput);
    cin >> c;

	while(!inputEnd)
	{
    input += c;
    c = getchar();
    }
    return input;
}


It worked for like a minute then I started getting this error in the windows console:"This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information."

The support team being, well, me. And not being able to solve the problem I turn to the cplusplus community.

Anyone?
Topic archived. No new replies allowed.