disable an apparently harmless VC++ warning about /EHa being required with _set_se_tr...
[wxWidgets.git] / src / msw / main.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: msw/main.cpp
3 // Purpose: WinMain/DllMain
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #include "wx/event.h"
32 #include "wx/app.h"
33 #include "wx/cmdline.h"
34
35 #include "wx/msw/private.h"
36
37 #if wxUSE_ON_FATAL_EXCEPTION
38 #include "wx/datetime.h"
39 #include "wx/msw/crashrpt.h"
40
41 #ifdef __VISUALC__
42 #include <eh.h>
43 #endif // __VISUALC__
44 #endif // wxUSE_ON_FATAL_EXCEPTION
45
46 #ifdef __WXWINCE__
47 // there is no ExitProcess() under CE but exiting the main thread has the
48 // same effect
49 #define ExitProcess ExitThread
50 #endif
51
52 #ifdef __BORLANDC__
53 // BC++ has to be special: its run-time expects the DLL entry point to be
54 // named DllEntryPoint instead of the (more) standard DllMain
55 #define DllMain DllEntryPoint
56 #endif
57
58 #if defined(__WXMICROWIN__)
59 #define HINSTANCE HANDLE
60 #endif
61
62 // ============================================================================
63 // implementation: various entry points
64 // ============================================================================
65
66 // ----------------------------------------------------------------------------
67 // wrapper wxEntry catching all Win32 exceptions occuring in a wx program
68 // ----------------------------------------------------------------------------
69
70 // wrap real wxEntry in a try-except block to be able to call
71 // OnFatalException() if necessary
72 #if wxUSE_ON_FATAL_EXCEPTION
73
74 // global pointer to exception information, only valid inside OnFatalException,
75 // used by wxStackWalker and wxCrashReport
76 extern EXCEPTION_POINTERS *wxGlobalSEInformation = NULL;
77
78 // flag telling us whether the application wants to handle exceptions at all
79 static bool gs_handleExceptions = false;
80
81 unsigned long wxGlobalSEHandler(EXCEPTION_POINTERS *pExcPtrs)
82 {
83 if ( gs_handleExceptions && wxTheApp )
84 {
85 // store the pointer to exception info
86 wxGlobalSEInformation = pExcPtrs;
87
88 // give the user a chance to do something special about this
89 __try
90 {
91 wxTheApp->OnFatalException();
92 }
93 __except ( EXCEPTION_EXECUTE_HANDLER )
94 {
95 // nothing to do here, just ignore the exception inside the
96 // exception handler
97 ;
98 }
99
100 wxGlobalSEInformation = NULL;
101
102 // this will execute our handler and terminate the process
103 return EXCEPTION_EXECUTE_HANDLER;
104 }
105
106 return EXCEPTION_CONTINUE_SEARCH;
107 }
108
109 #ifdef __VISUALC__
110
111 static void wxSETranslator(unsigned int WXUNUSED(code), EXCEPTION_POINTERS *ep)
112 {
113 wxGlobalSEHandler(ep);
114 }
115
116 #endif // __VISUALC__
117
118 bool wxHandleFatalExceptions(bool doit)
119 {
120 // assume this can only be called from the main thread
121 gs_handleExceptions = doit;
122
123 #ifdef __VISUALC__
124 // VC++ (at least from 4.0 up to version 7.1) is incredibly broken in that
125 // a "catch ( ... )" will *always* catch SEH exceptions in it even though
126 // it should have never been the case... to prevent such catches from
127 // stealing the exceptions from our wxGlobalSEHandler which is only called
128 // if the exception is not handled elsewhere, we have to also call it from
129 // a special SEH translator function which is called by VC CRT when a Win32
130 // exception occurs
131
132 // this warns that /EHa (async exceptions) should be used when using
133 // _set_se_translator but, in fact, this doesn't seem to change anything
134 // with VC++ up to 7.1 -- to be confirmed with VC++ 8
135 #if _MSC_VER <= 1310
136 #pragma warning(disable:4535)
137 #endif
138
139 _set_se_translator(doit ? wxSETranslator : NULL);
140 #endif
141
142 #if wxUSE_CRASHREPORT
143 if ( doit )
144 {
145 // try to find a place where we can put out report file later
146 wxChar fullname[MAX_PATH];
147 if ( !::GetTempPath(WXSIZEOF(fullname), fullname) )
148 {
149 wxLogLastError(_T("GetTempPath"));
150
151 // when all else fails...
152 wxStrcpy(fullname, _T("c:\\"));
153 }
154
155 // use PID and date to make the report file name more unique
156 wxString name = wxString::Format
157 (
158 _T("%s_%s_%lu.dmp"),
159 wxTheApp ? wxTheApp->GetAppName().c_str()
160 : _T("wxwindows"),
161 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str(),
162 ::GetCurrentProcessId()
163 );
164
165 wxStrncat(fullname, name, WXSIZEOF(fullname) - wxStrlen(fullname) - 1);
166
167 wxCrashReport::SetFileName(fullname);
168 }
169 #endif // wxUSE_CRASHREPORT
170
171 return true;
172 }
173
174 int wxEntry(int& argc, wxChar **argv)
175 {
176 __try
177 {
178 extern int wxEntryReal(int& argc, wxChar **argv);
179
180 return wxEntryReal(argc, argv);
181 }
182 __except ( wxGlobalSEHandler(GetExceptionInformation()) )
183 {
184 ::ExitProcess(3); // the same exit code as abort()
185
186 #if !defined(_MSC_VER) || _MSC_VER < 1300
187 // this code is unreachable but put it here to suppress warnings
188 // from some compilers
189 return -1;
190 #endif
191 }
192 }
193
194 #endif // wxUSE_ON_FATAL_EXCEPTION
195
196 #if wxUSE_GUI
197
198 // ----------------------------------------------------------------------------
199 // Windows-specific wxEntry
200 // ----------------------------------------------------------------------------
201
202 WXDLLEXPORT int wxEntry(HINSTANCE hInstance,
203 HINSTANCE WXUNUSED(hPrevInstance),
204 wxCmdLineArgType WXUNUSED(pCmdLine),
205 int nCmdShow)
206 {
207 // remember the parameters Windows gave us
208 wxSetInstance(hInstance);
209 wxApp::m_nCmdShow = nCmdShow;
210
211 // parse the command line: we can't use pCmdLine in Unicode build so it is
212 // simpler to never use it at all (this also results in a more correct
213 // argv[0])
214
215 // break the command line in words
216 wxArrayString args;
217
218 const wxChar *cmdLine = ::GetCommandLine();
219 if ( cmdLine )
220 {
221 args = wxCmdLineParser::ConvertStringToArgs(cmdLine);
222 }
223
224 #ifdef __WXWINCE__
225 // WinCE doesn't insert the program itself, so do it ourselves.
226 args.Insert(wxGetFullModuleName(), 0);
227 #endif
228
229 int argc = args.GetCount();
230
231 // +1 here for the terminating NULL
232 wxChar **argv = new wxChar *[argc + 1];
233 for ( int i = 0; i < argc; i++ )
234 {
235 argv[i] = wxStrdup(args[i]);
236 }
237
238 // argv[] must be NULL-terminated
239 argv[argc] = NULL;
240
241 return wxEntry(argc, argv);
242 }
243
244 // May wish not to have a DllMain or WinMain, e.g. if we're programming
245 // a Netscape plugin or if we're writing a console application
246 #if !defined(NOMAIN)
247
248 extern "C"
249 {
250
251 // ----------------------------------------------------------------------------
252 // WinMain
253 // ----------------------------------------------------------------------------
254
255 // Note that WinMain is also defined in dummy.obj, which is linked to
256 // an application that is using the DLL version of wxWidgets.
257
258 #if defined(_WINDLL)
259
260 // DLL entry point
261
262 BOOL WINAPI
263 DllMain(HANDLE hModule, DWORD fdwReason, LPVOID WXUNUSED(lpReserved))
264 {
265 // Only call wxEntry if the application itself is part of the DLL.
266 // If only the wxWidgets library is in the DLL, then the
267 // initialisation will be called when the application implicitly
268 // calls WinMain.
269 #ifndef WXMAKINGDLL
270 switch (fdwReason)
271 {
272 case DLL_PROCESS_ATTACH:
273 return wxEntry(hModule);
274
275 case DLL_PROCESS_DETACH:
276 wxEntryCleanup();
277 break;
278 }
279 #else
280 (void)hModule;
281 (void)fdwReason;
282 #endif // !WXMAKINGDLL
283
284 return TRUE;
285 }
286
287 #endif // _WINDLL
288
289 } // extern "C"
290
291 #endif // !NOMAIN
292
293 #endif // wxUSE_GUI
294
295 // ----------------------------------------------------------------------------
296 // global HINSTANCE
297 // ----------------------------------------------------------------------------
298
299 #if wxUSE_BASE
300
301 HINSTANCE wxhInstance = 0;
302
303 extern "C" HINSTANCE wxGetInstance()
304 {
305 return wxhInstance;
306 }
307
308 void wxSetInstance(HINSTANCE hInst)
309 {
310 wxhInstance = hInst;
311 }
312
313 #endif // wxUSE_BASE
314