Fix RegisterWindowMessage declaration and improve debug message.
[wine/dcerpc.git] / dlls / msvcrt / exit.c
blob259368e6d13040c076d9164f7c203bd7798d8129
1 /*
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
20 #include <stdio.h>
21 #include "msvcrt.h"
22 #include "mtdll.h"
23 #include "winuser.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28 /* MT */
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 */
36 static LPCSTR szMsgBoxTitle = "Wine C++ Runtime Library";
38 extern int MSVCRT_app_type;
39 extern char *MSVCRT__pgmptr;
41 void (*_aexit_rtn)(int) = MSVCRT__exit;
43 /* INTERNAL: call atexit functions */
44 void __MSVCRT__call_atexit(void)
46 /* Note: should only be called with the exit lock held */
47 TRACE("%d atext functions to call\n", MSVCRT_atexit_registered);
48 /* Last registered gets executed first */
49 while (MSVCRT_atexit_registered > 0)
51 MSVCRT_atexit_registered--;
52 TRACE("next is %p\n",MSVCRT_atexit_table[MSVCRT_atexit_registered]);
53 if (MSVCRT_atexit_table[MSVCRT_atexit_registered])
54 (*MSVCRT_atexit_table[MSVCRT_atexit_registered])();
55 TRACE("returned\n");
59 /*********************************************************************
60 * __dllonexit (MSVCRT.@)
62 MSVCRT__onexit_t __dllonexit(MSVCRT__onexit_t func, MSVCRT__onexit_t **start, MSVCRT__onexit_t **end)
64 MSVCRT__onexit_t *tmp;
65 int len;
67 TRACE("(%p,%p,%p)\n", func, start, end);
69 if (!start || !*start || !end || !*end)
71 FIXME("bad table\n");
72 return NULL;
75 len = (*end - *start);
77 TRACE("table start %p-%p, %d entries\n", *start, *end, len);
79 if (++len <= 0)
80 return NULL;
82 tmp = (MSVCRT__onexit_t *)MSVCRT_realloc(*start, len * sizeof(tmp));
83 if (!tmp)
84 return NULL;
85 *start = tmp;
86 *end = tmp + len;
87 tmp[len - 1] = func;
88 TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
89 return func;
92 /*********************************************************************
93 * _exit (MSVCRT.@)
95 void MSVCRT__exit(int exitcode)
97 TRACE("(%d)\n", exitcode);
98 ExitProcess(exitcode);
101 /* Print out an error message with an option to debug */
102 static void DoMessageBox(LPCSTR lead, LPCSTR message)
104 MSGBOXPARAMSA msgbox;
105 char text[2048];
106 INT ret;
108 snprintf(text,sizeof(text),"%s\n\nProgram: %s\n%s\n\n"
109 "Press OK to exit the program, or Cancel to start the Wine debugger.\n ",
110 lead, MSVCRT__pgmptr, message);
112 msgbox.cbSize = sizeof(msgbox);
113 msgbox.hwndOwner = GetActiveWindow();
114 msgbox.hInstance = 0;
115 msgbox.lpszText = text;
116 msgbox.lpszCaption = szMsgBoxTitle;
117 msgbox.dwStyle = MB_OKCANCEL|MB_ICONERROR;
118 msgbox.lpszIcon = NULL;
119 msgbox.dwContextHelpId = 0;
120 msgbox.lpfnMsgBoxCallback = NULL;
121 msgbox.dwLanguageId = LANG_NEUTRAL;
123 ret = MessageBoxIndirectA(&msgbox);
124 if (ret == IDCANCEL)
125 DebugBreak();
128 /*********************************************************************
129 * _amsg_exit (MSVCRT.@)
131 void _amsg_exit(int errnum)
133 TRACE("(%d)\n", errnum);
134 /* FIXME: text for the error number. */
135 if (MSVCRT_app_type == 2)
137 char text[32];
138 sprintf(text, "Error: R60%d",errnum);
139 DoMessageBox("Runtime error!", text);
141 else
142 _cprintf("\nruntime error R60%d\n",errnum);
143 _aexit_rtn(255);
146 /*********************************************************************
147 * abort (MSVCRT.@)
149 void MSVCRT_abort(void)
151 TRACE("()\n");
152 if (MSVCRT_app_type == 2)
154 DoMessageBox("Runtime error!", "abnormal program termination");
156 else
157 _cputs("\nabnormal program termination\n");
158 MSVCRT__exit(3);
161 /*********************************************************************
162 * _assert (MSVCRT.@)
164 void MSVCRT__assert(const char* str, const char* file, unsigned int line)
166 TRACE("(%s,%s,%d)\n",str,file,line);
167 if (MSVCRT_app_type == 2)
169 char text[2048];
170 snprintf(text, sizeof(text), "File: %s\nLine: %d\n\nEpression: \"%s\"", file, line, str);
171 DoMessageBox("Assertion failed!", text);
173 else
174 _cprintf("Assertion failed: %s, file %s, line %d\n\n",str, file, line);
175 MSVCRT__exit(3);
178 /*********************************************************************
179 * _c_exit (MSVCRT.@)
181 void MSVCRT__c_exit(void)
183 TRACE("(void)\n");
184 /* All cleanup is done on DLL detach; Return to caller */
187 /*********************************************************************
188 * _cexit (MSVCRT.@)
190 void MSVCRT__cexit(void)
192 TRACE("(void)\n");
193 /* All cleanup is done on DLL detach; Return to caller */
196 /*********************************************************************
197 * _onexit (MSVCRT.@)
199 MSVCRT__onexit_t MSVCRT__onexit(MSVCRT__onexit_t func)
201 TRACE("(%p)\n",func);
203 if (!func)
204 return NULL;
206 LOCK_EXIT;
207 if (MSVCRT_atexit_registered > MSVCRT_atexit_table_size - 1)
209 MSVCRT__onexit_t *newtable;
210 TRACE("expanding table\n");
211 newtable = MSVCRT_calloc(sizeof(void *),MSVCRT_atexit_table_size + 32);
212 if (!newtable)
214 TRACE("failed!\n");
215 UNLOCK_EXIT;
216 return NULL;
218 memcpy (newtable, MSVCRT_atexit_table, MSVCRT_atexit_table_size);
219 MSVCRT_atexit_table_size += 32;
220 if (MSVCRT_atexit_table)
221 MSVCRT_free (MSVCRT_atexit_table);
222 MSVCRT_atexit_table = newtable;
224 MSVCRT_atexit_table[MSVCRT_atexit_registered] = func;
225 MSVCRT_atexit_registered++;
226 UNLOCK_EXIT;
227 return func;
230 /*********************************************************************
231 * exit (MSVCRT.@)
233 void MSVCRT_exit(int exitcode)
235 TRACE("(%d)\n",exitcode);
236 LOCK_EXIT;
237 __MSVCRT__call_atexit();
238 UNLOCK_EXIT;
239 ExitProcess(exitcode);
242 /*********************************************************************
243 * atexit (MSVCRT.@)
245 int MSVCRT_atexit(void (*func)(void))
247 TRACE("(%p)\n", func);
248 return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
252 /*********************************************************************
253 * _purecall (MSVCRT.@)
255 void _purecall(void)
257 TRACE("(void)\n");
258 _amsg_exit( 25 );