
#include <QApplication>
#include <QImage>
#include <QPixmap>
#include <QPainter>
#include <QDebug>
#include <QMessageBox>

#ifdef Q_OS_WIN

#include <windows.h>
#include <psapi.h>
#include <Tlhelp32.h>
#include <QStringBuilder>

#ifndef Q_OS_WINCE
#include <ErrorRep.h>
#include <dbghelp.h>
#endif


long ExorUnhandledExceptionFilter(struct _EXCEPTION_POINTERS* pointers)
{
    PEXCEPTION_RECORD exceptionRecord = pointers->ExceptionRecord;
    switch(exceptionRecord->ExceptionCode) {
        case EXCEPTION_IN_PAGE_ERROR:
        case EXCEPTION_ACCESS_VIOLATION:
        case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
        case EXCEPTION_BREAKPOINT:
        case EXCEPTION_DATATYPE_MISALIGNMENT:
        case EXCEPTION_FLT_DENORMAL_OPERAND:
        case EXCEPTION_FLT_DIVIDE_BY_ZERO:
        case EXCEPTION_FLT_INEXACT_RESULT:
        case EXCEPTION_FLT_INVALID_OPERATION:
        case EXCEPTION_FLT_OVERFLOW:
        case EXCEPTION_FLT_STACK_CHECK:
        case EXCEPTION_FLT_UNDERFLOW:
        case EXCEPTION_ILLEGAL_INSTRUCTION:
        case EXCEPTION_INT_DIVIDE_BY_ZERO:
        case EXCEPTION_INT_OVERFLOW:
        case EXCEPTION_INVALID_DISPOSITION:
        case EXCEPTION_NONCONTINUABLE_EXCEPTION:
        case EXCEPTION_PRIV_INSTRUCTION:
        case EXCEPTION_SINGLE_STEP:
        case EXCEPTION_STACK_OVERFLOW:
        QMessageBox::warning(NULL, "got exception", QString("Exception %1").arg(exceptionRecord->ExceptionCode));
            break;
        default:
            return EXCEPTION_CONTINUE_SEARCH; // unhandled exception
            break;
    }
    return EXCEPTION_CONTINUE_SEARCH;
}

long NTAPI NtExorUnhandledExceptionFilter(struct _EXCEPTION_POINTERS* pointers)
{
    return ExorUnhandledExceptionFilter(pointers);
}
#endif



int main(int argc, char *argv[])
{

#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
        AddVectoredExceptionHandler(0, NtExorUnhandledExceptionFilter);
#elif defined(Q_OS_WINCE)
        AddVectoredExceptionHandler(0, ExorUnhandledExceptionFilter);
#endif
    QApplication a(argc, argv);

    QImage* canvas = new QImage(QSize(1024,768), QImage::Format_RGB16);
    QImage* img = new QImage(QSize(1024,768), QImage::Format_RGB16);


    QMessageBox::warning(NULL, "Begin", "Starting test...");

    QPainter p(canvas);
    p.setClipRect(QRectF(QPointF(344,116),QSizeF(129,106)));
    p.drawImage(QRectF(QPointF(346,118.5),QSize(125,97)),*img,QRectF(0,0,1024,768));

    QMessageBox::warning(NULL, "End", "End of test reached!");
}
