summaryrefslogtreecommitdiff
path: root/win32/common/errutil.cpp
blob: 302b4fa8951dd22eaabe5921de930758161e0562 (plain)
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
/*
 * AUTHOR
 * N. Nielsen
 *
 * LICENSE
 * This software is in the public domain.
 *
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties
 * of merchantability, fitness for a particular purpose, and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages, or other liability, whether in an action of
 * contract, tort, or otherwise, arising from, out of, or in connection
 * with the software or the use or other dealings in the software.
 *
 * SUPPORT
 * Send bug reports to: <nielsen@memberwebs.com>
 */

#include "stdafx.h"
#include "rliberr.h"
#include "errutil.h"

// rlibError: -------------------------------------------------------------
//  Displays errors from rlib. Assumes we have the appropriate
//  error codes compiled in as resources

HRESULT rlibError(HWND parent, int code, r_script* script)
{
	ASSERT(code < 0);
	USES_CONVERSION;

    LPVOID lpMsgBuf;
	HRESULT hr = HRESULT_FROM_RLIB(code);
	string message;

	DWORD dwRet = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_HMODULE |
								  FORMAT_MESSAGE_MAX_WIDTH_MASK, GetModuleHandle(NULL), hr,
								  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
								  (LPTSTR)&lpMsgBuf, 0, NULL);
	if(dwRet && lpMsgBuf)
	{
		message.append((LPTSTR)lpMsgBuf);

		// Free the buffer.
		::LocalFree(lpMsgBuf);

		// If there was a specific error message then tack that on
		if(script->error)
		{
			message.append(_T("\n\n"));
			message.append(A2T(script->error));
		}

		// If there's an error line then tack that on
		if(script->errline != 0)
		{
			string line;
			line.format(_T(" (near line %d)"), script->errline);
			message.append(line);
		}

		MessageBox(parent, message.c_str(), _T("Rep Droplet"), MB_OK | MB_ICONSTOP);
	}

	return hr;
}


// errorMessage: -----------------------------------------------------------
//  Displays standard windows errors.

HRESULT errorMessage(HWND parent, HRESULT hr, LPCTSTR format, ...)
{
	string message;

	va_list ap;
	va_start(ap, format);

	message.format_v(format, ap);

	va_end(ap);

	if(FAILED(hr))
	{
	    LPVOID lpMsgBuf;

		DWORD dwRet = ::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
									  FORMAT_MESSAGE_MAX_WIDTH_MASK, NULL, hr,
									  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
									  (LPTSTR) &lpMsgBuf, 0, NULL);
		if(dwRet && lpMsgBuf)
		{
			message.append(_T("\n\n"));
			message.append((LPTSTR)lpMsgBuf);
			message.resize(message.size() - 2); // Take off new line

			// Free the buffer.
			::LocalFree(lpMsgBuf);
		}
	}

	MessageBox(parent, message.c_str(), _T("Rep Droplet"), MB_OK | MB_ICONSTOP);

	return hr == S_OK ? E_FAIL : hr;
}