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