1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: WinMain/DllMain
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
33 #include "wx/cmdline.h"
35 #include "wx/msw/private.h"
37 #if wxUSE_ON_FATAL_EXCEPTION
38 #include "wx/datetime.h"
39 #include "wx/msw/crashrpt.h"
44 #endif // wxUSE_ON_FATAL_EXCEPTION
47 // there is no ExitProcess() under CE but exiting the main thread has the
49 #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
58 #if defined(__WXMICROWIN__)
59 #define HINSTANCE HANDLE
62 // ============================================================================
63 // implementation: various entry points
64 // ============================================================================
66 // ----------------------------------------------------------------------------
67 // wrapper wxEntry catching all Win32 exceptions occuring in a wx program
68 // ----------------------------------------------------------------------------
70 // wrap real wxEntry in a try-except block to be able to call
71 // OnFatalException() if necessary
72 #if wxUSE_ON_FATAL_EXCEPTION
74 // global pointer to exception information, only valid inside OnFatalException,
75 // used by wxStackWalker and wxCrashReport
76 extern EXCEPTION_POINTERS
*wxGlobalSEInformation
= NULL
;
78 // flag telling us whether the application wants to handle exceptions at all
79 static bool gs_handleExceptions
= false;
81 unsigned long wxGlobalSEHandler(EXCEPTION_POINTERS
*pExcPtrs
)
83 if ( gs_handleExceptions
&& wxTheApp
)
85 // store the pointer to exception info
86 wxGlobalSEInformation
= pExcPtrs
;
88 // give the user a chance to do something special about this
91 wxTheApp
->OnFatalException();
93 __except ( EXCEPTION_EXECUTE_HANDLER
)
95 // nothing to do here, just ignore the exception inside the
100 wxGlobalSEInformation
= NULL
;
102 // this will execute our handler and terminate the process
103 return EXCEPTION_EXECUTE_HANDLER
;
106 return EXCEPTION_CONTINUE_SEARCH
;
111 static void wxSETranslator(unsigned int WXUNUSED(code
), EXCEPTION_POINTERS
*ep
)
113 wxGlobalSEHandler(ep
);
116 #endif // __VISUALC__
118 bool wxHandleFatalExceptions(bool doit
)
120 // assume this can only be called from the main thread
121 gs_handleExceptions
= doit
;
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
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
136 #pragma warning(disable:4535)
139 _set_se_translator(doit
? wxSETranslator
: NULL
);
142 #if wxUSE_CRASHREPORT
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
) )
149 wxLogLastError(_T("GetTempPath"));
151 // when all else fails...
152 wxStrcpy(fullname
, _T("c:\\"));
155 // use PID and date to make the report file name more unique
156 wxString name
= wxString::Format
159 wxTheApp
? wxTheApp
->GetAppName().c_str()
161 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str(),
162 ::GetCurrentProcessId()
165 wxStrncat(fullname
, name
, WXSIZEOF(fullname
) - wxStrlen(fullname
) - 1);
167 wxCrashReport::SetFileName(fullname
);
169 #endif // wxUSE_CRASHREPORT
174 int wxEntry(int& argc
, wxChar
**argv
)
178 extern int wxEntryReal(int& argc
, wxChar
**argv
);
180 return wxEntryReal(argc
, argv
);
182 __except ( wxGlobalSEHandler(GetExceptionInformation()) )
184 ::ExitProcess(3); // the same exit code as abort()
186 #if !defined(_MSC_VER) || _MSC_VER < 1300
187 // this code is unreachable but put it here to suppress warnings
188 // from some compilers
194 #endif // wxUSE_ON_FATAL_EXCEPTION
198 // ----------------------------------------------------------------------------
199 // Windows-specific wxEntry
200 // ----------------------------------------------------------------------------
202 WXDLLEXPORT
int wxEntry(HINSTANCE hInstance
,
203 HINSTANCE
WXUNUSED(hPrevInstance
),
204 wxCmdLineArgType
WXUNUSED(pCmdLine
),
207 // remember the parameters Windows gave us
208 wxSetInstance(hInstance
);
209 wxApp::m_nCmdShow
= nCmdShow
;
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
215 // break the command line in words
218 const wxChar
*cmdLine
= ::GetCommandLine();
221 args
= wxCmdLineParser::ConvertStringToArgs(cmdLine
);
225 // WinCE doesn't insert the program itself, so do it ourselves.
226 args
.Insert(wxGetFullModuleName(), 0);
229 int argc
= args
.GetCount();
231 // +1 here for the terminating NULL
232 wxChar
**argv
= new wxChar
*[argc
+ 1];
233 for ( int i
= 0; i
< argc
; i
++ )
235 argv
[i
] = wxStrdup(args
[i
]);
238 // argv[] must be NULL-terminated
241 return wxEntry(argc
, argv
);
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
251 // ----------------------------------------------------------------------------
253 // ----------------------------------------------------------------------------
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.
263 DllMain(HANDLE hModule
, DWORD fdwReason
, LPVOID
WXUNUSED(lpReserved
))
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
272 case DLL_PROCESS_ATTACH
:
273 return wxEntry(hModule
);
275 case DLL_PROCESS_DETACH
:
282 #endif // !WXMAKINGDLL
295 // ----------------------------------------------------------------------------
297 // ----------------------------------------------------------------------------
301 HINSTANCE wxhInstance
= 0;
303 extern "C" HINSTANCE
wxGetInstance()
308 void wxSetInstance(HINSTANCE hInst
)