#include <QLibraryInfo>
#include <QMap>
#include <QMessageBox>
-#include <QMutex>
#include <QPlastiqueStyle>
#include <QProcess>
#include <QReadWriteLock>
typedef HRESULT (WINAPI *SHGetFolderPath_t)(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath);
///////////////////////////////////////////////////////////////////////////////
+// CRITICAL SECTION
+///////////////////////////////////////////////////////////////////////////////
+
+/*
+ * wrapper for native Win32 critical sections
+ */
+class CriticalSection
+{
+public:
+ inline CriticalSection(void)
+ {
+ InitializeCriticalSection(&m_win32criticalSection);
+ }
+
+ inline ~CriticalSection(void)
+ {
+ DeleteCriticalSection(&m_win32criticalSection);
+ }
+
+ inline void enter(void)
+ {
+ EnterCriticalSection(&m_win32criticalSection);
+ }
+
+ inline bool tryEnter(void)
+ {
+ return TryEnterCriticalSection(&m_win32criticalSection);
+ }
+
+ inline void leave(void)
+ {
+ LeaveCriticalSection(&m_win32criticalSection);
+ }
+
+protected:
+ CRITICAL_SECTION m_win32criticalSection;
+};
+
+/*
+ * RAII-style critical section locker
+ */
+class CSLocker
+{
+public:
+ inline CSLocker(CriticalSection &criticalSection)
+ :
+ m_locked(false),
+ m_criticalSection(criticalSection)
+ {
+ m_criticalSection.enter();
+ m_locked = true;
+ }
+
+ inline ~CSLocker(void)
+ {
+ forceUnlock();
+ }
+
+ inline void forceUnlock(void)
+ {
+ if(m_locked)
+ {
+ m_criticalSection.leave();
+ m_locked = false;
+ }
+ }
+protected:
+ volatile bool m_locked;
+ CriticalSection &m_criticalSection;
+};
+
+///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARS
///////////////////////////////////////////////////////////////////////////////
//Console attached flag
static bool g_lamexp_console_attached = false;
+//Fatal exit flags
+static volatile bool g_lamexp_fatal_flag = true;
+static CriticalSection g_lamexp_fatal_lock;
+
+//Global locks
+static CriticalSection g_lamexp_message_lock;
+
//Special folders
static struct
{
//Image formats
static const char *g_lamexp_imageformats[] = {"bmp", "png", "jpg", "gif", "ico", "xpm", NULL}; //"svg"
-//Global locks
-static QMutex g_lamexp_message_mutex;
-
//Main thread ID
static const DWORD g_main_thread_id = GetCurrentThreadId();
return;
}
- QMutexLocker lock(&g_lamexp_message_mutex);
+ CSLocker lock(g_lamexp_message_lock);
if(g_lamexp_log_file)
{
if((type == QtCriticalMsg) || (type == QtFatalMsg))
{
- lock.unlock();
- lamexp_fatal_exit(L"The application has encountered a critical error and will exit now!", QWCHAR(QString::fromUtf8(msg)));
+ lock.forceUnlock();
+ lamexp_fatal_exit(msg);
}
}
*/
static void lamexp_invalid_param_handler(const wchar_t* exp, const wchar_t* fun, const wchar_t* fil, unsigned int, uintptr_t)
{
- lamexp_fatal_exit(L"Invalid parameter handler invoked, application will exit!");
+ lamexp_fatal_exit("Invalid parameter handler invoked, application will exit!");
}
/*
static void lamexp_signal_handler(int signal_num)
{
signal(signal_num, lamexp_signal_handler);
- lamexp_fatal_exit(L"Signal handler invoked, application will exit!");
+ lamexp_fatal_exit("Signal handler invoked, application will exit!");
}
/*
*/
static LONG WINAPI lamexp_exception_handler(struct _EXCEPTION_POINTERS *ExceptionInfo)
{
- lamexp_fatal_exit(L"Unhandeled exception handler invoked, application will exit!");
+ lamexp_fatal_exit("Unhandeled exception handler invoked, application will exit!");
return LONG_MAX;
}
{
if(lamexp_check_for_debugger())
{
- lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
+ lamexp_fatal_exit("Not a debug build. Please unload debugger and try again!");
return 666;
}
lamexp_sleep(100);
{
if(lamexp_check_for_debugger())
{
- lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
+ lamexp_fatal_exit("Not a debug build. Please unload debugger and try again!");
}
const uintptr_t h = _beginthreadex(NULL, 0, lamexp_debug_thread_proc, NULL, 0, NULL);
return (HANDLE)(h^0xdeadbeef);
{
if((!(LAMEXP_DEBUG)) && lamexp_check_for_debugger())
{
- lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
+ lamexp_fatal_exit("Not a debug build. Please unload debugger and try again!");
}
switch(reinterpret_cast<MSG*>(message)->message)
}
/*
+ * Fatal application exit - helper
+ */
+static DWORD WINAPI lamexp_fatal_exit_helper(LPVOID lpParameter)
+{
+ MessageBoxA(NULL, ((LPCSTR) lpParameter), "LameXP - Guru Meditation", MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_TOPMOST | MB_SETFOREGROUND);
+ return 0;
+}
+
+/*
* Fatal application exit
*/
-#pragma intrinsic(_InterlockedExchange)
-void lamexp_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage)
+void lamexp_fatal_exit(const char* const errorMessage)
{
- static volatile long bFatalFlag = 0L;
+ g_lamexp_fatal_lock.enter();
+
+ if(!g_lamexp_fatal_flag)
+ {
+ return; /*prevent recursive invocation*/
+ }
- if(_InterlockedExchange(&bFatalFlag, 1L) == 0L)
+ g_lamexp_fatal_flag = false;
+
+ if(g_main_thread_id != GetCurrentThreadId())
{
- if(GetCurrentThreadId() != g_main_thread_id)
- {
- HANDLE mainThread = OpenThread(THREAD_TERMINATE, FALSE, g_main_thread_id);
- if(mainThread) TerminateThread(mainThread, ULONG_MAX);
- }
-
- if(errorBoxMessage)
+ if(HANDLE hThreadMain = OpenThread(THREAD_SUSPEND_RESUME, FALSE, g_main_thread_id))
{
- MessageBoxW(NULL, errorBoxMessage, L"LameXP - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
+ SuspendThread(hThreadMain); /*stop main thread*/
}
+ }
- for(;;)
- {
- FatalAppExit(0, exitMessage);
- TerminateProcess(GetCurrentProcess(), -1);
- }
+ if(HANDLE hThread = CreateThread(NULL, 0, lamexp_fatal_exit_helper, (LPVOID) errorMessage, 0, NULL))
+ {
+ WaitForSingleObject(hThread, INFINITE);
}
- TerminateThread(GetCurrentThread(), -1);
- Sleep(INFINITE);
+ for(;;)
+ {
+ TerminateProcess(GetCurrentProcess(), 666);
+ }
}
/*
{
if((!LAMEXP_DEBUG) && lamexp_check_for_debugger())
{
- lamexp_fatal_exit(L"Not a debug build. Please unload debugger and try again!");
+ lamexp_fatal_exit("Not a debug build. Please unload debugger and try again!");
}
//Zero *before* constructors are called