The dll works as follows:
1. It initializes the wxwidgets in a separate thread which keeps running until the dialog is closed.
Code: Select all
CODE: SELECT ALL
static const int CMD_SHOW_WINDOW = wxNewId();
class MyDllApp : public wxApp
{
public:
MyDllApp();
private:
void OnShowWindow(wxThreadEvent& event);
};
void MyDllApp::OnShowWindow(wxThreadEvent& event)
{
myDialog dlg(_( "Show sample dialog" ));
dlg.ShowModal();
}
IMPLEMENT_APP_NO_MAIN(MyDllApp);
// wx application startup code -- runs from its own thread
unsigned wxSTDCALL MyAppLauncher(void* event)
{
#ifdef WIN32
const HINSTANCE
hInstance = wxDynamicLibrary::MSWGetModuleHandle("my_dll-1 or my_dll-2",
&gs_wxMainThread);
if ( !hInstance )
return 0; // failed to get DLL's handle
// wxIMPLEMENT_WXWIN_MAIN does this as the first thing
wxDISABLE_DEBUG_SUPPORT();
// We do this before wxEntry() explicitly, even though wxEntry() would
// do it too, so that we know when wx is initialized and can signal
// run_wx_gui_from_dll() about it *before* starting the event loop.
wxInitializer wxinit;
if ( !wxinit.IsOk() )
return 0; // failed to init wx
// Signal run_wx_gui_from_dll() that it can continue
HANDLE hEvent = *(static_cast<HANDLE*>(event));
if ( !SetEvent(hEvent) )
return 0; // failed setting up the mutex
// Run the app:
wxEntry(hInstance);
#endif
return 1;
}
Code: Select all
void MyDllApp::OnShowWindow(wxThreadEvent& event)
{
CORELOG_PUTSTR_D("OnShowWindow - Function Start");
PromptCallback(); ==> this creates the wxwidgets dialog
CORELOG_PUTSTR_D("OnShowWindow - Function Start");
}
extern "C" WXEXPORT void ShowwxDialog()
{
wxCriticalSectionLocker lock(gs_wxStartupCS);
gs_wxMainThread = (HANDLE)_beginthreadex
(
NULL, // default security
0, // default stack size
&MyAppLauncher,
&hEvent, // arguments
0, // create running
NULL
);
// Wait until MyAppLauncher signals us that wx was initialized. This
// is because we use wxMessageQueue<> and wxString later and so must
// be sure that they are in working state.
//Note: We are using pthread_create and pthread_signal_wait etc on Linux/MacOS
WaitForSingleObject(hEvent, INFINITE);
CloseHandle(hEvent);
// Send a message to wx thread to show a new frame:
wxThreadEvent *event =
new wxThreadEvent(wxEVT_THREAD, CMD_SHOW_WINDOW);
wxQueueEvent(wxApp::GetInstance(), event);
....
}
Code: Select all
void MyDllApp::OnTerminate(wxThreadEvent& WXUNUSED(event))
{
ExitMainLoop();
}
void wx_dll_cleanup()
{
void *status = NULL;
int rsts = CSF_STS_OK;
#ifdef WIN32
DWORD exitCode = CSF_STS_OK;
#endif
wxCriticalSectionLocker lock(gs_wxUserAuthStartupCS);
#if defined(WIN32)
if ( !gs_wxMainThread ) {
goto clean_exit;
}
#endif
/* 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_THREAD, CMD_TERMINATE);
wxQueueEvent(wxApp::GetInstance(), event);
#if defined(WIN32)
/* We must then wait for the thread to actually terminate. */
WaitForSingleObject(gs_wxMainThread, INFINITE);
GetExitCodeThread (gs_wxMainThread, &exitCode);
CloseHandle(gs_wxMainThread);
gs_wxMainThread = NULL;
#endif
}
These dlls (dll-1 and dll-2) are loaded from some thread party application and same code is there is both the dlls but they show different dialogs.
The dlls are loaded once per process and keep running until the third party application closes or terminates.
It works fine:
1. If dll-1 is loaded to show the dialog multiple times without closing the third party applicaiton, it shows properly any number of times.
2. If dll-2 is loaded to show the dialog multiple times without closing the third party applicaiton, it shows properly any number of times.
Issue occurs when following sequence of steps are performed:
1. Load dll-1 and show the dialog and close the dialog.
2. Load dll-2 and show the dialog and close the dialog.
3. When we try to show the dialog of dll-1 the event to show CMD_SHOW_WINDOW is not getting triggered and UI is not shown. However when dialog from dll-2 is called, it shows the UI properly.
Maybe when dll-2 is called the main thread is not getting cleaned up or all the events are going to only dll-2 once dll-2 is used?
Can you please help to fix this issue?
The issue is reported on Windows 10.