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
);
64 extern int wxEntryCleanupReal(int& argc
, wxChar
**argv
);
66 // ============================================================================
67 // implementation: various entry points
68 // ============================================================================
72 // ----------------------------------------------------------------------------
73 // wrapper wxEntry catching all Win32 exceptions occurring in a wx program
74 // ----------------------------------------------------------------------------
76 // wrap real wxEntry in a try-except block to be able to call
77 // OnFatalException() if necessary
78 #if wxUSE_ON_FATAL_EXCEPTION
80 // global pointer to exception information, only valid inside OnFatalException,
81 // used by wxStackWalker and wxCrashReport
82 extern EXCEPTION_POINTERS
*wxGlobalSEInformation
= NULL
;
84 // flag telling us whether the application wants to handle exceptions at all
85 static bool gs_handleExceptions
= false;
87 static void wxFatalExit()
89 // use the same exit code as abort()
93 unsigned long wxGlobalSEHandler(EXCEPTION_POINTERS
*pExcPtrs
)
95 if ( gs_handleExceptions
&& wxTheApp
)
97 // store the pointer to exception info
98 wxGlobalSEInformation
= pExcPtrs
;
100 // give the user a chance to do something special about this
103 wxTheApp
->OnFatalException();
105 wxSEH_IGNORE
// ignore any exceptions inside the exception handler
107 wxGlobalSEInformation
= NULL
;
109 // this will execute our handler and terminate the process
110 return EXCEPTION_EXECUTE_HANDLER
;
113 return EXCEPTION_CONTINUE_SEARCH
;
118 void wxSETranslator(unsigned int WXUNUSED(code
), EXCEPTION_POINTERS
*ep
)
120 switch ( wxGlobalSEHandler(ep
) )
123 wxFAIL_MSG( wxT("unexpected wxGlobalSEHandler() return value") );
126 case EXCEPTION_EXECUTE_HANDLER
:
127 // if wxApp::OnFatalException() had been called we should exit the
128 // application -- but we shouldn't kill our host when we're a DLL
134 case EXCEPTION_CONTINUE_SEARCH
:
135 // we're called for each "catch ( ... )" and if we (re)throw from
136 // here, the catch handler body is not executed, so the effect is
137 // as if had inhibited translation of SE to C++ ones because the
138 // handler will never see any structured exceptions
143 #endif // __VISUALC__
145 bool wxHandleFatalExceptions(bool doit
)
147 // assume this can only be called from the main thread
148 gs_handleExceptions
= doit
;
150 #if wxUSE_CRASHREPORT
153 // try to find a place where we can put out report file later
154 wxChar fullname
[MAX_PATH
];
155 if ( !::GetTempPath(WXSIZEOF(fullname
), fullname
) )
157 wxLogLastError(wxT("GetTempPath"));
159 // when all else fails...
160 wxStrcpy(fullname
, wxT("c:\\"));
163 // use PID and date to make the report file name more unique
164 wxString name
= wxString::Format
166 wxT("%s_%s_%lu.dmp"),
167 wxTheApp
? (const wxChar
*)wxTheApp
->GetAppDisplayName().c_str()
169 wxDateTime::Now().Format(wxT("%Y%m%dT%H%M%S")).c_str(),
170 ::GetCurrentProcessId()
173 wxStrncat(fullname
, name
, WXSIZEOF(fullname
) - wxStrlen(fullname
) - 1);
175 wxCrashReport::SetFileName(fullname
);
177 #endif // wxUSE_CRASHREPORT
182 int wxEntry(int& argc
, wxChar
**argv
)
184 DisableAutomaticSETranslator();
188 return wxEntryReal(argc
, argv
);
193 #else // !wxUSE_ON_FATAL_EXCEPTION
195 int wxEntry(int& argc
, wxChar
**argv
)
197 return wxEntryReal(argc
, argv
);
200 #endif // wxUSE_ON_FATAL_EXCEPTION/!wxUSE_ON_FATAL_EXCEPTION
204 #if wxUSE_GUI && defined(__WXMSW__)
206 #if wxUSE_UNICODE && !defined(__WXWINCE__)
207 #define NEED_UNICODE_CHECK
210 #ifdef NEED_UNICODE_CHECK
212 // check whether Unicode is available
213 static bool wxIsUnicodeAvailable()
215 static const wchar_t *ERROR_STRING
= L
"wxWidgets Fatal Error";
217 if ( wxGetOsVersion() != wxOS_WINDOWS_NT
)
219 // we need to be built with MSLU support
220 #if !wxUSE_UNICODE_MSLU
221 // note that we can use MessageBoxW() as it's implemented even under
222 // Win9x - OTOH, we can't use wxGetTranslation() because the file APIs
223 // used by wxLocale are not
227 L
"This program uses Unicode and requires Windows NT/2000/XP.\n"
235 #else // wxUSE_UNICODE_MSLU
236 // and the MSLU DLL must also be available
237 HMODULE hmod
= ::LoadLibraryA("unicows.dll");
243 L
"This program uses Unicode and requires unicows.dll to work "
244 L
"under current operating system.\n"
246 L
"Please install unicows.dll and relaunch the program.",
253 // this is not really necessary but be tidy
256 // finally do the last check: has unicows.lib initialized correctly?
257 hmod
= ::LoadLibraryW(L
"unicows.dll");
263 L
"This program uses Unicode but is not using unicows.dll\n"
264 L
"correctly and so cannot work under current operating system.\n"
265 L
"Please contact the program author for an updated version.\n"
276 #endif // !wxUSE_UNICODE_MSLU
282 #endif // NEED_UNICODE_CHECK
284 // ----------------------------------------------------------------------------
285 // Windows-specific wxEntry
286 // ----------------------------------------------------------------------------
288 struct wxMSWCommandLineArguments
290 wxMSWCommandLineArguments() { argc
= 0; argv
= NULL
; }
292 void Init(const wxArrayString
& args
)
296 // +1 here for the terminating NULL
297 argv
= new wxChar
*[argc
+ 1];
298 for ( int i
= 0; i
< argc
; i
++ )
300 argv
[i
] = wxStrdup(args
[i
].wx_str());
303 // argv[] must be NULL-terminated
312 for ( int i
= 0; i
< argc
; i
++ )
326 static wxMSWCommandLineArguments wxArgs
;
328 // common part of wxMSW-specific wxEntryStart() and wxEntry() overloads
330 wxMSWEntryCommon(HINSTANCE hInstance
, int nCmdShow
)
332 // the first thing to do is to check if we're trying to run an Unicode
333 // program under Win9x w/o MSLU emulation layer - if so, abort right now
334 // as it has no chance to work and has all chances to crash
335 #ifdef NEED_UNICODE_CHECK
336 if ( !wxIsUnicodeAvailable() )
338 #endif // NEED_UNICODE_CHECK
341 // remember the parameters Windows gave us
342 wxSetInstance(hInstance
);
343 wxApp::m_nCmdShow
= nCmdShow
;
345 // parse the command line: we can't use pCmdLine in Unicode build so it is
346 // simpler to never use it at all (this also results in a more correct
349 // break the command line in words
352 const wxChar
*cmdLine
= ::GetCommandLine();
355 args
= wxCmdLineParser::ConvertStringToArgs(cmdLine
);
359 // WinCE doesn't insert the program itself, so do it ourselves.
360 args
.Insert(wxGetFullModuleName(), 0);
368 WXDLLEXPORT
bool wxEntryStart(HINSTANCE hInstance
,
369 HINSTANCE
WXUNUSED(hPrevInstance
),
370 wxCmdLineArgType
WXUNUSED(pCmdLine
),
373 if ( !wxMSWEntryCommon(hInstance
, nCmdShow
) )
376 return wxEntryStart(wxArgs
.argc
, wxArgs
.argv
);
379 WXDLLEXPORT
int wxEntry(HINSTANCE hInstance
,
380 HINSTANCE
WXUNUSED(hPrevInstance
),
381 wxCmdLineArgType
WXUNUSED(pCmdLine
),
384 if ( !wxMSWEntryCommon(hInstance
, nCmdShow
) )
387 wxON_BLOCK_EXIT_OBJ0(wxArgs
, wxMSWCommandLineArguments::Free
);
389 return wxEntry(wxArgs
.argc
, wxArgs
.argv
);
392 #endif // wxUSE_GUI && __WXMSW__
394 // ----------------------------------------------------------------------------
396 // ----------------------------------------------------------------------------
400 HINSTANCE wxhInstance
= 0;
402 extern "C" HINSTANCE
wxGetInstance()
407 void wxSetInstance(HINSTANCE hInst
)