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
|
// Version 1. 0. 2. 1 October 2010
// Add Delay on start option, and dynamically load bpq32
// Version 1. 0. 3. 1 October 2011
// Call CloseBPQ32 on exit
// Version 2.0.1.1 July 2002
// Add try/except round main loop
// Version 2.0.1.2 July 2026
// Call RunBPQ32Background() every 10mS
#define _CRT_SECURE_NO_DEPRECATE
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include <time.h>
#include "DbgHelp.h"
#include "winstdint.h"
//#define DYNLOADBPQ // Dynamically Load BPQ32.dll
#include "bpq32.h"
#define WinApp
#include "Versions.h"
#include "GetVersion.h"
VOID APIENTRY SetFrameWnd(HWND hWnd);
#define BPQICON 400
HINSTANCE hInst;
char AppName[] = "BPQ32";
char Title[80] = "Program to hold BPQ32.dll in memory";
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(CONST WNDCLASS*);
BOOL InitApplication(HINSTANCE);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK FrameWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
HWND FrameWnd;
int TimerHandle = 0;
VOID WriteMiniDump()
{
#ifdef WIN32
HANDLE hFile;
BOOL ret;
char FN[256];
sprintf(FN, "%s/Logs/MiniDump%x.dmp", GetBPQDirectory(), time(NULL));
hFile = CreateFile(FN, GENERIC_READ | GENERIC_WRITE,
0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if((hFile != NULL) && (hFile != INVALID_HANDLE_VALUE))
{
// Create the minidump
ret = MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(),
hFile, MiniDumpNormal, 0, 0, 0 );
if(!ret)
Debugprintf("MiniDumpWriteDump failed. Error: %u", GetLastError());
else
Debugprintf("Minidump %s created.", FN);
CloseHandle(hFile);
}
#endif
}
HANDLE process;
void printStack()
{
unsigned int i, n;
void * stack[ 100 ];
unsigned short frames;
DWORD dwDisplacement;
IMAGEHLP_LINE64 line;
char * fptr;
char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)];
PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;
Debugprintf("Stack Backtrace\r\n");
process = GetCurrentProcess();
SymSetOptions(SYMOPT_LOAD_LINES);
SymInitialize( process, NULL, TRUE );
frames = RtlCaptureStackBackTrace(0, 60, stack, NULL );
for( i = 0; i < frames; i++ )
{
n = SymGetLineFromAddr64(process, (DWORD64)stack[i], &dwDisplacement, &line);
n = SymFromAddr( process, ( DWORD64 )( stack[ i ] ), 0, symbol );
if (line.FileName)
{
fptr = line.FileName + (int)strlen(line.FileName); // remove path
while (*fptr != '\\' && *fptr != '/')
fptr--;
fptr++;
Debugprintf("%i: %s - %s Line %d Addr %p", frames - i - 1, symbol->Name, fptr, line.LineNumber, symbol->Address );
}
}
}
int inErrorhandling = 0;
LONG WINAPI UnhandledExcepFilter(PEXCEPTION_POINTERS pExcepPointers)
{
DWORD dwDisplacement;
IMAGEHLP_LINE64 line;
char * fptr;
DWORD ExceptionCode = pExcepPointers->ExceptionRecord->ExceptionCode;
time_t T;
struct tm * tm;
char buffer[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)] = "";
PSYMBOL_INFO symbol = (PSYMBOL_INFO)buffer;
symbol->SizeOfStruct = sizeof(SYMBOL_INFO);
symbol->MaxNameLen = MAX_SYM_NAME;
T = time(NULL);
tm = gmtime(&T);
if (inErrorhandling)
{
MessageBox(NULL,"Program Error in error handler - program closing. See Debug Log for details","BPQ32",MB_ICONSTOP);
exit(0);
}
switch (ExceptionCode)
{
case DBG_PRINTEXCEPTION_C:
return EXCEPTION_CONTINUE_EXECUTION;
case STATUS_INTEGER_DIVIDE_BY_ZERO:
case STATUS_ACCESS_VIOLATION:
case STATUS_STACK_OVERFLOW:
case STATUS_FLOAT_DIVIDE_BY_ZERO:
break;
// ignore others
default:
return EXCEPTION_CONTINUE_SEARCH;
}
inErrorhandling = 1;
if (SymFromAddr(process, ((DWORD)pExcepPointers->ExceptionRecord->ExceptionAddress), 0, symbol))
{
Debugprintf("\r\n\r\nException %x trapped at %02d:%02d:%02d\r\n", ExceptionCode, tm->tm_hour, tm->tm_min, tm->tm_sec);
line.SizeOfStruct = sizeof(IMAGEHLP_LINE64);
SymGetLineFromAddr64(process, (DWORD)pExcepPointers->ExceptionRecord->ExceptionAddress, &dwDisplacement, &line);
if (line.FileName)
{
fptr = line.FileName + (int)strlen(line.FileName); // remove path
while (*fptr != '\\' && *fptr != '/')
fptr--;
fptr++;
Debugprintf("In Procedure %s - %s Line %d Addr %p\r\n", symbol->Name, fptr, line.LineNumber, symbol->Address );
}
}
printStack();
CloseDebugLog();
MessageBox(NULL,"Program Error - program closing. See Debug Log for details","BPQ32",MB_ICONSTOP);
RealCloseAllPrograms();
Sleep(1000);
exit(0);
}
//
// FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
//
// PURPOSE: Entry point for the application.
//
// COMMENTS:
//
// This function initializes the application and processes the
// message loop.
//
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG msg;
int Running = 1;
GetVersionInfo("bpq32.exe");
Debugprintf("BPQ32.exe %s Version %s", lpCmdLine, TextVerstring);
process = GetCurrentProcess();
SymSetOptions(SYMOPT_LOAD_LINES);
SymInitialize( process, NULL, TRUE );
AddVectoredExceptionHandler(1, UnhandledExcepFilter);
if (_stricmp(lpCmdLine, "Wait") == 0) // If AutoRestart then Delay 5 Secs
Sleep(5000);
// GetAPI();
if (!InitInstance(hInstance, nCmdShow))
return (FALSE);
RunBPQ32Background();
// Main message loop:
// __try
{
while(Running)
{
Sleep(10);
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
Running = 0;
}
RunBPQ32Background();
}
}
// #define EXCEPTMSG "BPQ32.exe Main Loop"
// #include "StdExcept.c"
// }
Debugprintf("BPQ32.exe exiting %d", msg.message);
KillTimer(NULL,TimerHandle);
CloseBPQ32(); // Close Ext Drivers if last bpq32 process
CloseDebugLog();
return (msg.wParam);
}
//
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
WNDCLASSEX wndclassMainFrame;
hInst = hInstance; // Store instance handle in our global variable
wndclassMainFrame.cbSize = sizeof(WNDCLASSEX);
wndclassMainFrame.style = CS_HREDRAW | CS_VREDRAW;
wndclassMainFrame.lpfnWndProc = FrameWndProc;
wndclassMainFrame.cbClsExtra = 0;
wndclassMainFrame.cbWndExtra = 0;
wndclassMainFrame.hInstance = hInstance;
wndclassMainFrame.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE(BPQICON));
wndclassMainFrame.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclassMainFrame.hbrBackground = (HBRUSH) GetStockObject(GRAY_BRUSH);
wndclassMainFrame.lpszMenuName = NULL;
wndclassMainFrame.lpszClassName = AppName;
wndclassMainFrame.hIconSm = NULL;
if(!RegisterClassEx(&wndclassMainFrame))
{
Debugprintf("BPQ32.exe RC failed %d", GetLastError());
return 0;
}
FrameWnd = CreateWindow(AppName,
"BPQ32",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, // allows system choose an x position
CW_USEDEFAULT, // allows system choose a y position
CW_USEDEFAULT, // width, CW_USEDEFAULT allows system to choose height and width
CW_USEDEFAULT, // height, CW_USEDEFAULT ignores heights as this is set by setting
// CW_USEDEFAULT in width above.
HWND_MESSAGE, // Message only Window
NULL, // handle to menu
hInstance, // handle to the instance of module
NULL); // Long pointer to a value to be passed to the window through the
// CREATESTRUCT structure passed in the lParam parameter the WM_CREATE message
TimerHandle=SetTimer(FrameWnd,WM_TIMER,5000,NULL);
CheckTimer();
return (TRUE);
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
LRESULT CALLBACK FrameWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) {
case WM_TIMER:
CheckTimer();
return 0;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0);
}
|