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