
/*******************************************************\
{                                                       }
{       Microsoft Visual C++ Version 4.X                }
{       Alarm Clock 1.5                                 }
{                                                       }
{       Copyright (C) 1996, CAT Computing Center (R).   }
{                                                       }
\*******************************************************/

#include <Windows.h>
#include <tchar.h>
#include "Resource.h"
#include <eh.h>
#include <new.h>
#include "Alarm.h"

void ExitAlarm();

LONG WINAPI UnExFilter(EXCEPTION_POINTERS *pExceptionInfo)
{
	ReportBox(IDS_UNEXCEPTION, MB_OK|MB_TASKMODAL|MB_ICONSTOP);
	ExitAlarm();
	return EXCEPTION_CONTINUE_SEARCH;
}

void _thread_se_translator(unsigned int nCode, EXCEPTION_POINTERS* pException)
{
	TRACE("SEH -> Exception code : %X", pException->ExceptionRecord->ExceptionCode);
	throw(pException);
}

int _handle_out_of_memory(size_t nSize)
{
	TRACE("NEW -> Out of memory for %d bytes\n", nSize);
	if(IDCANCEL == ReportBox(IDS_OUT_OF_MEM, MB_RETRYCANCEL|MB_TASKMODAL|MB_ICONSTOP))
	{
		ExitAlarm();
		return 0;
	}
	return 1;
}

void SetGlobalHandlers()
{
	SetUnhandledExceptionFilter(UnExFilter);	// For Win32 SEH
	_set_se_translator(_thread_se_translator);	// For C++ try/catch
	_set_new_handler(_handle_out_of_memory);
	_set_new_mode(1);
}
