compilation fix
[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/init.h"
32 #include "wx/event.h"
33 #include "wx/app.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 int wxEntry(WXHINSTANCE hInstance,
73 WXHINSTANCE WXUNUSED(hPrevInstance),
74 char *pCmdLine,
75 int nCmdShow)
76 {
77 // remember the parameters Windows gave us
78 wxSetInstance((HINSTANCE)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((WXHINSTANCE) hInstance,
110 (WXHINSTANCE) hPrevInstance,
111 lpCmdLine,
112 nCmdShow);
113 }
114
115 #else // _WINDLL
116
117 // DLL entry point
118
119 BOOL WINAPI
120 DllMain(HANDLE hModule, DWORD fdwReason, LPVOID WXUNUSED(lpReserved))
121 {
122 // Only call wxEntry if the application itself is part of the DLL.
123 // If only the wxWindows library is in the DLL, then the
124 // initialisation will be called when the application implicitly
125 // calls WinMain.
126 #ifndef WXMAKINGDLL
127 switch (fdwReason)
128 {
129 case DLL_PROCESS_ATTACH:
130 return wxEntry((WXHINSTANCE) hModule);
131
132 case DLL_PROCESS_DETACH:
133 wxEntryCleanup();
134 break;
135 }
136 #else
137 (void)hModule;
138 (void)fdwReason;
139 #endif // !WXMAKINGDLL
140
141 return TRUE;
142 }
143
144 #endif // _WINDLL/!_WINDLL
145
146 } // extern "C"
147
148 #endif // !NOMAIN
149
150 // ---------------------------------------------------------------------------
151 // Convert Windows to argc, argv style
152 // ---------------------------------------------------------------------------
153
154 wxChar **ConvertToStandardCommandArgs(const wxChar *p, int& argc)
155 {
156 // break the command line in words
157 wxArrayString args;
158 if ( p )
159 {
160 args = wxCmdLineParser::ConvertStringToArgs(p);
161 }
162
163 // +1 here for the program name
164 argc = args.GetCount() + 1;
165
166 // and +1 here for the terminating NULL
167 wxChar **argv = new wxChar *[argc + 1];
168
169 argv[0] = new wxChar[MAX_PATH];
170 ::GetModuleFileName(wxhInstance, argv[0], MAX_PATH);
171
172 // copy all the other arguments to wxApp::argv[]
173 for ( int i = 1; i < argc; i++ )
174 {
175 argv[i] = wxStrdup(args[i - 1]);
176 }
177
178 // argv[] must be NULL-terminated
179 argv[argc] = NULL;
180
181 return argv;
182 }
183
184 #endif // wxUSE_GUI
185
186 // ----------------------------------------------------------------------------
187 // global HINSTANCE
188 // ----------------------------------------------------------------------------
189
190 #if wxUSE_BASE
191
192 HINSTANCE wxhInstance = 0;
193
194 HINSTANCE wxGetInstance()
195 {
196 return wxhInstance;
197 }
198
199 void wxSetInstance(HINSTANCE hInst)
200 {
201 wxhInstance = hInst;
202 }
203
204 #endif // wxUSE_BASE
205