user2699298
user2699298

Reputation: 1476

Is there such event as "Closing console" in C++?

Is there any event like in C# "FormClosing" but in C++ as Console closing where I can execute some code before the Console close? (In my case, I'd like to create a directory with the input of the user before the console is closed completely).

Upvotes: 8

Views: 1707

Answers (3)

deW1
deW1

Reputation: 5660

My guess is that you want to get the Event when clicking the [X]

BOOL WINAPI HandlerRoutine( DWORD eventCode )
{
  switch( eventCode )
  {
      case CTRL_CLOSE_EVENT:
      // do your thing
      return FALSE;
      break;
   }

  return TRUE;
}

Is that what you're looking for?

You also need to enable the Handler:

int main()
{
    SetConsoleCtrlHandler( HandlerRoutine , TRUE );
    getch();
}

More Info

Upvotes: 14

EkriirkE
EkriirkE

Reputation: 2304

A closing console effectively kills your application. In windows I'm unsure of the ability to trap this, but in linux you could trap the KILL signal?

Upvotes: 0

vallentin
vallentin

Reputation: 26197

If you want to close the console, then you can use FreeConsole();

If you then further want to open the console again, you can use AllocConsole();

Upvotes: 0

Related Questions