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__onexit_t
*MSVCRT_atexit_table
= NULL
;
33 static int MSVCRT_atexit_table_size
= 0;
34 static int MSVCRT_atexit_registered
= 0; /* Points to free slot */
35 static MSVCRT_purecall_handler purecall_handler
= NULL
;
37 typedef struct MSVCRT__onexit_table_t
39 MSVCRT__onexit_t
*_first
;
40 MSVCRT__onexit_t
*_last
;
41 MSVCRT__onexit_t
*_end
;
42 } MSVCRT__onexit_table_t
;
44 extern int MSVCRT_app_type
;
45 extern MSVCRT_wchar_t
*MSVCRT__wpgmptr
;
47 static unsigned int MSVCRT_abort_behavior
= MSVCRT__WRITE_ABORT_MSG
| MSVCRT__CALL_REPORTFAULT
;
48 static int MSVCRT_error_mode
= MSVCRT__OUT_TO_DEFAULT
;
50 void (*CDECL _aexit_rtn
)(int) = MSVCRT__exit
;
52 /* INTERNAL: call atexit functions */
53 static void __MSVCRT__call_atexit(void)
55 /* Note: should only be called with the exit lock held */
56 TRACE("%d atext functions to call\n", MSVCRT_atexit_registered
);
57 /* Last registered gets executed first */
58 while (MSVCRT_atexit_registered
> 0)
60 MSVCRT_atexit_registered
--;
61 TRACE("next is %p\n",MSVCRT_atexit_table
[MSVCRT_atexit_registered
]);
62 if (MSVCRT_atexit_table
[MSVCRT_atexit_registered
])
63 (*MSVCRT_atexit_table
[MSVCRT_atexit_registered
])();
68 /*********************************************************************
69 * __dllonexit (MSVCRT.@)
71 MSVCRT__onexit_t CDECL
__dllonexit(MSVCRT__onexit_t func
, MSVCRT__onexit_t
**start
, MSVCRT__onexit_t
**end
)
73 MSVCRT__onexit_t
*tmp
;
76 TRACE("(%p,%p,%p)\n", func
, start
, end
);
78 if (!start
|| !*start
|| !end
|| !*end
)
84 len
= (*end
- *start
);
86 TRACE("table start %p-%p, %d entries\n", *start
, *end
, len
);
91 tmp
= MSVCRT_realloc(*start
, len
* sizeof(*tmp
));
97 TRACE("new table start %p-%p, %d entries\n", *start
, *end
, len
);
101 /*********************************************************************
104 void CDECL
MSVCRT__exit(int exitcode
)
106 TRACE("(%d)\n", exitcode
);
107 ExitProcess(exitcode
);
110 /* Print out an error message with an option to debug */
111 static void DoMessageBoxW(const MSVCRT_wchar_t
*lead
, const MSVCRT_wchar_t
*message
)
113 static const MSVCRT_wchar_t message_format
[] = {'%','s','\n','\n','P','r','o','g','r','a','m',':',' ','%','s','\n',
114 '%','s','\n','\n','P','r','e','s','s',' ','O','K',' ','t','o',' ','e','x','i','t',' ','t','h','e',' ',
115 'p','r','o','g','r','a','m',',',' ','o','r',' ','C','a','n','c','e','l',' ','t','o',' ','s','t','a','r','t',' ',
116 't','h','e',' ','W','i','n','e',' ','d','e','b','u','g','g','e','r','.','\n',0};
117 static const WCHAR title
[] =
118 {'W','i','n','e',' ','C','+','+',' ','R','u','n','t','i','m','e',' ','L','i','b','r','a','r','y',0};
120 MSGBOXPARAMSW msgbox
;
121 MSVCRT_wchar_t text
[2048];
124 MSVCRT__snwprintf(text
, sizeof(text
)/sizeof(text
[0]), message_format
,
125 lead
, MSVCRT__wpgmptr
, message
);
127 msgbox
.cbSize
= sizeof(msgbox
);
128 msgbox
.hwndOwner
= GetActiveWindow();
129 msgbox
.hInstance
= 0;
130 msgbox
.lpszText
= text
;
131 msgbox
.lpszCaption
= title
;
132 msgbox
.dwStyle
= MB_OKCANCEL
|MB_ICONERROR
;
133 msgbox
.lpszIcon
= NULL
;
134 msgbox
.dwContextHelpId
= 0;
135 msgbox
.lpfnMsgBoxCallback
= NULL
;
136 msgbox
.dwLanguageId
= LANG_NEUTRAL
;
138 ret
= MessageBoxIndirectW(&msgbox
);
143 static void DoMessageBox(const char *lead
, const char *message
)
145 MSVCRT_wchar_t leadW
[1024], messageW
[1024];
147 MSVCRT_mbstowcs(leadW
, lead
, 1024);
148 MSVCRT_mbstowcs(messageW
, message
, 1024);
150 DoMessageBoxW(leadW
, messageW
);
153 /*********************************************************************
154 * _amsg_exit (MSVCRT.@)
156 void CDECL
_amsg_exit(int errnum
)
158 TRACE("(%d)\n", errnum
);
160 if ((MSVCRT_error_mode
== MSVCRT__OUT_TO_MSGBOX
) ||
161 ((MSVCRT_error_mode
== MSVCRT__OUT_TO_DEFAULT
) && (MSVCRT_app_type
== 2)))
164 sprintf(text
, "Error: R60%d",errnum
);
165 DoMessageBox("Runtime error!", text
);
168 _cprintf("\nruntime error R60%d\n",errnum
);
172 /*********************************************************************
175 void CDECL
MSVCRT_abort(void)
179 if (MSVCRT_abort_behavior
& MSVCRT__WRITE_ABORT_MSG
)
181 if ((MSVCRT_error_mode
== MSVCRT__OUT_TO_MSGBOX
) ||
182 ((MSVCRT_error_mode
== MSVCRT__OUT_TO_DEFAULT
) && (MSVCRT_app_type
== 2)))
184 DoMessageBox("Runtime error!", "abnormal program termination");
187 _cputs("\nabnormal program termination\n");
189 MSVCRT_raise(MSVCRT_SIGABRT
);
190 /* in case raise() returns */
194 /*********************************************************************
195 * _set_abort_behavior (MSVCR80.@)
197 unsigned int CDECL
MSVCRT__set_abort_behavior(unsigned int flags
, unsigned int mask
)
199 unsigned int old
= MSVCRT_abort_behavior
;
201 TRACE("%x, %x\n", flags
, mask
);
202 if (mask
& MSVCRT__CALL_REPORTFAULT
)
203 FIXME("_WRITE_CALL_REPORTFAULT unhandled\n");
205 MSVCRT_abort_behavior
= (MSVCRT_abort_behavior
& ~mask
) | (flags
& mask
);
209 /*********************************************************************
210 * _wassert (MSVCRT.@)
212 void CDECL
MSVCRT__wassert(const MSVCRT_wchar_t
* str
, const MSVCRT_wchar_t
* file
, unsigned int line
)
214 static const MSVCRT_wchar_t assertion_failed
[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d','!',0};
215 static const MSVCRT_wchar_t format_msgbox
[] = {'F','i','l','e',':',' ','%','s','\n','L','i','n','e',':',' ','%','d',
216 '\n','\n','E','x','p','r','e','s','s','i','o','n',':',' ','\"','%','s','\"',0};
217 static const MSVCRT_wchar_t format_console
[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d',':',' ',
218 '%','s',',',' ','f','i','l','e',' ','%','s',',',' ','l','i','n','e',' ','%','d','\n','\n',0};
220 TRACE("(%s,%s,%d)\n", debugstr_w(str
), debugstr_w(file
), line
);
222 if ((MSVCRT_error_mode
== MSVCRT__OUT_TO_MSGBOX
) ||
223 ((MSVCRT_error_mode
== MSVCRT__OUT_TO_DEFAULT
) && (MSVCRT_app_type
== 2)))
225 MSVCRT_wchar_t text
[2048];
226 MSVCRT__snwprintf(text
, sizeof(text
), format_msgbox
, file
, line
, str
);
227 DoMessageBoxW(assertion_failed
, text
);
230 _cwprintf(format_console
, str
, file
, line
);
232 MSVCRT_raise(MSVCRT_SIGABRT
);
236 /*********************************************************************
239 void CDECL
MSVCRT__assert(const char* str
, const char* file
, unsigned int line
)
241 MSVCRT_wchar_t strW
[1024], fileW
[1024];
243 MSVCRT_mbstowcs(strW
, str
, 1024);
244 MSVCRT_mbstowcs(fileW
, file
, 1024);
246 MSVCRT__wassert(strW
, fileW
, line
);
249 /*********************************************************************
252 void CDECL
MSVCRT__c_exit(void)
255 /* All cleanup is done on DLL detach; Return to caller */
258 /*********************************************************************
261 void CDECL
MSVCRT__cexit(void)
265 __MSVCRT__call_atexit();
269 /*********************************************************************
272 MSVCRT__onexit_t CDECL
MSVCRT__onexit(MSVCRT__onexit_t func
)
274 TRACE("(%p)\n",func
);
280 if (MSVCRT_atexit_registered
> MSVCRT_atexit_table_size
- 1)
282 MSVCRT__onexit_t
*newtable
;
283 TRACE("expanding table\n");
284 newtable
= MSVCRT_calloc(MSVCRT_atexit_table_size
+ 32, sizeof(void *));
291 memcpy (newtable
, MSVCRT_atexit_table
, MSVCRT_atexit_table_size
*sizeof(void *));
292 MSVCRT_atexit_table_size
+= 32;
293 MSVCRT_free (MSVCRT_atexit_table
);
294 MSVCRT_atexit_table
= newtable
;
296 MSVCRT_atexit_table
[MSVCRT_atexit_registered
] = func
;
297 MSVCRT_atexit_registered
++;
302 /*********************************************************************
305 void CDECL
MSVCRT_exit(int exitcode
)
308 static const WCHAR mscoreeW
[] = {'m','s','c','o','r','e','e',0};
309 void (WINAPI
*pCorExitProcess
)(int);
311 TRACE("(%d)\n",exitcode
);
314 hmscoree
= GetModuleHandleW(mscoreeW
);
318 pCorExitProcess
= (void*)GetProcAddress(hmscoree
, "CorExitProcess");
321 pCorExitProcess(exitcode
);
324 ExitProcess(exitcode
);
327 /*********************************************************************
330 int CDECL
MSVCRT_atexit(void (*func
)(void))
332 TRACE("(%p)\n", func
);
333 return MSVCRT__onexit((MSVCRT__onexit_t
)func
) == (MSVCRT__onexit_t
)func
? 0 : -1;
336 /*********************************************************************
337 * _crt_atexit (UCRTBASE.@)
339 int CDECL
MSVCRT__crt_atexit(void (*func
)(void))
341 TRACE("(%p)\n", func
);
342 return MSVCRT__onexit((MSVCRT__onexit_t
)func
) == (MSVCRT__onexit_t
)func
? 0 : -1;
346 /*********************************************************************
347 * _initialize_onexit_table (UCRTBASE.@)
349 int CDECL
MSVCRT__initialize_onexit_table(MSVCRT__onexit_table_t
*table
)
351 TRACE("(%p)\n", table
);
356 if (table
->_first
== table
->_end
)
357 table
->_last
= table
->_end
= table
->_first
= NULL
;
361 /*********************************************************************
362 * _register_onexit_function (UCRTBASE.@)
364 int CDECL
MSVCRT__register_onexit_function(MSVCRT__onexit_table_t
*table
, MSVCRT__onexit_t func
)
366 TRACE("(%p %p)\n", table
, func
);
373 table
->_first
= MSVCRT_calloc(32, sizeof(void *));
376 WARN("failed to allocate initial table.\n");
379 table
->_last
= table
->_first
;
380 table
->_end
= table
->_first
+ 32;
384 if (table
->_last
== table
->_end
)
386 int len
= table
->_end
- table
->_first
;
387 MSVCRT__onexit_t
*tmp
= MSVCRT_realloc(table
->_first
, 2 * len
* sizeof(void *));
390 WARN("failed to grow table.\n");
394 table
->_end
= table
->_first
+ 2 * len
;
395 table
->_last
= table
->_first
+ len
;
398 *table
->_last
= func
;
403 /*********************************************************************
404 * _execute_onexit_table (UCRTBASE.@)
406 int CDECL
MSVCRT__execute_onexit_table(MSVCRT__onexit_table_t
*table
)
408 MSVCRT__onexit_t
*func
;
410 TRACE("(%p)\n", table
);
415 if (!table
->_first
|| table
->_first
>= table
->_last
)
418 for (func
= table
->_last
- 1; func
>= table
->_first
; func
--)
424 MSVCRT_free(table
->_first
);
425 memset(table
, 0, sizeof(*table
));
426 MSVCRT__initialize_onexit_table(table
);
430 /*********************************************************************
431 * _set_purecall_handler (MSVCR71.@)
433 MSVCRT_purecall_handler CDECL
_set_purecall_handler(MSVCRT_purecall_handler function
)
435 MSVCRT_purecall_handler ret
= purecall_handler
;
437 TRACE("(%p)\n", function
);
438 purecall_handler
= function
;
442 /*********************************************************************
443 * _purecall (MSVCRT.@)
445 void CDECL
_purecall(void)
454 /******************************************************************************
455 * _set_error_mode (MSVCRT.@)
457 * Set the error mode, which describes where the C run-time writes error messages.
460 * mode - the new error mode
463 * The old error mode.
466 int CDECL
_set_error_mode(int mode
)
469 const int old
= MSVCRT_error_mode
;
470 if ( MSVCRT__REPORT_ERRMODE
!= mode
) {
471 MSVCRT_error_mode
= mode
;