X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/61aba4609aa384e8906e76715ea483cbf46c6725..5568067a3d151caf17d01de9192ca4471decba97:/samples/dll/my_dll.cpp diff --git a/samples/dll/my_dll.cpp b/samples/dll/my_dll.cpp index 8b8dfd003a..bfd6ed508f 100644 --- a/samples/dll/my_dll.cpp +++ b/samples/dll/my_dll.cpp @@ -27,6 +27,7 @@ #endif #include "wx/app.h" +#include "wx/dynlib.h" #include "wx/frame.h" #include "wx/panel.h" #include "wx/sizer.h" @@ -149,10 +150,10 @@ MyDllApp::MyDllApp() Connect(wxEVT_IDLE, wxIdleEventHandler(MyDllApp::OnIdle)); Connect(CMD_SHOW_WINDOW, - wxEVT_COMMAND_THREAD, + wxEVT_THREAD, wxThreadEventHandler(MyDllApp::OnShowWindow)); Connect(CMD_TERMINATE, - wxEVT_COMMAND_THREAD, + wxEVT_THREAD, wxThreadEventHandler(MyDllApp::OnTerminate)); } @@ -186,22 +187,21 @@ HANDLE gs_wxMainThread = NULL; // wx application startup code -- runs from its own thread -__stdcall unsigned MyAppLauncher(void* event) +unsigned wxSTDCALL MyAppLauncher(void* event) { // Note: The thread that called run_wx_gui_from_dll() holds gs_wxStartupCS // at this point and won't release it until we signal it. // We need to pass correct HINSTANCE to wxEntry() and the right value is - // HINSTANCE of this DLL, not of the main .exe. - // - // This method of obtaining DLL's instance handle requires at least - // Windows XP/2003. We could also implement DllMain() and remember it from - // there, that would work on older systems too. - HINSTANCE hInstance; - int ret = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS, - (LPCTSTR)&MyAppLauncher, - &hInstance); - if ( ret == 0 ) + // HINSTANCE of this DLL, not of the main .exe, use this MSW-specific wx + // function to get it. Notice that under Windows XP and later the name is + // not needed/used as we retrieve the DLL handle from an address inside it + // but you do need to use the correct name for this code to work with older + // systems as well. + const HINSTANCE + hInstance = wxDynamicLibrary::MSWGetModuleHandle("my_dll", + &gs_wxMainThread); + if ( !hInstance ) return 0; // failed to get DLL's handle // IMPLEMENT_WXWIN_MAIN does this as the first thing @@ -231,7 +231,9 @@ __stdcall unsigned MyAppLauncher(void* event) // public DLL interface // ---------------------------------------------------------------------------- -extern "C" WXEXPORT +extern "C" +{ + void run_wx_gui_from_dll(const char *title) { // In order to prevent conflicts with hosting app's event loop, we @@ -286,14 +288,12 @@ void run_wx_gui_from_dll(const char *title) // Send a message to wx thread to show a new frame: wxThreadEvent *event = - new wxThreadEvent(wxEVT_COMMAND_THREAD, CMD_SHOW_WINDOW); + new wxThreadEvent(wxEVT_THREAD, CMD_SHOW_WINDOW); event->SetString(title); wxQueueEvent(wxApp::GetInstance(), event); } - -extern "C" WXEXPORT -void wx_dll_cleanup(void) +void wx_dll_cleanup() { wxCriticalSectionLocker lock(gs_wxStartupCS); @@ -303,7 +303,7 @@ void wx_dll_cleanup(void) // If wx main thread is running, we need to stop it. To accomplish this, // send a message telling it to terminate the app. wxThreadEvent *event = - new wxThreadEvent(wxEVT_COMMAND_THREAD, CMD_TERMINATE); + new wxThreadEvent(wxEVT_THREAD, CMD_TERMINATE); wxQueueEvent(wxApp::GetInstance(), event); // We must then wait for the thread to actually terminate. @@ -311,3 +311,5 @@ void wx_dll_cleanup(void) CloseHandle(gs_wxMainThread); gs_wxMainThread = NULL; } + +} // extern "C"