1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Sample showing how to use wx from a DLL
4 // Author: Vaclav Slavik
7 // Copyright: (c) 2009 Vaclav Slavik
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 #include "wx/wxprec.h"
26 #error "This sample is Windows-only"
30 #include "wx/dynlib.h"
34 #include "wx/stattext.h"
35 #include "wx/button.h"
36 #include "wx/thread.h"
37 #include "wx/msgdlg.h"
38 #include "wx/msw/wrapwin.h"
40 #include <process.h> // for _beginthreadex()
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 class MyDllFrame
: public wxFrame
51 MyDllFrame(wxWindow
*parent
, const wxString
& label
);
53 void OnAbout(wxCommandEvent
& event
);
59 static const int CMD_SHOW_WINDOW
= wxNewId();
60 static const int CMD_TERMINATE
= wxNewId();
62 class MyDllApp
: public wxApp
68 void OnShowWindow(wxThreadEvent
& event
);
69 void OnTerminate(wxThreadEvent
& event
);
73 // ============================================================================
75 // ============================================================================
77 // ----------------------------------------------------------------------------
79 // ----------------------------------------------------------------------------
81 BEGIN_EVENT_TABLE(MyDllFrame
, wxFrame
)
82 EVT_BUTTON(wxID_ABOUT
, MyDllFrame::OnAbout
)
85 MyDllFrame::MyDllFrame(wxWindow
*parent
, const wxString
& label
)
86 : wxFrame(parent
, wxID_ANY
, label
)
88 wxPanel
*p
= new wxPanel(this, wxID_ANY
);
89 wxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
99 "wxApp instance is %p, thread ID %ld",
101 wxApp::GetInstance(),
102 wxThread::GetCurrentId()
105 wxSizerFlags(1).Expand().Border(wxALL
, 10)
110 new wxButton(p
, wxID_ABOUT
, "Show info"),
111 wxSizerFlags(0).Right().Border(wxALL
, 10)
114 p
->SetSizerAndFit(sizer
);
116 wxSizer
*fsizer
= new wxBoxSizer(wxVERTICAL
);
117 fsizer
->Add(p
, wxSizerFlags(1).Expand());
118 SetSizerAndFit(fsizer
);
121 void MyDllFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
123 wxMessageBox("This window is running in its own thread,\n"
124 "using private wxWidgets instance compiled into the DLL.",
126 wxOK
| wxICON_INFORMATION
);
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
136 // Keep the wx "main" thread running even without windows. This greatly
137 // simplifies threads handling, because we don't have to correctly
138 // implement wx-thread restarting.
140 // Note that this only works if you don't explicitly call ExitMainLoop(),
141 // except in reaction to wx_dll_cleanup()'s message. wx_dll_cleanup()
142 // relies on the availability of wxApp instance and if the event loop
143 // terminated, wxEntry() would return and wxApp instance would be
146 // Also note that this is efficient, because if there are no windows, the
147 // thread will sleep waiting for a new event. We could safe some memory
148 // by shutting the thread down when it's no longer needed, though.
149 SetExitOnFrameDelete(false);
151 Connect(CMD_SHOW_WINDOW
,
153 wxThreadEventHandler(MyDllApp::OnShowWindow
));
154 Connect(CMD_TERMINATE
,
156 wxThreadEventHandler(MyDllApp::OnTerminate
));
159 void MyDllApp::OnShowWindow(wxThreadEvent
& event
)
161 wxFrame
*f
= new MyDllFrame(NULL
, event
.GetString());
165 void MyDllApp::OnTerminate(wxThreadEvent
& WXUNUSED(event
))
171 // ----------------------------------------------------------------------------
172 // application startup
173 // ----------------------------------------------------------------------------
175 // we can't have WinMain() in a DLL and want to start the app ourselves
176 IMPLEMENT_APP_NO_MAIN(MyDllApp
)
181 // Critical section that guards everything related to wxWidgets "main" thread
182 // startup or shutdown.
183 wxCriticalSection gs_wxStartupCS
;
184 // Handle of wx "main" thread if running, NULL otherwise
185 HANDLE gs_wxMainThread
= NULL
;
188 // wx application startup code -- runs from its own thread
189 unsigned wxSTDCALL
MyAppLauncher(void* event
)
191 // Note: The thread that called run_wx_gui_from_dll() holds gs_wxStartupCS
192 // at this point and won't release it until we signal it.
194 // We need to pass correct HINSTANCE to wxEntry() and the right value is
195 // HINSTANCE of this DLL, not of the main .exe, use this MSW-specific wx
196 // function to get it. Notice that under Windows XP and later the name is
197 // not needed/used as we retrieve the DLL handle from an address inside it
198 // but you do need to use the correct name for this code to work with older
201 hInstance
= wxDynamicLibrary::MSWGetModuleHandle("my_dll",
204 return 0; // failed to get DLL's handle
206 // IMPLEMENT_WXWIN_MAIN does this as the first thing
207 wxDISABLE_DEBUG_SUPPORT();
209 // We do this before wxEntry() explicitly, even though wxEntry() would
210 // do it too, so that we know when wx is initialized and can signal
211 // run_wx_gui_from_dll() about it *before* starting the event loop.
212 wxInitializer wxinit
;
213 if ( !wxinit
.IsOk() )
214 return 0; // failed to init wx
216 // Signal run_wx_gui_from_dll() that it can continue
217 HANDLE hEvent
= *(static_cast<HANDLE
*>(event
));
218 if ( !SetEvent(hEvent
) )
219 return 0; // failed setting up the mutex
227 } // anonymous namespace
229 // ----------------------------------------------------------------------------
230 // public DLL interface
231 // ----------------------------------------------------------------------------
236 void run_wx_gui_from_dll(const char *title
)
238 // In order to prevent conflicts with hosting app's event loop, we
239 // launch wx app from the DLL in its own thread.
241 // We can't even use wxInitializer: it initializes wxModules and one of
242 // the modules it handles is wxThread's private module that remembers
243 // ID of the main thread. But we need to fool wxWidgets into thinking that
244 // the thread we are about to create now is the main thread, not the one
245 // from which this function is called.
247 // Note that we cannot use wxThread here, because the wx library wasn't
248 // initialized yet. wxCriticalSection is safe to use, though.
250 wxCriticalSectionLocker
lock(gs_wxStartupCS
);
252 if ( !gs_wxMainThread
)
254 HANDLE hEvent
= CreateEvent
256 NULL
, // default security attributes
258 FALSE
, // initially non-signaled
264 // NB: If your compiler doesn't have _beginthreadex(), use CreateThread()
265 gs_wxMainThread
= (HANDLE
)_beginthreadex
267 NULL
, // default security
268 0, // default stack size
270 &hEvent
, // arguments
275 if ( !gs_wxMainThread
)
281 // Wait until MyAppLauncher signals us that wx was initialized. This
282 // is because we use wxMessageQueue<> and wxString later and so must
283 // be sure that they are in working state.
284 WaitForSingleObject(hEvent
, INFINITE
);
288 // Send a message to wx thread to show a new frame:
289 wxThreadEvent
*event
=
290 new wxThreadEvent(wxEVT_THREAD
, CMD_SHOW_WINDOW
);
291 event
->SetString(title
);
292 wxQueueEvent(wxApp::GetInstance(), event
);
295 void wx_dll_cleanup()
297 wxCriticalSectionLocker
lock(gs_wxStartupCS
);
299 if ( !gs_wxMainThread
)
302 // If wx main thread is running, we need to stop it. To accomplish this,
303 // send a message telling it to terminate the app.
304 wxThreadEvent
*event
=
305 new wxThreadEvent(wxEVT_THREAD
, CMD_TERMINATE
);
306 wxQueueEvent(wxApp::GetInstance(), event
);
308 // We must then wait for the thread to actually terminate.
309 WaitForSingleObject(gs_wxMainThread
, INFINITE
);
310 CloseHandle(gs_wxMainThread
);
311 gs_wxMainThread
= NULL
;