File: exchndl_win.cpp

package info (click to toggle)
warzone2100 4.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 660,348 kB
  • sloc: cpp: 675,711; ansic: 387,204; javascript: 75,107; python: 16,628; php: 4,294; sh: 3,941; makefile: 2,330; lisp: 1,492; cs: 489; xml: 404; perl: 224; ruby: 156; java: 89
file content (723 lines) | stat: -rw-r--r-- 23,255 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
/*
	This file is part of Warzone 2100.
	Portions derived from: exchndl_mingw.cpp
	Copyright (C) 2008-2020  Warzone 2100 Project

	Warzone 2100 is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	Warzone 2100 is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Warzone 2100; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#if (_WIN32_WINNT < 0x0501)			// must force Windows XP or higher
#undef _WIN32_WINNT
#define _WIN32_WINNT 0x0501
#endif
#if !defined(NOCRYPT)
#define NOCRYPT
#endif
#include "lib/framework/frame.h"
#include "dumpinfo.h"
#include "exchndl.h"
#include "3rdparty/StackWalker.h"

#include <assert.h>
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <shlobj.h>
#include <shlwapi.h>

#include <vector>

// Declare the static variables
static wchar_t szLogFileName[MAX_PATH] = L"";
static wchar_t szMinidumpFileName[MAX_PATH] = L"";
static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter = NULL;
static char *formattedVersionString = NULL;

// Provide this wrapper explicitly, as mingw headers do not provide the appropriate wcsncat_s wrapper
template <size_t size> inline
errno_t wcsncat_s_wrapper(wchar_t(&strDest)[size], const wchar_t* strSource, size_t count)
{
	return wcsncat_s(strDest, size, strSource, count);
}

static void MyStrCpy(char* szDest, size_t nMaxDestSize, const char* szSrc)
{
	if (nMaxDestSize <= 0) return;
	strncpy_s(szDest, nMaxDestSize, szSrc, _TRUNCATE);
}  // MyStrCpy

class WzFormattedStackWalker : public StackWalker
{
public:
	WzFormattedStackWalker()
		: StackWalker()
	{}
protected:

	virtual void OnDbgHelpErr(LPCSTR szFuncName, DWORD gle, DWORD64 addr)
	{
		if ((strcmp(szFuncName, "SymGetLineFromAddr64") == 0) && (gle == 487))
		{
			// ignore SymGetLineFromAddr64, error 487
			return;
		}
		StackWalker::OnDbgHelpErr(szFuncName, gle, addr);
	}

	virtual void BeforeLoadModules()
	{
		OnOutput("\r\nLoaded Modules:\r\n");
	}

	virtual void BeforeShowCallstack()
	{
		OnOutput("\r\nCall Stack:\r\n");
	}

	virtual void OnLoadModule(LPCSTR img, LPCSTR mod, DWORD64 baseAddr, DWORD size, DWORD result, LPCSTR symType, LPCSTR pdbName, ULONGLONG fileVersion)
	{
		CHAR buffer[STACKWALK_MAX_NAMELEN];
		size_t maxLen = STACKWALK_MAX_NAMELEN;
#if _MSC_VER >= 1400
		maxLen = _TRUNCATE;
#endif
		if (fileVersion == 0)
			_snprintf_s(buffer, maxLen, "%s:%s (%p), size: %d (result: %d), SymType: '%s', PDB: '%s'\r\n", img, mod, (LPVOID)baseAddr, size, result, symType, pdbName);
		else
		{
			DWORD v4 = (DWORD)(fileVersion & 0xFFFF);
			DWORD v3 = (DWORD)((fileVersion >> 16) & 0xFFFF);
			DWORD v2 = (DWORD)((fileVersion >> 32) & 0xFFFF);
			DWORD v1 = (DWORD)((fileVersion >> 48) & 0xFFFF);
			_snprintf_s(buffer, maxLen, "%s:%s (%p), size: %d (result: %d), SymType: '%s', PDB: '%s', fileVersion: %d.%d.%d.%d\r\n", img, mod, (LPVOID)baseAddr, size, result, symType, pdbName, v1, v2, v3, v4);
		}
		buffer[STACKWALK_MAX_NAMELEN - 1] = 0;  // be sure it is NUL terminated
		OnOutput(buffer);
	}

	virtual void OnCallstackEntry(CallstackEntryType eType, CallstackEntry &entry)
	{
		CHAR buffer[STACKWALK_MAX_NAMELEN];
		size_t maxLen = STACKWALK_MAX_NAMELEN;
#if _MSC_VER >= 1400
		maxLen = _TRUNCATE;
#endif
		if ((eType != lastEntry) && (entry.offset != 0))
		{
			if (entry.name[0] == 0)
				MyStrCpy(entry.name, STACKWALK_MAX_NAMELEN, "(function-name not available)");
			if (entry.undName[0] != 0)
				MyStrCpy(entry.name, STACKWALK_MAX_NAMELEN, entry.undName);
			if (entry.undFullName[0] != 0)
				MyStrCpy(entry.name, STACKWALK_MAX_NAMELEN, entry.undFullName);
			if (entry.lineFileName[0] == 0)
			{
				MyStrCpy(entry.lineFileName, STACKWALK_MAX_NAMELEN, "(filename not available)");
				if (entry.moduleName[0] == 0)
					MyStrCpy(entry.moduleName, STACKWALK_MAX_NAMELEN, "(module-name not available)");
				_snprintf_s(buffer, maxLen, "%p  %s:%p  %s\r\n", (LPVOID)entry.offset, entry.loadedImageName, (LPVOID)entry.offset, entry.name);
			}
			else
				_snprintf_s(buffer, maxLen, "%p  %s  %s  %s:%d\r\n", (LPVOID)entry.offset, entry.loadedImageName, entry.name, entry.lineFileName, entry.lineNumber);
			buffer[STACKWALK_MAX_NAMELEN - 1] = 0;
			OnOutput(buffer);
		}
	}
};

class WzExceptionStackWalker : public WzFormattedStackWalker
{
public:
	WzExceptionStackWalker(HANDLE outputFile)
		: WzFormattedStackWalker()
		, hOutputFile(outputFile)
	{}
protected:
	virtual void OnOutput(LPCSTR szText)
	{
		DWORD cbWritten = 0;
		DWORD len = static_cast<DWORD>(std::min<size_t>(strlen(szText), std::numeric_limits<DWORD>::max()));
		WriteFile(hOutputFile, szText, len * sizeof(char), &cbWritten, 0);
	}

private:
	HANDLE hOutputFile = INVALID_HANDLE_VALUE;
};

static
int __cdecl rprintf(HANDLE hReportFile, const TCHAR *format, ...)
{
	TCHAR szBuff[4096];
	int retValue;
	DWORD cbWritten;
	va_list argptr;

	va_start(argptr, format);
	retValue = wvsprintf(szBuff, format, argptr);
	va_end(argptr);

	WriteFile(hReportFile, szBuff, retValue * sizeof(TCHAR), &cbWritten, 0);

	return retValue;
}

// The GetModuleBase function retrieves the base address of the module that contains the specified address.
static
PVOID GetModuleBase(LPCVOID dwAddress)
{
	MEMORY_BASIC_INFORMATION Buffer;

	return VirtualQuery(dwAddress, &Buffer, sizeof(Buffer)) ? Buffer.AllocationBase : 0;
}

// Generate a MiniDump

# include "dbghelp.h"
static bool OutputMiniDump(LPCWSTR miniDumpPath, PEXCEPTION_POINTERS pExceptionInfo)
{
	bool bSuccess = false;
	HANDLE miniDumpFile = CreateFileW(miniDumpPath, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

	if (miniDumpFile != INVALID_HANDLE_VALUE)
	{
		size_t package_version_len = strlen(PACKAGE_VERSION);
		ULONG package_version_len_ulong = (package_version_len <= ULONG_MAX) ? (ULONG)package_version_len : 0;
		MINIDUMP_USER_STREAM uStream = { LastReservedStream + 1, package_version_len_ulong, (PVOID)PACKAGE_VERSION };
		MINIDUMP_USER_STREAM_INFORMATION uInfo = { 1, &uStream };
		MINIDUMP_EXCEPTION_INFORMATION eInfo = { GetCurrentThreadId(), pExceptionInfo, false };

		if (MiniDumpWriteDump(
			GetCurrentProcess(),
			GetCurrentProcessId(),
			miniDumpFile,
			MiniDumpNormal,
			pExceptionInfo ? &eInfo : NULL,
			&uInfo,
			NULL))
		{
			// success
			bSuccess = true;
		}
		else
		{
			bSuccess = false;
		}

		CloseHandle(miniDumpFile);
	}
	else
	{
		bSuccess = false;
	}

	return bSuccess;
}

static
void GenerateExceptionReport(HANDLE hReportFile, PEXCEPTION_POINTERS pExceptionInfo)
{
	PEXCEPTION_RECORD pExceptionRecord = pExceptionInfo->ExceptionRecord;
	TCHAR szModule[MAX_PATH];
	HMODULE hModule;
	PCONTEXT pContext;

	// Start out with a banner
	rprintf(hReportFile, _T("-------------------\r\n\r\n"));

	{
		const TCHAR *lpDayOfWeek[] =
		{
			_T("Sunday"),
			_T("Monday"),
			_T("Tuesday"),
			_T("Wednesday"),
			_T("Thursday"),
			_T("Friday"),
			_T("Saturday")
		};
		const TCHAR *lpMonth[] =
		{
			NULL,
			_T("January"),
			_T("February"),
			_T("March"),
			_T("April"),
			_T("May"),
			_T("June"),
			_T("July"),
			_T("August"),
			_T("September"),
			_T("October"),
			_T("November"),
			_T("December")
		};
		SYSTEMTIME SystemTime;

		GetLocalTime(&SystemTime);
		rprintf(hReportFile, _T("Error occurred on %s, %s %i, %i at %02i:%02i:%02i.\r\n\r\n"),
		        lpDayOfWeek[SystemTime.wDayOfWeek],
		        lpMonth[SystemTime.wMonth],
		        SystemTime.wDay,
		        SystemTime.wYear,
		        SystemTime.wHour,
		        SystemTime.wMinute,
		        SystemTime.wSecond
		       );
	}

	// Dump a generic info header
	dbgDumpHeader(hReportFile);

	// First print information about the type of fault
	rprintf(hReportFile, _T("\r\n%s caused "),  GetModuleFileName(NULL, szModule, MAX_PATH) ? szModule : _T("Application"));
	switch (pExceptionRecord->ExceptionCode)
	{
	case EXCEPTION_ACCESS_VIOLATION:
		rprintf(hReportFile, _T("an Access Violation"));
		break;

	case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
		rprintf(hReportFile, _T("an Array Bound Exceeded"));
		break;

	case EXCEPTION_BREAKPOINT:
		rprintf(hReportFile, _T("a Breakpoint"));
		break;

	case EXCEPTION_DATATYPE_MISALIGNMENT:
		rprintf(hReportFile, _T("a Datatype Misalignment"));
		break;

	case EXCEPTION_FLT_DENORMAL_OPERAND:
		rprintf(hReportFile, _T("a Float Denormal Operand"));
		break;

	case EXCEPTION_FLT_DIVIDE_BY_ZERO:
		rprintf(hReportFile, _T("a Float Divide By Zero"));
		break;

	case EXCEPTION_FLT_INEXACT_RESULT:
		rprintf(hReportFile, _T("a Float Inexact Result"));
		break;

	case EXCEPTION_FLT_INVALID_OPERATION:
		rprintf(hReportFile, _T("a Float Invalid Operation"));
		break;

	case EXCEPTION_FLT_OVERFLOW:
		rprintf(hReportFile, _T("a Float Overflow"));
		break;

	case EXCEPTION_FLT_STACK_CHECK:
		rprintf(hReportFile, _T("a Float Stack Check"));
		break;

	case EXCEPTION_FLT_UNDERFLOW:
		rprintf(hReportFile, _T("a Float Underflow"));
		break;

	case EXCEPTION_GUARD_PAGE:
		rprintf(hReportFile, _T("a Guard Page"));
		break;

	case EXCEPTION_ILLEGAL_INSTRUCTION:
		rprintf(hReportFile, _T("an Illegal Instruction"));
		break;

	case EXCEPTION_IN_PAGE_ERROR:
		rprintf(hReportFile, _T("an In Page Error"));
		break;

	case EXCEPTION_INT_DIVIDE_BY_ZERO:
		rprintf(hReportFile, _T("an Integer Divide By Zero"));
		break;

	case EXCEPTION_INT_OVERFLOW:
		rprintf(hReportFile, _T("an Integer Overflow"));
		break;

	case EXCEPTION_INVALID_DISPOSITION:
		rprintf(hReportFile, _T("an Invalid Disposition"));
		break;

	case EXCEPTION_INVALID_HANDLE:
		rprintf(hReportFile, _T("an Invalid Handle"));
		break;

	case EXCEPTION_NONCONTINUABLE_EXCEPTION:
		rprintf(hReportFile, _T("a Noncontinuable Exception"));
		break;

	case EXCEPTION_PRIV_INSTRUCTION:
		rprintf(hReportFile, _T("a Privileged Instruction"));
		break;

	case EXCEPTION_SINGLE_STEP:
		rprintf(hReportFile, _T("a Single Step"));
		break;

	case EXCEPTION_STACK_OVERFLOW:
		rprintf(hReportFile, _T("a Stack Overflow"));
		break;

	case DBG_CONTROL_C:
		rprintf(hReportFile, _T("a Control+C"));
		break;

	case DBG_CONTROL_BREAK:
		rprintf(hReportFile, _T("a Control+Break"));
		break;

	case DBG_TERMINATE_THREAD:
		rprintf(hReportFile, _T("a Terminate Thread"));
		break;

	case DBG_TERMINATE_PROCESS:
		rprintf(hReportFile, _T("a Terminate Process"));
		break;

	case RPC_S_UNKNOWN_IF:
		rprintf(hReportFile, _T("an Unknown Interface"));
		break;

	case RPC_S_SERVER_UNAVAILABLE:
		rprintf(hReportFile, _T("a Server Unavailable"));
		break;

	default:
		/*
		static TCHAR szBuffer[512] = { 0 };

		// If not one of the "known" exceptions, try to get the string
		// from NTDLL.DLL's message table.

		FormatMessage(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_HMODULE,
						GetModuleHandle(_T("NTDLL.DLL")),
						dwCode, 0, szBuffer, sizeof(szBuffer), 0);
		*/

		rprintf(hReportFile, _T("an Unknown [0x%lX] Exception"), pExceptionRecord->ExceptionCode);
		break;
	}

	// Now print information about where the fault occurred
	rprintf(hReportFile, _T(" at location 0x%" PRIXPTR ""), (uintptr_t)pExceptionRecord->ExceptionAddress);
	if ((hModule = (HMODULE) GetModuleBase(pExceptionRecord->ExceptionAddress)) && GetModuleFileName(hModule, szModule, sizeof(szModule)))
	{
		rprintf(hReportFile, _T(" in module %s"), szModule);
	}

	// If the exception was an access violation, print out some additional information, to the error log and the debugger.
	if (pExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION && pExceptionRecord->NumberParameters >= 2)
	{
		rprintf(hReportFile, _T(" %s location %08x"), pExceptionRecord->ExceptionInformation[0] ? "Writing to" : "Reading from", pExceptionRecord->ExceptionInformation[1]);
	}

	rprintf(hReportFile, _T(".\r\n\r\n"));

	dbgDumpLog(hReportFile);

	pContext = pExceptionInfo->ContextRecord;

#ifdef _M_IX86	// Intel Only!

	// Show the registers
	rprintf(hReportFile, _T("Registers:\r\n"));
	if (pContext->ContextFlags & CONTEXT_INTEGER)
		rprintf(
			hReportFile,
		    _T("eax=%08lx ebx=%08lx ecx=%08lx edx=%08lx esi=%08lx edi=%08lx\r\n"),
		    pContext->Eax,
		    pContext->Ebx,
		    pContext->Ecx,
		    pContext->Edx,
		    pContext->Esi,
		    pContext->Edi
		);
	if (pContext->ContextFlags & CONTEXT_CONTROL)
		rprintf(
			hReportFile,
		    _T("eip=%08lx esp=%08lx ebp=%08lx iopl=%1lx %s %s %s %s %s %s %s %s %s %s\r\n"),
		    pContext->Eip,
		    pContext->Esp,
		    pContext->Ebp,
		    (pContext->EFlags >> 12) & 3,	//  IOPL level value
		    pContext->EFlags & 0x00100000 ? "vip" : "   ",	//  VIP (virtual interrupt pending)
		    pContext->EFlags & 0x00080000 ? "vif" : "   ",	//  VIF (virtual interrupt flag)
		    pContext->EFlags & 0x00000800 ? "ov" : "nv",	//  VIF (virtual interrupt flag)
		    pContext->EFlags & 0x00000400 ? "dn" : "up",	//  OF (overflow flag)
		    pContext->EFlags & 0x00000200 ? "ei" : "di",	//  IF (interrupt enable flag)
		    pContext->EFlags & 0x00000080 ? "ng" : "pl",	//  SF (sign flag)
		    pContext->EFlags & 0x00000040 ? "zr" : "nz",	//  ZF (zero flag)
		    pContext->EFlags & 0x00000010 ? "ac" : "na",	//  AF (aux carry flag)
		    pContext->EFlags & 0x00000004 ? "po" : "pe",	//  PF (parity flag)
		    pContext->EFlags & 0x00000001 ? "cy" : "nc"	//  CF (carry flag)
		);
	if (pContext->ContextFlags & CONTEXT_SEGMENTS)
	{
		rprintf(
			hReportFile,
		    _T("cs=%04lx  ss=%04lx  ds=%04lx  es=%04lx  fs=%04lx  gs=%04lx"),
		    pContext->SegCs,
		    pContext->SegSs,
		    pContext->SegDs,
		    pContext->SegEs,
		    pContext->SegFs,
		    pContext->SegGs,
		    pContext->EFlags
		);
		if (pContext->ContextFlags & CONTEXT_CONTROL)
			rprintf(
				hReportFile,
			    _T("             efl=%08lx"),
			    pContext->EFlags
			);
	}
	else if (pContext->ContextFlags & CONTEXT_CONTROL)
		rprintf(
			hReportFile,
		    _T("                                                                       efl=%08lx"),
		    pContext->EFlags
		);
	rprintf(hReportFile, _T("\r\n\r\n"));

#endif

	// Print a StackTrace
	WzExceptionStackWalker stackWalker(hReportFile);
	stackWalker.ShowCallstack(GetCurrentThread(), pContext);

	rprintf(hReportFile, _T("\r\n\r\n"));
}

#include <stdio.h>
#include <fcntl.h>
#include <io.h>

// Entry point where control comes on an unhandled exception
static
LONG WINAPI TopLevelExceptionFilter(PEXCEPTION_POINTERS pExceptionInfo)
{
	static bool bBeenHere = FALSE;

	if (!bBeenHere)
	{
		UINT fuOldErrorMode;

		bBeenHere = TRUE;

		fuOldErrorMode = SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);

		HANDLE hReportFile = CreateFileW(
		                  szLogFileName,
		                  GENERIC_WRITE,
		                  0,
		                  0,
		                  OPEN_ALWAYS,
		                  FILE_FLAG_WRITE_THROUGH,
		                  0
		              );
		if (hReportFile == INVALID_HANDLE_VALUE)
		{
			// Retrieve the system error message for the last-error code

			LPVOID lpMsgBuf;
			DWORD dw = GetLastError();
			TCHAR szBuffer[4196];

			FormatMessage(
			    FORMAT_MESSAGE_ALLOCATE_BUFFER |
			    FORMAT_MESSAGE_FROM_SYSTEM |
			    FORMAT_MESSAGE_IGNORE_INSERTS,
			    NULL,
			    dw,
			    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
			    (LPTSTR) &lpMsgBuf,
			    0, NULL);

			wsprintf(szBuffer, _T("Exception handler failed with error %d: %s\n"), dw, lpMsgBuf);
			MessageBox((HWND)MB_ICONEXCLAMATION, szBuffer, _T("Error"), MB_OK);

			LocalFree(lpMsgBuf);
			debug(LOG_ERROR, "Exception handler failed to create file!");
		}

		if (hReportFile)
		{
			wchar_t szBuffer[4196];
			int err;

			SetFilePointer(hReportFile, 0, 0, FILE_END);

			GenerateExceptionReport(hReportFile, pExceptionInfo);
			CloseHandle(hReportFile);

			bool savedMiniDump = false;
			if (MessageBoxW(NULL, L"Warzone crashed unexpectedly, would you like to save a \"mini-dump\" diagnostic file?\r\n(A standard report log .RPT will always be saved.)", L"Warzone 2100", MB_YESNO | MB_ICONEXCLAMATION) == IDYES)
			{
				savedMiniDump = OutputMiniDump(szMinidumpFileName, pExceptionInfo);
			}

			if (savedMiniDump)
			{
				wsprintfW(szBuffer, L"Warzone has crashed. See the following files for more details: \r\n- %s\r\n(The crash report file - please attach this to your bug report)\r\n- %s\r\n(The \"mini-dump\" - for developers only)", szLogFileName, szMinidumpFileName);
			}
			else
			{
				wsprintfW(szBuffer, L"Warzone has crashed.\r\nSee %s for more details\r\n", szLogFileName);
			}
			err = MessageBoxW(NULL, szBuffer, L"Warzone Crashed!", MB_OK | MB_ICONERROR);
			if (err == 0)
			{
				LPVOID lpMsgBuf;
				DWORD dw = GetLastError();
				wchar_t szFailureBuffer[4196];

				FormatMessageW(
				    FORMAT_MESSAGE_ALLOCATE_BUFFER |
				    FORMAT_MESSAGE_FROM_SYSTEM |
				    FORMAT_MESSAGE_IGNORE_INSERTS,
				    NULL,
				    dw,
				    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
				    (LPWSTR) &lpMsgBuf,
				    0, NULL);

				wsprintfW(szFailureBuffer, L"Failed to display message box with error %d: %s\n", dw, lpMsgBuf);
				MessageBoxW(NULL, szFailureBuffer, L"Error", MB_OK | MB_ICONEXCLAMATION);

				LocalFree(lpMsgBuf);
				debug(LOG_ERROR, "Failed to display crash message box: %lu", dw);
			}
			hReportFile = 0;
		}
		SetErrorMode(fuOldErrorMode);
	}

	if (prevExceptionFilter)
	{
		return prevExceptionFilter(pExceptionInfo);
	}
	else
	{
		return EXCEPTION_CONTINUE_SEARCH;
	}
}

void ExchndlSetup(const char *packageVersion, const std::string &writeDir, bool portable_mode)
{
	wchar_t miniDumpPath[PATH_MAX] = {'\0'};
	DWORD dwRetVal = 0;

	// Install the unhandled exception filter function
	prevExceptionFilter = SetUnhandledExceptionFilter(TopLevelExceptionFilter);

	// Retrieve the current version
	formattedVersionString = strdup(packageVersion);

	// Convert the writeDir to WSTRING (UTF-16)
	int wcharsRequired = MultiByteToWideChar(CP_UTF8, 0, writeDir.c_str(), -1, NULL, 0);
	if (wcharsRequired == 0)
	{
		MessageBox((HWND)MB_ICONEXCLAMATION, "Failed to convert writeDir string to wide chars!\nProgram will now exit." , _T("Error"), MB_OK);
		exit(1);
	}
	std::vector<wchar_t> wchar_writeDir(wcharsRequired);
	if(MultiByteToWideChar(CP_UTF8, 0, writeDir.c_str(), -1, &wchar_writeDir[0], wcharsRequired) == 0)
	{
		MessageBox((HWND)MB_ICONEXCLAMATION, "Failed to convert writeDir string to wide chars! (2)\nProgram will now exit." , _T("Error"), MB_OK);
		exit(1);
	}

	// Because of UAC on vista / win7 we use this to write our dumps to (unless we override it via OverrideRPTDirectory())
	// NOTE: CSIDL_PERSONAL =  C:\Users\user name\Documents
	if (!portable_mode && SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, miniDumpPath)))
	{
		PathAppendW(miniDumpPath, wchar_writeDir.data());
		PathAppendW(miniDumpPath, L"\\logs");

		if (!PathFileExistsW(miniDumpPath))
		{
			if (ERROR_SUCCESS != SHCreateDirectoryExW(NULL, miniDumpPath, NULL))
			{
				//last attempt to get a path
				dwRetVal = GetTempPathW(PATH_MAX, miniDumpPath);
				if (dwRetVal > MAX_PATH || (dwRetVal == 0))
				{
					MessageBox((HWND)MB_ICONEXCLAMATION, "Could not created a temporary directory!\nProgram will now exit." , _T("Error"), MB_OK);
					exit(1);
				}
			}
		}
	}
	// in portable mode, we use where they installed it on, since this is a removeable drive (most likely), we will use where the program is located in.
	else if (portable_mode && ((dwRetVal = GetCurrentDirectoryW(MAX_PATH, miniDumpPath))))
	{
		if (dwRetVal > MAX_PATH)
		{
			MessageBox((HWND)MB_ICONEXCLAMATION, _T("Buffer exceeds maximum path to create directory.  Exiting."), _T("Error"), MB_OK);
			exit(1);
		}
		PathAppendW(miniDumpPath, wchar_writeDir.data());
		PathAppendW(miniDumpPath, L"\\logs");
	}
	else
	{
		// should never fail, but if it does, we fall back to this
		dwRetVal = GetTempPathW(PATH_MAX, miniDumpPath);
		if (dwRetVal > MAX_PATH || (dwRetVal == 0))
		{
			MessageBox((HWND)MB_ICONEXCLAMATION, _T("Could not created a temporary directory!\nProgram will now exit.") , _T("Error!"), MB_OK);
			exit(1);
		}
	}

	wcsncpy_s(szLogFileName, L"Warzone2100.RPT", _TRUNCATE);
	wcsncpy_s(szMinidumpFileName, L"Warzone2100.latest.mdmp", _TRUNCATE);

	wchar_t tmpPath[PATH_MAX] = {'\0'};
	if (wcsncpy_s(tmpPath, miniDumpPath, _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (B-1).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }
	if (wcsncat_s_wrapper(tmpPath, L"\\Warzone2100.RPT", _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (C-1).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }
	if (wcsncpy_s(szLogFileName, tmpPath, _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (D-1).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }

	if (wcsncpy_s(tmpPath, miniDumpPath, _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (B-2).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }
	if (wcsncat_s_wrapper(tmpPath, L"\\Warzone2100.latest.mdmp", _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (C-2).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }
	if (wcsncpy_s(szMinidumpFileName, tmpPath, _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (D-2).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }

	atexit(ExchndlShutdown);
}
void ResetRPTDirectory(wchar_t *newPath)
{
	wchar_t miniDumpPath[PATH_MAX] = { '\0' };
	if (wcsncpy_s(miniDumpPath, newPath, _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (E).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }
	if (wcsncat_s_wrapper(miniDumpPath, L"\\logs\\Warzone2100.RPT", _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (F).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }
	if (wcsncpy_s(szLogFileName, miniDumpPath, _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (G).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }

	if (wcsncpy_s(miniDumpPath, newPath, _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (H).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }
	if (wcsncat_s_wrapper(miniDumpPath, L"\\logs\\Warzone2100.latest.mdmp", _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (I).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }
	if (wcsncpy_s(szMinidumpFileName, miniDumpPath, _TRUNCATE) != 0) { MessageBox(NULL, _T("Buffer exceeds maximum path length (J).  Exiting."), _T("Error"), MB_OK | MB_ICONEXCLAMATION); exit(1); }
}
void ExchndlShutdown(void)
{
	if (prevExceptionFilter)
	{
		SetUnhandledExceptionFilter(prevExceptionFilter);
	}

	prevExceptionFilter = NULL;
	free(formattedVersionString);
	formattedVersionString = NULL;
}