2 * msvcrt.dll exit functions
4 * Copyright 2000 Jon Griffiths
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
29 #define LOCK_EXIT _mlock(_EXIT_LOCK1)
30 #define UNLOCK_EXIT _munlock(_EXIT_LOCK1)
32 static MSVCRT_purecall_handler purecall_handler
= NULL
;
34 typedef struct MSVCRT__onexit_table_t
36 MSVCRT__onexit_t
*_first
;
37 MSVCRT__onexit_t
*_last
;
38 MSVCRT__onexit_t
*_end
;
39 } MSVCRT__onexit_table_t
;
41 static MSVCRT__onexit_table_t MSVCRT_atexit_table
;
44 static MSVCRT__onexit_table_t MSVCRT_quick_exit_table
;
47 typedef void (__stdcall
*_tls_callback_type
)(void*,ULONG
,void*);
48 static _tls_callback_type tls_atexit_callback
;
50 static CRITICAL_SECTION MSVCRT_onexit_cs
;
51 static CRITICAL_SECTION_DEBUG MSVCRT_onexit_cs_debug
=
53 0, 0, &MSVCRT_onexit_cs
,
54 { &MSVCRT_onexit_cs_debug
.ProcessLocksList
, &MSVCRT_onexit_cs_debug
.ProcessLocksList
},
55 0, 0, { (DWORD_PTR
)(__FILE__
": MSVCRT_onexit_cs") }
57 static CRITICAL_SECTION MSVCRT_onexit_cs
= { &MSVCRT_onexit_cs_debug
, -1, 0, 0, 0, 0 };
59 extern int MSVCRT_app_type
;
60 extern MSVCRT_wchar_t
*MSVCRT__wpgmptr
;
62 static unsigned int MSVCRT_abort_behavior
= MSVCRT__WRITE_ABORT_MSG
| MSVCRT__CALL_REPORTFAULT
;
63 static int MSVCRT_error_mode
= MSVCRT__OUT_TO_DEFAULT
;
65 void (*CDECL _aexit_rtn
)(int) = MSVCRT__exit
;
67 static int initialize_onexit_table(MSVCRT__onexit_table_t
*table
)
72 if (table
->_first
== table
->_end
)
73 table
->_last
= table
->_end
= table
->_first
= NULL
;
77 static int register_onexit_function(MSVCRT__onexit_table_t
*table
, MSVCRT__onexit_t func
)
82 EnterCriticalSection(&MSVCRT_onexit_cs
);
85 table
->_first
= MSVCRT_calloc(32, sizeof(void *));
88 WARN("failed to allocate initial table.\n");
89 LeaveCriticalSection(&MSVCRT_onexit_cs
);
92 table
->_last
= table
->_first
;
93 table
->_end
= table
->_first
+ 32;
97 if (table
->_last
== table
->_end
)
99 int len
= table
->_end
- table
->_first
;
100 MSVCRT__onexit_t
*tmp
= MSVCRT_realloc(table
->_first
, 2 * len
* sizeof(void *));
103 WARN("failed to grow table.\n");
104 LeaveCriticalSection(&MSVCRT_onexit_cs
);
108 table
->_end
= table
->_first
+ 2 * len
;
109 table
->_last
= table
->_first
+ len
;
112 *table
->_last
= func
;
114 LeaveCriticalSection(&MSVCRT_onexit_cs
);
118 static int execute_onexit_table(MSVCRT__onexit_table_t
*table
)
120 MSVCRT__onexit_t
*func
;
121 MSVCRT__onexit_table_t copy
;
126 EnterCriticalSection(&MSVCRT_onexit_cs
);
127 if (!table
->_first
|| table
->_first
>= table
->_last
)
129 LeaveCriticalSection(&MSVCRT_onexit_cs
);
132 copy
._first
= table
->_first
;
133 copy
._last
= table
->_last
;
134 copy
._end
= table
->_end
;
135 memset(table
, 0, sizeof(*table
));
136 initialize_onexit_table(table
);
137 LeaveCriticalSection(&MSVCRT_onexit_cs
);
139 for (func
= copy
._last
- 1; func
>= copy
._first
; func
--)
145 MSVCRT_free(copy
._first
);
149 static void call_atexit(void)
151 /* Note: should only be called with the exit lock held */
152 if (tls_atexit_callback
) tls_atexit_callback(NULL
, DLL_PROCESS_DETACH
, NULL
);
153 execute_onexit_table(&MSVCRT_atexit_table
);
156 /*********************************************************************
157 * __dllonexit (MSVCRT.@)
159 MSVCRT__onexit_t CDECL
__dllonexit(MSVCRT__onexit_t func
, MSVCRT__onexit_t
**start
, MSVCRT__onexit_t
**end
)
161 MSVCRT__onexit_t
*tmp
;
164 TRACE("(%p,%p,%p)\n", func
, start
, end
);
166 if (!start
|| !*start
|| !end
|| !*end
)
168 FIXME("bad table\n");
172 len
= (*end
- *start
);
174 TRACE("table start %p-%p, %d entries\n", *start
, *end
, len
);
179 tmp
= MSVCRT_realloc(*start
, len
* sizeof(*tmp
));
185 TRACE("new table start %p-%p, %d entries\n", *start
, *end
, len
);
189 /*********************************************************************
192 void CDECL
MSVCRT__exit(int exitcode
)
194 TRACE("(%d)\n", exitcode
);
195 ExitProcess(exitcode
);
198 /* Print out an error message with an option to debug */
199 static void DoMessageBoxW(const MSVCRT_wchar_t
*lead
, const MSVCRT_wchar_t
*message
)
201 static const MSVCRT_wchar_t message_format
[] = {'%','s','\n','\n','P','r','o','g','r','a','m',':',' ','%','s','\n',
202 '%','s','\n','\n','P','r','e','s','s',' ','O','K',' ','t','o',' ','e','x','i','t',' ','t','h','e',' ',
203 'p','r','o','g','r','a','m',',',' ','o','r',' ','C','a','n','c','e','l',' ','t','o',' ','s','t','a','r','t',' ',
204 't','h','e',' ','W','i','n','e',' ','d','e','b','u','g','g','e','r','.','\n',0};
205 static const WCHAR title
[] =
206 {'W','i','n','e',' ','C','+','+',' ','R','u','n','t','i','m','e',' ','L','i','b','r','a','r','y',0};
208 MSGBOXPARAMSW msgbox
;
209 MSVCRT_wchar_t text
[2048];
212 MSVCRT__snwprintf(text
, ARRAY_SIZE(text
), message_format
, lead
, MSVCRT__wpgmptr
, message
);
214 msgbox
.cbSize
= sizeof(msgbox
);
215 msgbox
.hwndOwner
= GetActiveWindow();
216 msgbox
.hInstance
= 0;
217 msgbox
.lpszText
= text
;
218 msgbox
.lpszCaption
= title
;
219 msgbox
.dwStyle
= MB_OKCANCEL
|MB_ICONERROR
;
220 msgbox
.lpszIcon
= NULL
;
221 msgbox
.dwContextHelpId
= 0;
222 msgbox
.lpfnMsgBoxCallback
= NULL
;
223 msgbox
.dwLanguageId
= LANG_NEUTRAL
;
225 ret
= MessageBoxIndirectW(&msgbox
);
230 static void DoMessageBox(const char *lead
, const char *message
)
232 MSVCRT_wchar_t leadW
[1024], messageW
[1024];
234 MSVCRT_mbstowcs(leadW
, lead
, 1024);
235 MSVCRT_mbstowcs(messageW
, message
, 1024);
237 DoMessageBoxW(leadW
, messageW
);
240 /*********************************************************************
241 * _amsg_exit (MSVCRT.@)
243 void CDECL
_amsg_exit(int errnum
)
245 TRACE("(%d)\n", errnum
);
247 if ((MSVCRT_error_mode
== MSVCRT__OUT_TO_MSGBOX
) ||
248 ((MSVCRT_error_mode
== MSVCRT__OUT_TO_DEFAULT
) && (MSVCRT_app_type
== 2)))
251 sprintf(text
, "Error: R60%d",errnum
);
252 DoMessageBox("Runtime error!", text
);
255 _cprintf("\nruntime error R60%d\n",errnum
);
259 /*********************************************************************
262 void CDECL
MSVCRT_abort(void)
266 if (MSVCRT_abort_behavior
& MSVCRT__WRITE_ABORT_MSG
)
268 if ((MSVCRT_error_mode
== MSVCRT__OUT_TO_MSGBOX
) ||
269 ((MSVCRT_error_mode
== MSVCRT__OUT_TO_DEFAULT
) && (MSVCRT_app_type
== 2)))
271 DoMessageBox("Runtime error!", "abnormal program termination");
274 _cputs("\nabnormal program termination\n");
276 MSVCRT_raise(MSVCRT_SIGABRT
);
277 /* in case raise() returns */
282 /*********************************************************************
283 * _set_abort_behavior (MSVCR80.@)
285 unsigned int CDECL
MSVCRT__set_abort_behavior(unsigned int flags
, unsigned int mask
)
287 unsigned int old
= MSVCRT_abort_behavior
;
289 TRACE("%x, %x\n", flags
, mask
);
290 if (mask
& MSVCRT__CALL_REPORTFAULT
)
291 FIXME("_WRITE_CALL_REPORTFAULT unhandled\n");
293 MSVCRT_abort_behavior
= (MSVCRT_abort_behavior
& ~mask
) | (flags
& mask
);
298 /*********************************************************************
299 * _wassert (MSVCRT.@)
301 void CDECL
MSVCRT__wassert(const MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* file
, unsigned int line
)
303 static const MSVCRT_wchar_t assertion_failed
[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d','!',0};
304 static const MSVCRT_wchar_t format_msgbox
[] = {'F','i','l','e',':',' ','%','s','\n','L','i','n','e',':',' ','%','d',
305 '\n','\n','E','x','p','r','e','s','s','i','o','n',':',' ','\"','%','s','\"',0};
306 static const MSVCRT_wchar_t format_console
[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d',':',' ',
307 '%','s',',',' ','f','i','l','e',' ','%','s',',',' ','l','i','n','e',' ','%','d','\n','\n',0};
309 TRACE("(%s,%s,%d)\n", debugstr_w(str
), debugstr_w(file
), line
);
311 if ((MSVCRT_error_mode
== MSVCRT__OUT_TO_MSGBOX
) ||
312 ((MSVCRT_error_mode
== MSVCRT__OUT_TO_DEFAULT
) && (MSVCRT_app_type
== 2)))
314 MSVCRT_wchar_t text
[2048];
315 MSVCRT__snwprintf(text
, sizeof(text
), format_msgbox
, file
, line
, str
);
316 DoMessageBoxW(assertion_failed
, text
);
319 _cwprintf(format_console
, str
, file
, line
);
321 MSVCRT_raise(MSVCRT_SIGABRT
);
325 /*********************************************************************
328 void CDECL
MSVCRT__assert(const char* str
, const char* file
, unsigned int line
)
330 MSVCRT_wchar_t strW
[1024], fileW
[1024];
332 MSVCRT_mbstowcs(strW
, str
, 1024);
333 MSVCRT_mbstowcs(fileW
, file
, 1024);
335 MSVCRT__wassert(strW
, fileW
, line
);
338 /*********************************************************************
341 void CDECL
MSVCRT__c_exit(void)
344 /* All cleanup is done on DLL detach; Return to caller */
347 /*********************************************************************
350 void CDECL
MSVCRT__cexit(void)
358 /*********************************************************************
361 MSVCRT__onexit_t CDECL
MSVCRT__onexit(MSVCRT__onexit_t func
)
363 TRACE("(%p)\n",func
);
369 register_onexit_function(&MSVCRT_atexit_table
, func
);
375 /*********************************************************************
378 void CDECL
MSVCRT_exit(int exitcode
)
381 static const WCHAR mscoreeW
[] = {'m','s','c','o','r','e','e',0};
382 void (WINAPI
*pCorExitProcess
)(int);
384 TRACE("(%d)\n",exitcode
);
387 hmscoree
= GetModuleHandleW(mscoreeW
);
391 pCorExitProcess
= (void*)GetProcAddress(hmscoree
, "CorExitProcess");
394 pCorExitProcess(exitcode
);
397 ExitProcess(exitcode
);
400 /*********************************************************************
403 int CDECL
MSVCRT_atexit(void (__cdecl
*func
)(void))
405 TRACE("(%p)\n", func
);
406 return MSVCRT__onexit((MSVCRT__onexit_t
)func
) == (MSVCRT__onexit_t
)func
? 0 : -1;
411 /*********************************************************************
412 * _crt_at_quick_exit (UCRTBASE.@)
414 int CDECL
MSVCRT__crt_at_quick_exit(void (__cdecl
*func
)(void))
416 TRACE("(%p)\n", func
);
417 return register_onexit_function(&MSVCRT_quick_exit_table
, (MSVCRT__onexit_t
)func
);
420 /*********************************************************************
421 * quick_exit (MSVCRT.@)
423 void CDECL
MSVCRT_quick_exit(int exitcode
)
425 TRACE("(%d)\n", exitcode
);
427 execute_onexit_table(&MSVCRT_quick_exit_table
);
428 MSVCRT__exit(exitcode
);
431 /*********************************************************************
432 * _crt_atexit (UCRTBASE.@)
434 int CDECL
MSVCRT__crt_atexit(void (__cdecl
*func
)(void))
436 TRACE("(%p)\n", func
);
437 return MSVCRT__onexit((MSVCRT__onexit_t
)func
) == (MSVCRT__onexit_t
)func
? 0 : -1;
440 /*********************************************************************
441 * _initialize_onexit_table (UCRTBASE.@)
443 int CDECL
MSVCRT__initialize_onexit_table(MSVCRT__onexit_table_t
*table
)
445 TRACE("(%p)\n", table
);
447 return initialize_onexit_table(table
);
450 /*********************************************************************
451 * _register_onexit_function (UCRTBASE.@)
453 int CDECL
MSVCRT__register_onexit_function(MSVCRT__onexit_table_t
*table
, MSVCRT__onexit_t func
)
455 TRACE("(%p %p)\n", table
, func
);
457 return register_onexit_function(table
, func
);
460 /*********************************************************************
461 * _execute_onexit_table (UCRTBASE.@)
463 int CDECL
MSVCRT__execute_onexit_table(MSVCRT__onexit_table_t
*table
)
465 TRACE("(%p)\n", table
);
467 return execute_onexit_table(table
);
470 /*********************************************************************
471 * _register_thread_local_exe_atexit_callback (UCRTBASE.@)
473 void CDECL
_register_thread_local_exe_atexit_callback(_tls_callback_type callback
)
475 TRACE("(%p)\n", callback
);
476 tls_atexit_callback
= callback
;
479 #endif /* _MSVCR_VER>=140 */
482 /*********************************************************************
483 * _set_purecall_handler (MSVCR71.@)
485 MSVCRT_purecall_handler CDECL
_set_purecall_handler(MSVCRT_purecall_handler function
)
487 MSVCRT_purecall_handler ret
= purecall_handler
;
489 TRACE("(%p)\n", function
);
490 purecall_handler
= function
;
496 /*********************************************************************
497 * _get_purecall_handler (MSVCR80.@)
499 MSVCRT_purecall_handler CDECL
_get_purecall_handler(void)
502 return purecall_handler
;
506 /*********************************************************************
507 * _purecall (MSVCRT.@)
509 void CDECL
_purecall(void)
518 /******************************************************************************
519 * _set_error_mode (MSVCRT.@)
521 * Set the error mode, which describes where the C run-time writes error messages.
524 * mode - the new error mode
527 * The old error mode.
530 int CDECL
_set_error_mode(int mode
)
533 const int old
= MSVCRT_error_mode
;
534 if ( MSVCRT__REPORT_ERRMODE
!= mode
) {
535 MSVCRT_error_mode
= mode
;