run exit code?

i was wondering if and how i can run code when the user clicks the X of a console application..or any application at that

thanks
Well that sounds legit.
closed account (z05DSL3A)
The following may be useful:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

void onExit( void )
{
    // Run cleanup code here!
    
}

int main()
{
    // Register the function that will be called
    // when the program exits
    atexit(onExit);
    
    // Do stuff

    return 0;
}


Or you could develop an application class, and put cleanup code in its destructor.
Last edited on
Grey Wolf, when declaring a function with no parameters, do not put void, simply put nothing in between the parentheses. It's a common thing that some people do, but is bad coding (like void main() instead of int main())
Grey Wolf's example shows a function that does not require any parameters. I don't think there is really anything wrong with that?
int main(void) is nothing like void main(). The former is standard (although redundant). The latter is not.
Last edited on
closed account (z05DSL3A)
Matt,

Thank you for your input but there is nothing wrong with using void as a parameter declaration clause. For your reference see section 8.3.5 paragraph 2 of the standard "...The parameter list (void) is equivalent to the empty parameter list...".

The reason I put void as the parameter declaration clause, was to highlight the fact that the function that is passed into the atexit() function must not take any parameters (I forgot to put that in the comments).

Last edited on
I heard that this was a difference in C/C++ standards. I'm not all that familiar with C, but a friend of mine on another forum said that an empty parameter list was valid in both C/C++, but meant different things. On C++ it meant the function takes no parameters ( like func(void) ), but on C it meant the function could take any number of parameters ( like func(...) ).

He quoted "section 3.5.4.3 of the C89 standard" -- which I didn't bother to look up. Personally I took it with a grain of salt -- seems kind of silly to me.

In that case I can see someone wanting to stick to the func(void) mentality for clarity or if they're just used to C.
Last edited on
closed account (z05DSL3A)
I can't comment on the C89 Standard, but for C99 says

If the list terminates with an ellipsis (, ...), no information about the number or types of the parameters after the comma is supplied.

and
The special case of an unnamed parameter of type void as the only item in the list specifies that the function has no parameters.

and
An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied.


So
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    int func1();      /* Declares func1() but gives no info on parameters */
    int func2(void);  /* Declares a function that takes no parameters */
    int func3()       /* Declares and defines a function that takes no parameters */
    { /**/ }

    /*
    **
    **
    */

    int func1(int x) 
    {/*...*/}

    int func2(void)
    {/*...*/}
NB: Head compiler used, code might not be entirely accurate. ;0)
Last edited on
All of which is applicable only to C, since C++ requires full prototyping. Therefore in C++ int foo() declares a function that takes no parameters.
closed account (z05DSL3A)
How do you spell the sound that is made when your head hits the keyboard?

bndfbnjhbnjhbnjhbnjhbnjh56y

never mind... ;0)
The atexit registered functions are called on clean shutdown. The same goes for calling the destructors of global classes.

Pressing X on a Windows CMD.EXE console calls KillProcess. The program may not know it's being killed.
No, it definitely doesn't kill the process. I've seen ping terminating cleanly when closing the console. My guess is cmd sends the TERM signal (or its equivalent) to whatever is running on it.
closed account (z05DSL3A)
Thinking about it, kbw and helios are probably both correct. Windows would most likely sent a signal to the app to close and if it doesn't respond Kill it (or at least pop up a dialog asking if it can kill it).

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
#include <windows.h> 
#include <stdio.h> 
 
BOOL CtrlHandler( DWORD fdwCtrlType ) 
{ 
  switch( fdwCtrlType ) 
  { 
    // Handle the CTRL-C signal. 
    case CTRL_C_EVENT: 
      printf( "Ctrl-C event\n\n" );
      Beep( 750, 300 ); 
      return( TRUE );
 
    // CTRL-CLOSE: confirm that the user wants to exit. 
    case CTRL_CLOSE_EVENT: 
      Beep( 600, 200 ); 
      printf( "Ctrl-Close event\n\n" );
      return( TRUE ); 
 
    // Pass other signals to the next handler. 
    case CTRL_BREAK_EVENT: 
      Beep( 900, 200 ); 
      printf( "Ctrl-Break event\n\n" );
      return FALSE; 
 
    case CTRL_LOGOFF_EVENT: 
      Beep( 1000, 200 ); 
      printf( "Ctrl-Logoff event\n\n" );
      return FALSE; 
 
    case CTRL_SHUTDOWN_EVENT: 
      Beep( 750, 500 ); 
      printf( "Ctrl-Shutdown event\n\n" );
      return FALSE; 
 
    default: 
      return FALSE; 
  } 
} 
 
void main( void ) 
{ 
  if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) 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( 1 ){ } 
  } 
  else 
    printf( "\nERROR: Could not set control handler"); 
}
NB: C code for windows only
Last edited on
genius! thanks a lot guys!
Topic archived. No new replies allowed.