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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "msvcrt/conio.h"
24 #include "msvcrt/stdlib.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt
);
33 #define LOCK_EXIT _mlock(_EXIT_LOCK1)
34 #define UNLOCK_EXIT _munlock(_EXIT_LOCK1)
36 static _onexit_t
*MSVCRT_atexit_table
= NULL
;
37 static int MSVCRT_atexit_table_size
= 0;
38 static int MSVCRT_atexit_registered
= 0; /* Points to free slot */
40 static LPCSTR szMsgBoxTitle
= "Wine C++ Runtime Library";
42 extern int MSVCRT_app_type
;
43 extern char *MSVCRT__pgmptr
;
45 /* INTERNAL: call atexit functions */
46 void __MSVCRT__call_atexit(void)
48 /* Note: should only be called with the exit lock held */
49 TRACE("%d atext functions to call\n", MSVCRT_atexit_registered
);
50 /* Last registered gets executed first */
51 while (MSVCRT_atexit_registered
> 0)
53 MSVCRT_atexit_registered
--;
54 TRACE("next is %p\n",MSVCRT_atexit_table
[MSVCRT_atexit_registered
]);
55 if (MSVCRT_atexit_table
[MSVCRT_atexit_registered
])
56 (*MSVCRT_atexit_table
[MSVCRT_atexit_registered
])();
61 /*********************************************************************
62 * __dllonexit (MSVCRT.@)
64 _onexit_t
__dllonexit(_onexit_t func
, _onexit_t
**start
, _onexit_t
**end
)
69 TRACE("(%p,%p,%p)\n", func
, start
, end
);
71 if (!start
|| !*start
|| !end
|| !*end
)
77 len
= (*end
- *start
);
79 TRACE("table start %p-%p, %d entries\n", *start
, *end
, len
);
84 tmp
= (_onexit_t
*)MSVCRT_realloc(*start
, len
* sizeof(tmp
));
90 TRACE("new table start %p-%p, %d entries\n", *start
, *end
, len
);
94 /*********************************************************************
97 void MSVCRT__exit(int exitcode
)
99 TRACE("(%d)\n", exitcode
);
100 ExitProcess(exitcode
);
103 /* Print out an error message with an option to debug */
104 static void DoMessageBox(LPCSTR lead
, LPCSTR message
)
106 MSGBOXPARAMSA msgbox
;
110 snprintf(text
,sizeof(text
),"%s\n\nProgram: %s\n%s\n\n"
111 "Press OK to exit the program, or Cancel to start the Wine debugger.\n ",
112 lead
, MSVCRT__pgmptr
, message
);
114 msgbox
.cbSize
= sizeof(msgbox
);
115 msgbox
.hwndOwner
= GetActiveWindow();
116 msgbox
.hInstance
= 0;
117 msgbox
.lpszText
= text
;
118 msgbox
.lpszCaption
= szMsgBoxTitle
;
119 msgbox
.dwStyle
= MB_OKCANCEL
|MB_ICONERROR
;
120 msgbox
.lpszIcon
= NULL
;
121 msgbox
.dwContextHelpId
= 0;
122 msgbox
.lpfnMsgBoxCallback
= NULL
;
123 msgbox
.dwLanguageId
= LANG_NEUTRAL
;
125 ret
= MessageBoxIndirectA(&msgbox
);
130 /*********************************************************************
131 * _amsg_exit (MSVCRT.@)
133 void MSVCRT__amsg_exit(int errnum
)
135 TRACE("(%d)\n", errnum
);
136 /* FIXME: text for the error number. */
137 if (MSVCRT_app_type
== 2)
140 sprintf(text
, "Error: R60%d",errnum
);
141 DoMessageBox("Runtime error!", text
);
144 _cprintf("\nruntime error R60%d\n",errnum
);
148 /*********************************************************************
151 void MSVCRT_abort(void)
154 if (MSVCRT_app_type
== 2)
156 DoMessageBox("Runtime error!", "abnormal program termination");
159 _cputs("\nabnormal program termination\n");
163 /*********************************************************************
166 void MSVCRT__assert(const char* str
, const char* file
, unsigned int line
)
168 TRACE("(%s,%s,%d)\n",str
,file
,line
);
169 if (MSVCRT_app_type
== 2)
172 snprintf(text
, sizeof(text
), "File: %s\nLine: %d\n\nEpression: \"%s\"", file
, line
, str
);
173 DoMessageBox("Assertion failed!", text
);
176 _cprintf("Assertion failed: %s, file %s, line %d\n\n",str
, file
, line
);
180 /*********************************************************************
183 void MSVCRT__c_exit(void)
186 /* All cleanup is done on DLL detach; Return to caller */
189 /*********************************************************************
192 void MSVCRT__cexit(void)
195 /* All cleanup is done on DLL detach; Return to caller */
198 /*********************************************************************
201 _onexit_t
_onexit(_onexit_t func
)
203 TRACE("(%p)\n",func
);
209 if (MSVCRT_atexit_registered
> MSVCRT_atexit_table_size
- 1)
212 TRACE("expanding table\n");
213 newtable
= MSVCRT_calloc(sizeof(void *),MSVCRT_atexit_table_size
+ 32);
220 memcpy (newtable
, MSVCRT_atexit_table
, MSVCRT_atexit_table_size
);
221 MSVCRT_atexit_table_size
+= 32;
222 if (MSVCRT_atexit_table
)
223 MSVCRT_free (MSVCRT_atexit_table
);
224 MSVCRT_atexit_table
= newtable
;
226 MSVCRT_atexit_table
[MSVCRT_atexit_registered
] = func
;
227 MSVCRT_atexit_registered
++;
232 /*********************************************************************
235 void MSVCRT_exit(int exitcode
)
237 TRACE("(%d)\n",exitcode
);
239 __MSVCRT__call_atexit();
241 ExitProcess(exitcode
);
244 /*********************************************************************
247 int MSVCRT_atexit(void (*func
)(void))
249 TRACE("(%p)\n", func
);
250 return _onexit((_onexit_t
)func
) == (_onexit_t
)func
? 0 : -1;
254 /*********************************************************************
255 * _purecall (MSVCRT.@)
260 MSVCRT__amsg_exit( 25 );