开发者

How to use wxTheApp macro outside the module it is declared?

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.


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:

When to use dynamic vs. static libraries

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜