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
131 _set_se_translator(doit
? wxSETranslator
: NULL
);
134 #if wxUSE_CRASHREPORT
137 // try to find a place where we can put out report file later
138 wxChar fullname
[MAX_PATH
];
139 if ( !::GetTempPath(WXSIZEOF(fullname
), fullname
) )
141 wxLogLastError(_T("GetTempPath"));
143 // when all else fails...
144 wxStrcpy(fullname
, _T("c:\\"));
147 // use PID and date to make the report file name more unique
148 wxString name
= wxString::Format
151 wxTheApp
? wxTheApp
->GetAppName().c_str()
153 wxDateTime::Now().Format(_T("%Y%m%dT%H%M%S")).c_str(),
154 ::GetCurrentProcessId()
157 wxStrncat(fullname
, name
, WXSIZEOF(fullname
) - wxStrlen(fullname
) - 1);
159 wxCrashReport::SetFileName(fullname
);
161 #endif // wxUSE_CRASHREPORT
166 int wxEntry(int& argc
, wxChar
**argv
)
170 extern int wxEntryReal(int& argc
, wxChar
**argv
);
172 return wxEntryReal(argc
, argv
);
174 __except ( wxGlobalSEHandler(GetExceptionInformation()) )
176 ::ExitProcess(3); // the same exit code as abort()
178 #if !defined(_MSC_VER) || _MSC_VER < 1300
179 // this code is unreachable but put it here to suppress warnings
180 // from some compilers
186 #endif // wxUSE_ON_FATAL_EXCEPTION
190 // ----------------------------------------------------------------------------
191 // Windows-specific wxEntry
192 // ----------------------------------------------------------------------------
194 WXDLLEXPORT
int wxEntry(HINSTANCE hInstance
,
195 HINSTANCE
WXUNUSED(hPrevInstance
),
196 wxCmdLineArgType
WXUNUSED(pCmdLine
),
199 // remember the parameters Windows gave us
200 wxSetInstance(hInstance
);
201 wxApp::m_nCmdShow
= nCmdShow
;
203 // parse the command line: we can't use pCmdLine in Unicode build so it is
204 // simpler to never use it at all (this also results in a more correct
207 // break the command line in words
210 const wxChar
*cmdLine
= ::GetCommandLine();
213 args
= wxCmdLineParser::ConvertStringToArgs(cmdLine
);
217 // WinCE doesn't insert the program itself, so do it ourselves.
218 args
.Insert(wxGetFullModuleName(), 0);
221 int argc
= args
.GetCount();
223 // +1 here for the terminating NULL
224 wxChar
**argv
= new wxChar
*[argc
+ 1];
225 for ( int i
= 0; i
< argc
; i
++ )
227 argv
[i
] = wxStrdup(args
[i
]);
230 // argv[] must be NULL-terminated
233 return wxEntry(argc
, argv
);
236 // May wish not to have a DllMain or WinMain, e.g. if we're programming
237 // a Netscape plugin or if we're writing a console application
243 // ----------------------------------------------------------------------------
245 // ----------------------------------------------------------------------------
247 // Note that WinMain is also defined in dummy.obj, which is linked to
248 // an application that is using the DLL version of wxWidgets.
255 DllMain(HANDLE hModule
, DWORD fdwReason
, LPVOID
WXUNUSED(lpReserved
))
257 // Only call wxEntry if the application itself is part of the DLL.
258 // If only the wxWidgets library is in the DLL, then the
259 // initialisation will be called when the application implicitly
264 case DLL_PROCESS_ATTACH
:
265 return wxEntry(hModule
);
267 case DLL_PROCESS_DETACH
:
274 #endif // !WXMAKINGDLL
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
293 HINSTANCE wxhInstance
= 0;
295 extern "C" HINSTANCE
wxGetInstance()
300 void wxSetInstance(HINSTANCE hInst
)