Creating a wxFrame in a DLL

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
sharkie69
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Aug 22, 2006 2:57 pm

Creating a wxFrame in a DLL

Post by sharkie69 »

I am currently trying to create a wxFrame in a dll (in Windows). I can create the dll, however when I try to use the class (wxFrame) from the dll my application crashes. After a bit of debugging, I have found that the constructor of the class is not being called. Thus the application crashes when the object of the class is used. The frame works perfectly if I do not put it into a dll. Does any one have any idea as to why this would be happening?
User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Post by xaviou »

Hello.
I recently made a small tuto for creating a dll plugin application: I first create the wxFrame in the exe, and add some wxpanels from one or more dll (a plugins system)

The tuto is in french, but perhaps it can help you
You can find it here :
http://www.wxdevelop.com/tuto=FAIRE_UNE ... id=20.html
sharkie69
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Aug 22, 2006 2:57 pm

Post by sharkie69 »

Thank you for your response. I do not understand French but hopefully I can make out something by looking at the source files of your program.
sharkie69
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Aug 22, 2006 2:57 pm

Post by sharkie69 »

Still having no joy. I have examined the source but have not found a solution. I cannot see any where in the source code where a dll is created. How do you do this? Do you know if there is any English tutorial around. Thanks.
sharkie69
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Aug 22, 2006 2:57 pm

Post by sharkie69 »

Xaviou, if you or your company offer a solution as a comercial service, do let me know as I am willing to pay for some instructions on how to get this sorted.
marioavs
In need of some credit
In need of some credit
Posts: 6
Joined: Thu Jun 08, 2006 5:18 am
Location: Guatemala, Central America
Contact:

Post by marioavs »

I'm gonna paste code that I don't remember where I found, but the last time somebody else wrote a link.

Something that I didn't know was the use of __cdecl, but WXIMPORT and WXEXPORT do all the job. If have compiled the DLL I think you have done the most difficult part. Maybe your problem is that you haven't compiled wxWindows as shared library.

header

Code: Select all

#include <wx/app.h>
#include <wx/list.h>

#ifdef WXMAKINGDLL_GLOBAL
	#define WXUSINGDLL_GLOBAL WXEXPORT
#else
	#define WXUSINGDLL_GLOBAL WXIMPORT
#endif


//Application
class wxDLLGlobalApp : public wxApp
{
public:
    bool OnInit();
};

bool WXUSINGDLL_GLOBAL xyzfunction(const wxString& parametro);


cpp file:

Code: Select all

// We use IMPLEMENT_APP_NO_MAIN so we can start the app from DllMain
// as we don't have a WinMain or main entry point
//

IMPLEMENT_APP_NO_MAIN(wxDLLGlobalApp)

bool wxDLLGlobalApp::OnInit()
{
    return true;
}

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
    switch (ul_reason_for_call)
	{
		case DLL_PROCESS_ATTACH:
		{
        //use wxInitialize() if you don't want GUI instead of the following lines
            wxSetInstance((HINSTANCE)hModule);
            int argc = 0;
            char **argv = NULL;
            wxEntryStart(argc, argv);
            if ( !wxTheApp || !wxTheApp->CallOnInit() )
            return FALSE;
		}
            break;

		case DLL_PROCESS_DETACH:
            wxEntryCleanup(); //use wxUninitialize() if you don't want GUI
            break;

		case DLL_THREAD_ATTACH:
		case DLL_THREAD_DETACH:
			break;
    }
    return TRUE;
}
2.6.1, wxMSW, MinGW, Code::Blocks
User avatar
xaviou
Super wx Problem Solver
Super wx Problem Solver
Posts: 437
Joined: Mon Aug 21, 2006 3:18 pm
Location: Annecy - France
Contact:

Post by xaviou »

Sharkie: Hello.

I'm sorry, but I made this "for fun". Programming isn't my job.

And the base idea used in this tuto is not mine : I juste found an example that I've simplified.
The original idea can be found here: http://bzzt.net/~wxwidgets/PluginExample.zip

If you've downloaded the source code of my example, you should open the workspace file with Code::Blocks.
Notice that the dll code doesn't have any exported function. It has only classes dynamically declared.

Each plugin is loaded from the exe file with the "wxPluginManager::LoadLibrary(.....)" function.
Doing this makes dynamic classes of the plugin being registered in the RTTI (the wxPluginManager is a non-documented class of wxWidgets).

When all the plugins are loaded, we browse the RTTI classes list, to look for each classes named "DllPlugin". An instance of each one is the created with the wxClassInfo::CreateObject() function, and placed in a global wxList.

When the wxFrame is created, it creates a wxNotebook, adds a first page to it, and adds a page for each plugin present in the list.

I hope this will help you. If not, ask again.

Regards
sharkie69
In need of some credit
In need of some credit
Posts: 5
Joined: Tue Aug 22, 2006 2:57 pm

Post by sharkie69 »

Thank you xaviou and marioavs for your help. I have finally managed to get my problem solved. The problem was caused since I was compiling the dll with the wxWidgets source code. When I compiled the wxWidgets library to dll's my application works perfectly.
soaringhawkzf
Knows some wx things
Knows some wx things
Posts: 35
Joined: Sun Nov 12, 2006 4:54 am

Post by soaringhawkzf »

sharkie:
Do you mean using the wxWidgets DLL instead static lib can solve the problem?

My problem is similar.
I create wxApp and wxMDIParentFrame in app, and create a wxMDIChildFrame(implementation of myself) while loading a DLL, and I use wxWidgets as a static lib.
I expose a CreateSubFrame function in the DLL, and invoke it sucessfully, but failed in wxMDIChildFrame constructor.

Thanks.
Post Reply