tags:

views:

185

answers:

1

Hi! I'm using wxWidgets 2.8.9, built with the default settings under Windows XP, VC9. And I have absolutely standard EXE with IMPLEMENT_APP like this:

#include <wx/wx.h>
#include <wx/image.h>
#include "MainFrame.h"

class MyMainApp: public wxApp {
public:
    bool OnInit();
};

IMPLEMENT_APP(MyMainApp)

bool MyMainApp::OnInit()
{
    wxInitAllImageHandlers();
    wxFrame* frame_mainFrame = new MainFrame(NULL, wxID_ANY, wxEmptyString);
    SetTopWindow(frame_mainFrame);
    frame_mainFrame->Show();
    return true;
}

The MainFrame is a wxFrame with a "HelloWorld" text. This works fine when everything is linked in the EXE. The problem is, I would like to reuse this MainFrame class in another application and therefore I would like to have it in a DLL, so I can use the DLL code from everywhere.

Because my DLL has different export macro than wxWidgets, I can't export any derived from wxFrame class outside my Dll, so I make a factory class, which simply has one static method create(), returning new MainFrame(NULL, wxID_ANY, wxEmptyString);

So far so good. I have now a DLL, containing the MainFrame class, and one more FrameFactory class. Only the FrameFactory class is exported from my DLL and I can create the MainFrame in the EXE, in the OnInit() method like this: wxFrame* frame_mainFrame = FrameFactory::create();

The problem is that the constructor of the base class wxFrame calls wxTopLevelWindowMSW::CreateFrame(...), where the macro wxTheApp is invoked. This wxTheApp macro is actually a call to wxApp::GetInstance(). I was surprised that my wxApp instance is NULL when MainFrame is not in the EXE.

Could somebody familiar with wxWidgets help me what am I doing wrong? I made several more expreriments and always wxTheApp is NULL when the code using this instance variable is used in a different module, than the one where macro IMPLEMENT_APP is called.

+1  A: 

I don't use wxWidgets myself (go Qt!) But did you by any chance statically link your DLL to wxWidgets, such that the EXE and the DLL each have their own copy of the lib...?

http://wiki.wxwidgets.org/Creating_A_DLL_Of_An_Application

That would explain why your DLL's global variables for tracking the instance would be null (while the EXEs were set up in app initialization). If this is the case I'd be concerned about your SetInstance() workaround...who knows what other singletons there are:

http://stackoverflow.com/questions/140061/when-to-use-dynamic-vs-static-libraries/140444#140444

Hostile Fork
Maybe I should go to sleep... You are absolutely correct. The default build settings for wxWidgets produce static libs... at the moment I built wxWidgets with DLLs it all worked :) Thanks for pointing this out :) I deleted my answer with SetInstance() because it is totally incorrect.
m_pGladiator
I probably shouldn't go around answering questions about things I don't use. But all the things I *do* use get 5 answers before I can finish a response. So glad my guess was right here. :)
Hostile Fork