MinGW compilation fixes.
[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 #ifdef __GNUG__
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/init.h"
34 #include "wx/cmdline.h"
35
36 #include "wx/msw/private.h"
37
38 // Don't implement WinMain if we're building an MFC/wxWindows hybrid app.
39 #if wxUSE_MFC && !defined(NOMAIN)
40 #define NOMAIN 1
41 #endif
42
43 #ifdef __BORLANDC__
44 // BC++ has to be special: its run-time expects the DLL entry point to be
45 // named DllEntryPoint instead of the (more) standard DllMain
46 #define DllMain DllEntryPoint
47 #endif
48
49 #if defined(__WXMICROWIN__)
50 #define HINSTANCE HANDLE
51 #endif
52
53 #if wxUSE_GUI
54
55 // ----------------------------------------------------------------------------
56 // function prototypes
57 // ----------------------------------------------------------------------------
58
59 // from src/msw/app.cpp
60 extern void WXDLLEXPORT wxEntryCleanup();
61
62 static wxChar **ConvertToStandardCommandArgs(const wxChar *p, int& argc);
63
64 // ============================================================================
65 // implementation: various entry points
66 // ============================================================================
67
68 // ----------------------------------------------------------------------------
69 // Windows-specific wxEntry
70 // ----------------------------------------------------------------------------
71
72 WXDLLEXPORT int wxEntry(HINSTANCE hInstance,
73 HINSTANCE WXUNUSED(hPrevInstance),
74 char *pCmdLine,
75 int nCmdShow)
76 {
77 // remember the parameters Windows gave us
78 wxSetInstance(hInstance);
79 wxApp::m_nCmdShow = nCmdShow;
80
81 // parse the command line
82 int argc;
83 wxChar **argv = ConvertToStandardCommandArgs(wxConvertMB2WX(pCmdLine), argc);
84
85 return wxEntry(argc, argv);
86 }
87
88 // May wish not to have a DllMain or WinMain, e.g. if we're programming
89 // a Netscape plugin or if we're writing a console application
90 #if !defined(NOMAIN)
91
92 extern "C"
93 {
94
95 // ----------------------------------------------------------------------------
96 // WinMain
97 // ----------------------------------------------------------------------------
98
99 // Note that WinMain is also defined in dummy.obj, which is linked to
100 // an application that is using the DLL version of wxWindows.
101
102 #if !defined(_WINDLL)
103
104 int PASCAL WinMain(HINSTANCE hInstance,
105 HINSTANCE hPrevInstance,
106 LPSTR lpCmdLine,
107 int nCmdShow)
108 {
109 return wxEntry(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
110 }
111
112 #else // _WINDLL
113
114 // DLL entry point
115
116 BOOL WINAPI
117 DllMain(HANDLE hModule, DWORD fdwReason, LPVOID WXUNUSED(lpReserved))
118 {
119 // Only call wxEntry if the application itself is part of the DLL.
120 // If only the wxWindows library is in the DLL, then the
121 // initialisation will be called when the application implicitly
122 // calls WinMain.
123 #ifndef WXMAKINGDLL
124 switch (fdwReason)
125 {
126 case DLL_PROCESS_ATTACH:
127 return wxEntry(hModule);
128
129 case DLL_PROCESS_DETACH:
130 wxEntryCleanup();
131 break;
132 }
133 #else
134 (void)hModule;
135 (void)fdwReason;
136 #endif // !WXMAKINGDLL
137
138 return TRUE;
139 }
140
141 #endif // _WINDLL/!_WINDLL
142
143 } // extern "C"
144
145 #endif // !NOMAIN
146
147 // ---------------------------------------------------------------------------
148 // Convert Windows to argc, argv style
149 // ---------------------------------------------------------------------------
150
151 wxChar **ConvertToStandardCommandArgs(const wxChar *p, int& argc)
152 {
153 // break the command line in words
154 wxArrayString args;
155 if ( p )
156 {
157 args = wxCmdLineParser::ConvertStringToArgs(p);
158 }
159
160 // +1 here for the program name
161 argc = args.GetCount() + 1;
162
163 // and +1 here for the terminating NULL
164 wxChar **argv = new wxChar *[argc + 1];
165
166 argv[0] = new wxChar[MAX_PATH];
167 ::GetModuleFileName(wxhInstance, argv[0], MAX_PATH);
168
169 // copy all the other arguments to wxApp::argv[]
170 for ( int i = 1; i < argc; i++ )
171 {
172 argv[i] = wxStrdup(args[i - 1]);
173 }
174
175 // argv[] must be NULL-terminated
176 argv[argc] = NULL;
177
178 return argv;
179 }
180
181 #endif // wxUSE_GUI
182
183 // ----------------------------------------------------------------------------
184 // global HINSTANCE
185 // ----------------------------------------------------------------------------
186
187 #if wxUSE_BASE
188
189 HINSTANCE wxhInstance = 0;
190
191 HINSTANCE wxGetInstance()
192 {
193 return wxhInstance;
194 }
195
196 void wxSetInstance(HINSTANCE hInst)
197 {
198 wxhInstance = hInst;
199 }
200
201 #endif // wxUSE_BASE
202