/*

Copyright 2009 Jesper Svennevid

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

#include "ExceptionHandler.h"

#include <stdio.h>

namespace dsbridge
{

ExceptionHandler::ExceptionHandler()
{
}

ExceptionHandler::~ExceptionHandler()
{
}

LONG WINAPI ExceptionHandler::filter(const char* name, struct _EXCEPTION_POINTERS* info)
{
	// uncomment to catch exception in debugger
	//return EXCEPTION_CONTINUE_SEARCH;

	char buf[256];

	ExceptionHandler handler;
	if (handler.createDump(GetCurrentThreadId(), name, info))
	{
		sprintf_s(buf, sizeof(buf), "Exception in DSBridge (%s)\n\nDetails have been written to dsound.mdmp,\nlook in installation directory.", name);
	}
	else
	{
		sprintf_s(buf, sizeof(buf), "Exception in DSBridge (%s)\n\nCould not generate minidump, exception information is not available.", name);
	}

	MessageBox(0, buf, "DSBridge Exception Handler", MB_ICONEXCLAMATION|MB_OK);

	return EXCEPTION_EXECUTE_HANDLER;
}

bool ExceptionHandler::createDump(DWORD thread, const char* name, struct _EXCEPTION_POINTERS* info)
{
	bool success = false;
	HANDLE dumpFile = INVALID_HANDLE_VALUE;

	do
	{
		if (!initialize())
		{
			break;
		}

		dumpFile = CreateFile("dsound.mdmp", GENERIC_WRITE, 0, 0, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
		if (dumpFile == INVALID_HANDLE_VALUE)
		{
			break;
		}

		MINIDUMP_EXCEPTION_INFORMATION miniInfo;
		miniInfo.ThreadId = thread;
		miniInfo.ExceptionPointers = info;
		miniInfo.ClientPointers = TRUE;

		if (!MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), dumpFile, MiniDumpNormal, &miniInfo, 0, 0))
		{
			break;
		}

		success = true;
	}
	while (0);

	if (dumpFile != INVALID_HANDLE_VALUE)
	{
		CloseHandle(dumpFile);
	}

	return success;
}

bool ExceptionHandler::initialize()
{
	m_module = LoadLibrary("dbghelp.dll");
	if (!m_module)
	{
		return false;
	}

	MiniDumpWriteDump = reinterpret_cast<MINIDUMPWRITEDUMP>(GetProcAddress(m_module, "MiniDumpWriteDump"));

	return !!MiniDumpWriteDump;
}

}
