본문 바로가기

C..C++....

콘솔 윈도우 강제종료시 이벤트 처리하기

#include <windows.h>

BOOL CtrlHandler(DWORD fdwCtrlType)
{
  switch(fdwCtrlType)
  {
  case CTRL_C_EVENT:
  case CTRL_CLOSE_EVENT:
  case CTRL_LOGOFF_EVENT:
  case CTRL_SHUTDOWN_EVENT:
  case CTRL_BREAK_EVENT:
  default:
    printf("종료");
  }
  return FALSE;
}

int _tmain(int argc, _TCHAR* argv[])
{
  BOOL fSuccess = SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler,TRUE);

  while(1)
  {

  }
  if(!fSuccess)
  {
    printf("종료2");
  }
  return 0;
}


자료출처 : http://perry.tistory.com/27