|
|
1.1 root 1: /*++
2:
3: Copyright (c) 1993 Microsoft Corporation
4:
5: Module Name:
6:
7: error.c
8:
9: Abstract:
10:
11: This file implements the error handeling functions for the
12: entire DRWTSN32 application. This includes error popups,
13: debug prints, and assertions.
14:
15: Author:
16:
17: Wesley Witt (wesw) 1-May-1993
18:
19: Environment:
20:
21: User Mode
22:
23: --*/
24:
25: #include <windows.h>
26: #include <stdlib.h>
27: #include <stdio.h>
28: #include <string.h>
29:
30: #include "drwatson.h"
31: #include "proto.h"
32: #include "resource.h"
33: #include "messages.h"
34:
35:
36: void
37: FatalError(char *format, ...)
38:
39: /*++
40:
41: Routine Description:
42:
43: This function is called when there is nothing else to do, hence
44: the name FatalError. It puts up a popup and then terminates.
45:
46: Arguments:
47:
48: Same as printf.
49:
50: Return Value:
51:
52: None.
53:
54: --*/
55:
56: {
57: char vbuf[1024];
58: char buf[1024];
59: char szErrorCode[10];
60: DWORD dwCount;
61: DWORD dwArgs[4];
62: va_list arg_ptr;
63:
64: va_start(arg_ptr, format);
65: _vsnprintf(vbuf, sizeof(vbuf), format, arg_ptr);
66: wsprintf( szErrorCode, "%d", GetLastError() );
67: dwArgs[0] = (DWORD)vbuf;
68: dwArgs[1] = (DWORD)szErrorCode;
69: dwCount = FormatMessage(
70: FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
71: NULL,
72: MSG_FATAL_ERROR,
73: 0, // GetUserDefaultLangID(),
74: buf,
75: sizeof(buf),
76: (va_list*)dwArgs
77: );
78: Assert( dwCount != 0 );
79: MessageBox( NULL,
80: buf,
81: LoadRcString( IDS_FATAL_ERROR ),
82: MB_SETFOREGROUND | MB_OK
83: );
84: ExitProcess( 0 );
85: }
86:
87: void
88: NonFatalError(char *format, ...)
89:
90: /*++
91:
92: Routine Description:
93:
94: This function is used to generate a popup with some kind of
95: warning message inside.
96:
97: Arguments:
98:
99: Same as printf.
100:
101: Return Value:
102:
103: None.
104:
105: --*/
106:
107: {
108: char vbuf[1024];
109: char buf[1024];
110: char szErrorCode[10];
111: DWORD dwCount;
112: DWORD dwArgs[4];
113: va_list arg_ptr;
114:
115: va_start(arg_ptr, format);
116: _vsnprintf(vbuf, sizeof(vbuf), format, arg_ptr);
117: wsprintf( szErrorCode, "%d", GetLastError() );
118: dwArgs[0] = (DWORD)vbuf;
119: dwArgs[1] = (DWORD)szErrorCode;
120: dwCount = FormatMessage(
121: FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_ARGUMENT_ARRAY,
122: NULL,
123: MSG_FATAL_ERROR,
124: 0, // GetUserDefaultLangID(),
125: buf,
126: sizeof(buf),
127: (va_list*)dwArgs
128: );
129: Assert( dwCount != 0 );
130: MessageBox( NULL,
131: buf,
132: LoadRcString( IDS_NONFATAL_ERROR ),
133: MB_SETFOREGROUND | MB_OK
134: );
135: }
136:
137: void
138: dprintf(char *format, ...)
139:
140: /*++
141:
142: Routine Description:
143:
144: This function is a var-args version of OutputDebugString.
145:
146: Arguments:
147:
148: Same as printf.
149:
150: Return Value:
151:
152: None.
153:
154: --*/
155:
156: {
157: char buf[1024];
158:
159: va_list arg_ptr;
160: va_start(arg_ptr, format);
161: _vsnprintf(buf, sizeof(buf), format, arg_ptr);
162: OutputDebugString( buf );
163: return;
164: }
165:
166: BOOL CALLBACK
167: AssertDialogProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
168:
169: /*++
170:
171: Routine Description:
172:
173: This is the dialog procedure for the assert dialog box. Normally
174: an assertion box is simply a message box but in this case a Help
175: button is desired so a dialog box is used.
176:
177: Arguments:
178:
179: hDlg - window handle to the dialog box
180: message - message number
181: wParam - first message parameter
182: lParam - second message parameter
183:
184: Return Value:
185:
186: TRUE - did not process the message
187: FALSE - did process the message
188:
189: --*/
190:
191: {
192: char *p;
193: HICON hIcon;
194: char szHelpFileName[MAX_PATH];
195:
196: switch (message) {
197: case WM_INITDIALOG:
198: //
199: // lParam comes in as a pointer to a buffer containing
200: // 2 null terminated strings
201: //
202:
203: //
204: // get the assertion text
205: //
206: p = (char *) lParam;
207: SetDlgItemText( hDlg, ID_ASSERT_TEXT, p );
208:
209: //
210: // get the app name and use it as the title
211: //
212: p += (strlen(p)+1);
213: SetWindowText( hDlg, p );
214:
215: //
216: // set the icon
217: //
218: hIcon = LoadIcon( NULL, IDI_HAND );
219: SendMessage( GetDlgItem( hDlg, ID_ASSERT_ICON ), STM_SETICON, (WPARAM) hIcon, 0 );
220: break;
221:
222: case WM_COMMAND:
223: switch (wParam) {
224: case IDABORT:
225: //
226: // end the dialog and say why
227: //
228: EndDialog( hDlg, IDABORT );
229: break;
230:
231: case IDRETRY:
232: //
233: // end the dialog and say why
234: //
235: EndDialog( hDlg, IDRETRY );
236: break;
237:
238: case IDIGNORE:
239: //
240: // end the dialog and say why
241: //
242: EndDialog( hDlg, IDIGNORE );
243: break;
244:
245: case ID_HELP:
246: //
247: // get the help file name
248: //
249: GetHelpFileName( szHelpFileName, sizeof(szHelpFileName) );
250:
251: //
252: // call winhelp
253: //
254: WinHelp( hDlg, szHelpFileName, HELP_CONTEXT, IDH_ASSERT );
255: break;
256: }
257: break;
258: }
259:
260: return FALSE;
261: }
262:
263: void
264: AssertError(
265: char *Expression,
266: char *File,
267: DWORD LineNumber
268: )
269:
270: /*++
271:
272: Routine Description:
273:
274: Display an assertion failure message box which gives the user a choice
275: as to whether the process should be aborted, the assertion ignored or
276: a break exception generated.
277:
278: Arguments:
279:
280: Expression - Supplies a string representation of the failed assertion.
281: File - Supplies a pointer to the file name where the assertion
282: failed.
283: LineNumber - Supplies the line number in the file where the assertion
284: failed.
285:
286: Return Value:
287:
288: None.
289:
290: --*/
291:
292: {
293: int Response;
294: char ModuleBuffer[ MAX_PATH ];
295: DWORD Length;
296: char Buffer[ 4096 ];
297: DWORD Args[ ] = {
298: ( DWORD ) Expression,
299: ( DWORD ) GetLastError( ),
300: ( DWORD ) File,
301: ( DWORD ) LineNumber
302: };
303:
304: //
305: // Format the assertion string that describes the failure.
306: //
307: FormatMessage(
308: FORMAT_MESSAGE_ARGUMENT_ARRAY
309: | FORMAT_MESSAGE_FROM_STRING
310: & ~FORMAT_MESSAGE_FROM_HMODULE,
311: ( LPVOID ) "Assertion Failed : %1!s! (%2!d!)\nin file %3!hs! at line %4!d!\n",
312: 0,
313: 0, // GetUserDefaultLangID(),
314: Buffer,
315: sizeof( Buffer ),
316: (va_list*)Args
317: );
318:
319:
320: //
321: // Get the asserting module's file name.
322: //
323: Length = GetModuleFileName(
324: NULL,
325: ModuleBuffer,
326: sizeof( ModuleBuffer )
327: );
328:
329: //
330: // put it at the end of the buffer
331: //
332: strcpy( &Buffer[strlen(Buffer)+1], ModuleBuffer );
333:
334: Response = DialogBoxParam( GetModuleHandle( NULL ),
335: MAKEINTRESOURCE( ASSERTDIALOG ),
336: 0,
337: AssertDialogProc,
338: (LPARAM) Buffer
339: );
340:
341: switch( Response ) {
342: case IDABORT:
343: //
344: // Terminate the process.
345: //
346: ExitProcess( (UINT) -1 );
347: break;
348:
349: case IDIGNORE:
350: //
351: // Ignore the failed assertion.
352: //
353: break;
354:
355: case IDRETRY:
356: //
357: // Break into a debugger.
358: //
359: DebugBreak( );
360: break;
361:
362: default:
363: //
364: // Break into a debugger because of a catastrophic failure.
365: //
366: DebugBreak( );
367: break;
368: }
369: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.