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 MSW-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(wxEVT_IDLE
, wxIdleEventHandler(MyDllApp::OnIdle
));
152 Connect(CMD_SHOW_WINDOW
,
153 wxEVT_COMMAND_THREAD
,
154 wxThreadEventHandler(MyDllApp::OnShowWindow
));
155 Connect(CMD_TERMINATE
,
156 wxEVT_COMMAND_THREAD
,
157 wxThreadEventHandler(MyDllApp::OnTerminate
));
160 void MyDllApp::OnShowWindow(wxThreadEvent
& event
)
162 wxFrame
*f
= new MyDllFrame(NULL
, event
.GetString());
166 void MyDllApp::OnTerminate(wxThreadEvent
& WXUNUSED(event
))
172 // ----------------------------------------------------------------------------
173 // application startup
174 // ----------------------------------------------------------------------------
176 // we can't have WinMain() in a DLL and want to start the app ourselves
177 IMPLEMENT_APP_NO_MAIN(MyDllApp
)
182 // Critical section that guards everything related to wxWidgets "main" thread
183 // startup or shutdown.
184 wxCriticalSection gs_wxStartupCS
;
185 // Handle of wx "main" thread if running, NULL otherwise
186 HANDLE gs_wxMainThread
= NULL
;
189 // wx application startup code -- runs from its own thread
190 unsigned wxSTDCALL
MyAppLauncher(void* event
)
192 // Note: The thread that called run_wx_gui_from_dll() holds gs_wxStartupCS
193 // at this point and won't release it until we signal it.
195 // We need to pass correct HINSTANCE to wxEntry() and the right value is
196 // HINSTANCE of this DLL, not of the main .exe, use this MSW-specific wx
197 // function to get it. Notice that under Windows XP and later the name is
198 // not needed/used as we retrieve the DLL handle from an address inside it
199 // but you do need to use the correct name for this code to work with older
202 hInstance
= wxDynamicLibrary::MSWGetModuleHandle("my_dll",
205 return 0; // failed to get DLL's handle
207 // IMPLEMENT_WXWIN_MAIN does this as the first thing
208 wxDISABLE_DEBUG_SUPPORT();
210 // We do this before wxEntry() explicitly, even though wxEntry() would
211 // do it too, so that we know when wx is initialized and can signal
212 // run_wx_gui_from_dll() about it *before* starting the event loop.
213 wxInitializer wxinit
;
214 if ( !wxinit
.IsOk() )
215 return 0; // failed to init wx
217 // Signal run_wx_gui_from_dll() that it can continue
218 HANDLE hEvent
= *(static_cast<HANDLE
*>(event
));
219 if ( !SetEvent(hEvent
) )
220 return 0; // failed setting up the mutex
228 } // anonymous namespace
230 // ----------------------------------------------------------------------------
231 // public DLL interface
232 // ----------------------------------------------------------------------------
237 void run_wx_gui_from_dll(const char *title
)
239 // In order to prevent conflicts with hosting app's event loop, we
240 // launch wx app from the DLL in its own thread.
242 // We can't even use wxInitializer: it initializes wxModules and one of
243 // the modules it handles is wxThread's private module that remembers
244 // ID of the main thread. But we need to fool wxWidgets into thinking that
245 // the thread we are about to create now is the main thread, not the one
246 // from which this function is called.
248 // Note that we cannot use wxThread here, because the wx library wasn't
249 // initialized yet. wxCriticalSection is safe to use, though.
251 wxCriticalSectionLocker
lock(gs_wxStartupCS
);
253 if ( !gs_wxMainThread
)
255 HANDLE hEvent
= CreateEvent
257 NULL
, // default security attributes
259 FALSE
, // initially non-signaled
265 // NB: If your compiler doesn't have _beginthreadex(), use CreateThread()
266 gs_wxMainThread
= (HANDLE
)_beginthreadex
268 NULL
, // default security
269 0, // default stack size
271 &hEvent
, // arguments
276 if ( !gs_wxMainThread
)
282 // Wait until MyAppLauncher signals us that wx was initialized. This
283 // is because we use wxMessageQueue<> and wxString later and so must
284 // be sure that they are in working state.
285 WaitForSingleObject(hEvent
, INFINITE
);
289 // Send a message to wx thread to show a new frame:
290 wxThreadEvent
*event
=
291 new wxThreadEvent(wxEVT_COMMAND_THREAD
, CMD_SHOW_WINDOW
);
292 event
->SetString(title
);
293 wxQueueEvent(wxApp::GetInstance(), event
);
296 void wx_dll_cleanup()
298 wxCriticalSectionLocker
lock(gs_wxStartupCS
);
300 if ( !gs_wxMainThread
)
303 // If wx main thread is running, we need to stop it. To accomplish this,
304 // send a message telling it to terminate the app.
305 wxThreadEvent
*event
=
306 new wxThreadEvent(wxEVT_COMMAND_THREAD
, CMD_TERMINATE
);
307 wxQueueEvent(wxApp::GetInstance(), event
);
309 // We must then wait for the thread to actually terminate.
310 WaitForSingleObject(gs_wxMainThread
, INFINITE
);
311 CloseHandle(gs_wxMainThread
);
312 gs_wxMainThread
= NULL
;