-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviewer.c
More file actions
211 lines (180 loc) · 6.09 KB
/
Copy pathviewer.c
File metadata and controls
211 lines (180 loc) · 6.09 KB
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
/*
* viewer.c - BER.exe
* Crash-dump viewer launched by BERDebugger.exe.
*/
#include "common.h"
#include "viewer.h"
#define ID_EDIT 100
#define ID_OK 101
/* Global handles used in WndProc */
static HWND s_hEdit = NULL;
static HWND s_hOK = NULL;
static HWND s_hWnd = NULL;
/* ---------------------------------------------------------------
* WndProc
* ------------------------------------------------------------- */
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
RECT rc;
GetClientRect(hWnd, &rc);
/* Multi-line read-only edit for the register dump */
s_hEdit = CreateWindow(
L"EDIT", L"",
WS_CHILD | WS_VISIBLE | WS_VSCROLL |
ES_MULTILINE | ES_READONLY | ES_AUTOVSCROLL,
0, 0, rc.right, rc.bottom - 45,
hWnd, (HMENU)ID_EDIT,
((LPCREATESTRUCT)lParam)->hInstance, NULL);
/* OK button at the bottom-centre */
s_hOK = CreateWindow(
L"BUTTON", L"OK",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON | BS_DEFPUSHBUTTON,
rc.right / 2 - 50, rc.bottom - 40, 100, 32,
hWnd, (HMENU)ID_OK,
((LPCREATESTRUCT)lParam)->hInstance, NULL);
/* Use a fixed-pitch font so register columns line up */
{
HFONT hFont = (HFONT)GetStockObject(SYSTEM_FIXED_FONT);
if (hFont)
SendMessage(s_hEdit, WM_SETFONT, (WPARAM)hFont, FALSE);
}
break;
}
case WM_SIZE:
{
int w = LOWORD(lParam);
int h = HIWORD(lParam);
if (s_hEdit) MoveWindow(s_hEdit, 0, 0, w, h - 45, TRUE);
if (s_hOK) MoveWindow(s_hOK, w / 2 - 50, h - 40, 100, 32, TRUE);
break;
}
case WM_COMMAND:
if (LOWORD(wParam) == ID_OK)
PostQuitMessage(0);
break;
case WM_COPYDATA:
{
const COPYDATASTRUCT *cds = (const COPYDATASTRUCT *)lParam;
if (cds->dwData == COPYDATA_KEY &&
cds->cbData == sizeof(BERExceptionContext_t))
{
const BERExceptionContext_t *dump = (const BERExceptionContext_t *)cds->lpData;
WCHAR *buf = (WCHAR *)LocalAlloc(LMEM_ZEROINIT,
4096 * sizeof(WCHAR));
if (buf != NULL)
{
FormatDump(buf, 4096, dump);
SetWindowText(s_hEdit, buf);
LocalFree(buf);
}
/* Now reveal the window */
ShowWindow(hWnd, SW_SHOWNORMAL);
UpdateWindow(hWnd);
SetForegroundWindow(hWnd);
}
return TRUE;
}
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
/* ---------------------------------------------------------------
* RunViewer
* ------------------------------------------------------------- */
int RunViewer(HINSTANCE hInstance, int nCmdShow, DWORD processId)
{
WNDCLASS wc;
HWND hWnd;
WCHAR eventName[64];
HANDLE hEvent;
DWORD waitResult;
MSG msg;
int screenW, screenH;
(void)nCmdShow;
/* Register window class */
memset(&wc, 0, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = WINDOW_CLASS;
if (!RegisterClass(&wc))
{
AppendLog(L"Viewer: RegisterClass failed");
return EXIT_FAILURE;
}
/* Use full screen size */
screenW = GetSystemMetrics(SM_CXSCREEN);
screenH = GetSystemMetrics(SM_CYSCREEN);
/* Create window - hidden until WM_COPYDATA arrives */
hWnd = CreateWindow(
WINDOW_CLASS,
L"BER - Brain Exception Reporter",
WS_CAPTION | WS_SYSMENU | WS_SIZEBOX,
0, 0, screenW, screenH,
NULL, NULL, hInstance, NULL);
if (hWnd == NULL)
{
AppendLog(L"Viewer: CreateWindow failed");
return EXIT_FAILURE;
}
s_hWnd = hWnd;
/* Open (or create) the named event that BERDebugger will signal */
wsprintf(eventName, EVENT_NAME_FMT, processId);
hEvent = CreateEvent(NULL, TRUE, FALSE, eventName);
if (hEvent == NULL)
{
AppendLog(L"Viewer: CreateEvent failed");
DestroyWindow(hWnd);
return EXIT_FAILURE;
}
/* Block up to 5 seconds waiting for BERDebugger to signal us */
waitResult = WaitForSingleObject(hEvent, 5000);
CloseHandle(hEvent);
if (waitResult == WAIT_TIMEOUT)
{
/* BERDebugger did not signal in time - exit silently */
DestroyWindow(hWnd);
return EXIT_FAILURE;
}
/*
* Event fired: BERDebugger has found our window and is about to
* send WM_COPYDATA. Enter the message loop; WndProc will show
* the window once the data arrives.
*/
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
/* ---------------------------------------------------------------
* WinMain - BER.exe entry point
* ------------------------------------------------------------- */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPWSTR lpszCmdLine, int nCmdShow)
{
DWORD processId;
(void)hPrevInstance;
/* Argument must be "0x<hex>" */
if (lstrlenW(lpszCmdLine) <= 2 ||
lpszCmdLine[0] != L'0' ||
lpszCmdLine[1] != L'x')
{
return EXIT_FAILURE;
}
swscanf(&lpszCmdLine[2], L"%lx", &processId);
return RunViewer(hInstance, nCmdShow, processId);
}