I wrote some small application and dll using wxWidgets 2.6.3.
The main application creates main window, dynamically loads the dll library and calls it’s functions. One of the dll’s function creates button on main application window. After that it try to connect event handler of this button to some function. For now - all god. But when I click on this button nothing appears – connected function doesn’t calls! Why?
dll.h
Code: Select all
#include <wx/wx.h>
#ifdef __WXMSW__
#ifndef _EXPORT_
#define _EXPORT_ extern "C" __declspec(dllexport)
#endif
#endif
/* Exported functions */
_EXPORT_ wxButton* CreateButton(wxWindow* parent, wxWindowID id, wxString& label, wxPoint& pos, wxSize& size);
/* Not exported */
HANDLE ThreadId;
class DLLApp : public wxApp
{
public:
bool OnInit();
void ButtonClick(wxEvent& event);
};
Code: Select all
#include <wx/wx.h>
#include "dll.h"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
IMPLEMENT_APP_NO_MAIN(DLLApp)
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
wxApp::SetInstance(new DLLApp());
wxEntry(GetModuleHandle(NULL), NULL, NULL, SW_SHOW);
return true;
}
bool APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
ThreadId = CreateThread(NULL, 0, ThreadProc, NULL, 0, NULL);
break;
case DLL_THREAD_ATTACH: break;
case DLL_THREAD_DETACH: break;
case DLL_PROCESS_DETACH:
wxEntryCleanup();
break;
}
return TRUE;
}
/* Exporting functions */
wxButton* CreateButton(wxWindow* parent, wxWindowID id, wxString& label, wxPoint& pos, wxSize& size)
{
wxButton* btn = new wxButton(parent, id, label, pos, size);
//This connects WDEvtFunc::ButtonClick to the new button EventHandler, but it doesn't work
btn->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(DLLApp::ButtonClick));
return btn;
}
/* Not exported functions */
bool DLLApp::OnInit()
{
return true;
}
void DLLApp::ButtonClick(wxEvent& WXUNUSED(event))
{
wxMessageBox("I'm here!", "Confirm", wxOK);
}
Code: Select all
#include <wx/dynlib.h>
#include <wx/wx.h>
enum
{
MAINFRAMEID = 1,
MAINPANELID,
CBUTTONID,
LBUTTONID,
NBUTTONID,
};
typedef wxButton* (*CreateFuncPtr)(wxWindow*, wxWindowID, wxString&, wxPoint&, wxSize&);
class WDPanel : public wxPanel
{
public:
WDPanel(wxWindow* parent, wxWindowID id = -1, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxTAB_TRAVERSAL, const wxString& name = "panel");
wxButton *LButton;
wxButton *CButton;
};
class WDTopWindow : public wxFrame
{
public:
WDTopWindow(wxWindowID id, const wxString& title, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_FRAME_STYLE, const wxString& name = "frame");
WDPanel *MainPanel;
};
class WDApp : public wxApp
{
public:
bool OnInit();
void LButtonClick(wxEvent& event);
void CButtonClick(wxEvent& event);
WDTopWindow *MainFrame;
wxDynamicLibrary *Libs;
wxButton* NewBtnPtr;
};
Code: Select all
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "exe.h"
IMPLEMENT_APP(WDApp)
bool WDApp::OnInit()
{
MainFrame = new WDTopWindow(MAINFRAMEID, "-> >-", wxDefaultPosition, wxSize(150, 300));
MainFrame->Show();
SetTopWindow(MainFrame);
Libs = new wxDynamicLibrary();
return true;
}
void WDApp::LButtonClick(wxEvent& WXUNUSED(event))
{
WDApp *AppObjPtr = &wxGetApp();
AppObjPtr->Libs->Load("dll");
if (wxGetApp().Libs->IsLoaded())
{
AppObjPtr->MainFrame->MainPanel->LButton->Disable();
AppObjPtr->MainFrame->MainPanel->CButton->Enable();
AppObjPtr->MainFrame->SetTitle("->>-");
}
else
{
wxMessageBox("Can't find dll.DLL!", "Confirm", wxOK);
}
}
void WDApp::CButtonClick(wxEvent& WXUNUSED(event))
{
WDApp *AppObjPtr = &wxGetApp();
if (AppObjPtr->Libs->HasSymbol("CreateButton"))
{
CreateFuncPtr procPtr;
procPtr = (CreateFuncPtr)(AppObjPtr->Libs->GetSymbol("CreateButton"));
int width, height = 100;
AppObjPtr->MainFrame->MainPanel->GetClientSize(&width, &height);
AppObjPtr->NewBtnPtr = procPtr(AppObjPtr->MainFrame->MainPanel, NBUTTONID, "Push me!", wxPoint(5, 120), wxSize(width-10, 40));
AppObjPtr->MainFrame->MainPanel->CButton->Disable();
}
else
{
wxMessageBox("Can't find function CreateButton!", "Confirm", wxOK);
}
}
WDTopWindow::WDTopWindow(wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size, long style, const wxString& name)
:wxFrame((wxWindow*)NULL, id, title, pos, size, style, name)
{
int width, height = 100;
GetClientSize(&width, &height);
MainPanel = new WDPanel(this, MAINPANELID, wxPoint(0, 0), wxSize(width, height), wxTAB_TRAVERSAL, "panel");
}
WDPanel::WDPanel(wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style, const wxString& name)
:wxPanel(parent, id, pos, size, style, name)
{
int width, height = 100;
GetClientSize(&width, &height);
LButton = new wxButton(this, LBUTTONID, "Load DLL!", wxPoint(5, 5), wxSize(width-10, 20));
CButton = new wxButton(this, CBUTTONID, "Create!", wxPoint(5, 30), wxSize(width-10, 20));
CButton->Disable();
LButton->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(WDApp::LButtonClick));
CButton->Connect(wxID_ANY, wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(WDApp::CButtonClick));
}