1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/main.cpp
3 // Purpose: WinMain/DllMain
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
33 #include "wx/cmdline.h"
34 #include "wx/scopeguard.h"
36 #include "wx/msw/private.h"
37 #include "wx/msw/seh.h"
39 #if wxUSE_ON_FATAL_EXCEPTION
40 #include "wx/datetime.h"
41 #include "wx/msw/crashrpt.h"
42 #endif // wxUSE_ON_FATAL_EXCEPTION
45 // there is no ExitProcess() under CE but exiting the main thread has the
48 #define ExitProcess ExitThread
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 // __BORLANDC__
58 #if defined(__WXMICROWIN__)
59 #define HINSTANCE HANDLE
62 // defined in common/init.cpp
63 extern int wxEntryReal(int& argc
, wxChar
**argv
);
65 // ============================================================================
66 // implementation: various entry points
67 // ============================================================================
71 #if wxUSE_ON_FATAL_EXCEPTION && defined(__VISUALC__) && !defined(__WXWINCE__)
72 // VC++ (at least from 4.0 up to version 7.1) is incredibly broken in that
73 // a "catch ( ... )" will *always* catch SEH exceptions in it even though
74 // it should have never been the case... to prevent such catches from
75 // stealing the exceptions from our wxGlobalSEHandler which is only called
76 // if the exception is not handled elsewhere, we have to also call it from
77 // a special SEH translator function which is called by VC CRT when a Win32
80 // this warns that /EHa (async exceptions) should be used when using
81 // _set_se_translator but, in fact, this doesn't seem to change anything
82 // with VC++ up to 8.0
84 #pragma warning(disable:4535)
87 // note that the SE translator must be called wxSETranslator!
88 #define DisableAutomaticSETranslator() _set_se_translator(wxSETranslator)
90 #define DisableAutomaticSETranslator()
91 #endif // __VISUALC__/!__VISUALC__
93 // ----------------------------------------------------------------------------
94 // wrapper wxEntry catching all Win32 exceptions occurring in a wx program
95 // ----------------------------------------------------------------------------
97 // wrap real wxEntry in a try-except block to be able to call
98 // OnFatalException() if necessary
99 #if wxUSE_ON_FATAL_EXCEPTION
101 // global pointer to exception information, only valid inside OnFatalException,
102 // used by wxStackWalker and wxCrashReport
103 extern EXCEPTION_POINTERS
*wxGlobalSEInformation
= NULL
;
105 // flag telling us whether the application wants to handle exceptions at all
106 static bool gs_handleExceptions
= false;
108 static void wxFatalExit()
110 // use the same exit code as abort()
114 unsigned long wxGlobalSEHandler(EXCEPTION_POINTERS
*pExcPtrs
)
116 if ( gs_handleExceptions
&& wxTheApp
)
118 // store the pointer to exception info
119 wxGlobalSEInformation
= pExcPtrs
;
121 // give the user a chance to do something special about this
124 wxTheApp
->OnFatalException();
126 wxSEH_IGNORE
// ignore any exceptions inside the exception handler
128 wxGlobalSEInformation
= NULL
;
130 // this will execute our handler and terminate the process
131 return EXCEPTION_EXECUTE_HANDLER
;
134 return EXCEPTION_CONTINUE_SEARCH
;
139 void wxSETranslator(unsigned int WXUNUSED(code
), EXCEPTION_POINTERS
*ep
)
141 switch ( wxGlobalSEHandler(ep
) )
144 wxFAIL_MSG( _T("unexpected wxGlobalSEHandler() return value") );
147 case EXCEPTION_EXECUTE_HANDLER
:
148 // if wxApp::OnFatalException() had been called we should exit the
149 // application -- but we shouldn't kill our host when we're a DLL
155 case EXCEPTION_CONTINUE_SEARCH
:
156 // we're called for each "catch ( ... )" and if we (re)throw from
157 // here, the catch handler body is not executed, so the effect is
158 // as if had inhibited translation of SE to C++ ones because the
159 // handler will never see any structured exceptions
164 #endif // __VISUALC__
166 bool wxHandleFatalExceptions(bool doit
)
168 // assume this can only be called from the main thread
169 gs_handleExceptions
= doit
;
171 #if wxUSE_CRASHREPORT
174 // try to find a place where we can put out report file later
175 wxChar fullname
[MAX_PATH
];
176 if ( !::GetTempPath(WXSIZEOF(fullname
), fullname
) )
178 wxLogLastError(_T("GetTempPath"));
180 // when all else fails...
181 wxStrcpy(fullname
, _T("c:\\"));
184 // use PID and date to make the report file name more unique
185 wxString name
= wxString::Format
188 wxTheApp
? (const wxChar
*)wxTheApp
->GetAppName().c_str()
190 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str(),
191 ::GetCurrentProcessId()
194 wxStrncat(fullname
, name
, WXSIZEOF(fullname
) - wxStrlen(fullname
) - 1);
196 wxCrashReport::SetFileName(fullname
);
198 #endif // wxUSE_CRASHREPORT
203 int wxEntry(int& argc
, wxChar
**argv
)
205 DisableAutomaticSETranslator();
209 return wxEntryReal(argc
, argv
);
214 #else // !wxUSE_ON_FATAL_EXCEPTION
216 #if defined(__VISUALC__) && !defined(__WXWINCE__)
219 wxSETranslator(unsigned int WXUNUSED(code
), EXCEPTION_POINTERS
* WXUNUSED(ep
))
221 // see wxSETranslator() version for wxUSE_ON_FATAL_EXCEPTION above
225 #endif // __VISUALC__
227 int wxEntry(int& argc
, wxChar
**argv
)
229 DisableAutomaticSETranslator();
231 return wxEntryReal(argc
, argv
);
234 #endif // wxUSE_ON_FATAL_EXCEPTION/!wxUSE_ON_FATAL_EXCEPTION
238 #if wxUSE_GUI && defined(__WXMSW__)
240 #if wxUSE_UNICODE && !defined(__WXWINCE__)
241 #define NEED_UNICODE_CHECK
244 #ifdef NEED_UNICODE_CHECK
246 // check whether Unicode is available
247 static bool wxIsUnicodeAvailable()
249 static const wchar_t *ERROR_STRING
= L
"wxWidgets Fatal Error";
251 if ( wxGetOsVersion() != wxOS_WINDOWS_NT
)
253 // we need to be built with MSLU support
254 #if !wxUSE_UNICODE_MSLU
255 // note that we can use MessageBoxW() as it's implemented even under
256 // Win9x - OTOH, we can't use wxGetTranslation() because the file APIs
257 // used by wxLocale are not
261 L
"This program uses Unicode and requires Windows NT/2000/XP.\n"
269 #else // wxUSE_UNICODE_MSLU
270 // and the MSLU DLL must also be available
271 HMODULE hmod
= ::LoadLibraryA("unicows.dll");
277 L
"This program uses Unicode and requires unicows.dll to work "
278 L
"under current operating system.\n"
280 L
"Please install unicows.dll and relaunch the program.",
287 // this is not really necessary but be tidy
290 // finally do the last check: has unicows.lib initialized correctly?
291 hmod
= ::LoadLibraryW(L
"unicows.dll");
297 L
"This program uses Unicode but is not using unicows.dll\n"
298 L
"correctly and so cannot work under current operating system.\n"
299 L
"Please contact the program author for an updated version.\n"
310 #endif // !wxUSE_UNICODE_MSLU
316 #endif // NEED_UNICODE_CHECK
318 // ----------------------------------------------------------------------------
319 // Windows-specific wxEntry
320 // ----------------------------------------------------------------------------
322 // helper function used to clean up in wxEntry() just below
324 // notice that argv elements are supposed to be allocated using malloc() while
325 // argv array itself is allocated with new
326 static void wxFreeArgs(int argc
, wxChar
**argv
)
328 for ( int i
= 0; i
< argc
; i
++ )
336 WXDLLEXPORT
int wxEntry(HINSTANCE hInstance
,
337 HINSTANCE
WXUNUSED(hPrevInstance
),
338 wxCmdLineArgType
WXUNUSED(pCmdLine
),
341 // the first thing to do is to check if we're trying to run an Unicode
342 // program under Win9x w/o MSLU emulation layer - if so, abort right now
343 // as it has no chance to work and has all chances to crash
344 #ifdef NEED_UNICODE_CHECK
345 if ( !wxIsUnicodeAvailable() )
347 #endif // NEED_UNICODE_CHECK
350 // remember the parameters Windows gave us
351 wxSetInstance(hInstance
);
352 wxApp::m_nCmdShow
= nCmdShow
;
354 // parse the command line: we can't use pCmdLine in Unicode build so it is
355 // simpler to never use it at all (this also results in a more correct
358 // break the command line in words
361 const wxChar
*cmdLine
= ::GetCommandLine();
364 args
= wxCmdLineParser::ConvertStringToArgs(cmdLine
);
368 // WinCE doesn't insert the program itself, so do it ourselves.
369 args
.Insert(wxGetFullModuleName(), 0);
372 int argc
= args
.GetCount();
374 // +1 here for the terminating NULL
375 wxChar
**argv
= new wxChar
*[argc
+ 1];
376 for ( int i
= 0; i
< argc
; i
++ )
378 argv
[i
] = wxStrdup(args
[i
]);
381 // argv[] must be NULL-terminated
384 wxON_BLOCK_EXIT2(wxFreeArgs
, argc
, argv
);
386 return wxEntry(argc
, argv
);
389 #endif // wxUSE_GUI && __WXMSW__
391 // ----------------------------------------------------------------------------
393 // ----------------------------------------------------------------------------
397 HINSTANCE wxhInstance
= 0;
399 extern "C" HINSTANCE
wxGetInstance()
404 void wxSetInstance(HINSTANCE hInst
)