How to use Widgets from 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
ifvimy
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Aug 16, 2019 6:29 am

How to use Widgets from a dll

Post by ifvimy » Tue Aug 27, 2019 10:22 am

I have saw the dll sample, then if I want to use the wxPanel from the dll, for example, add the panel to a wxAuiNotbook, I tried, but failed, what should I do?And is there some easy way to use a wxPanel and etc from a dll?
Thanks a lot.

PB
Part Of The Furniture
Part Of The Furniture
Posts: 2020
Joined: Sun Jan 03, 2010 5:45 pm

Re: How to use Widgets from a dll

Post by PB » Tue Aug 27, 2019 10:50 am

There is no difference in code when using static and dynamic builds of wxWidgets. The differences are in project setup (defines, folders) and that DLLs must be available to the program at runtime, see e.g. here viewtopic.php?f=19&t=46212 for instructions how to switch between static and dynamic builds of wxWidgets with MSVC. The procedure is basically the same for MinGW.

What does it mean "I tried but I failed"?

ifvimy
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Aug 16, 2019 6:29 am

Re: How to use Widgets from a dll

Post by ifvimy » Wed Aug 28, 2019 1:12 am

Thanks for your prompt reply. I mean I want to use wxWidgets in my own dll, which maybe a wxPanel or wxDialog in it. In main application, I used the dll and try to construct dll's wxPanel, but failed to register wclass...'. Then I seen the 'dll samples', it solved, but I want to know is there a easier way?and if I want to add the wxPanell(dll's) to wxAuiNotebook, the sample doesn't work, what should I do?

User avatar
doublemax
Moderator
Moderator
Posts: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to use Widgets from a dll

Post by doublemax » Wed Aug 28, 2019 1:05 pm

Can you explain the use case a bit more? What about the host application? Will it use wxWidgets, too?
Use the source, Luke!

User avatar
T-Rex
Moderator
Moderator
Posts: 1178
Joined: Sat Oct 23, 2004 9:58 am
Location: Zaporizhzhya, Ukraine
Contact:

Re: How to use Widgets from a dll

Post by T-Rex » Wed Aug 28, 2019 1:13 pm

There is sample project which shows how to use wxWidgets controls from DLL.
You have to build DLL version of wxWidgets (Debug DLL and Release DLL) and use the same compiler settings (see C++ -> Code Generation -> Runtime Library parameter in wxWidgets projects settings). In fact, CMake should put the correct values there if you use it for generating MSVC projects.

https://github.com/T-Rex/wxModularApp

ifvimy
In need of some credit
In need of some credit
Posts: 4
Joined: Fri Aug 16, 2019 6:29 am

Re: How to use Widgets from a dll

Post by ifvimy » Thu Aug 29, 2019 1:14 am

doublemax wrote:
Wed Aug 28, 2019 1:05 pm
Can you explain the use case a bit more? What about the host application? Will it use wxWidgets, too?
I'm sorry. In my solution, there are so many wxPanels and wxDialogs, then I decide to write some of them in other project which will be build to dlls. First, I pass args(wxWindow *parent) to construct the wxPanel in dll, but it "failed to register a wclass…', then I see the dll sample to solve it, but is there any simpler and convenient way to solve the problem also?Besides, I want to use wxAui(main project's) to manager these wxPanels in dll, so I need to gain the dll's wxPanel object and add them to wxAui, so I export new function to gain the dll's wxPanel object, however, it failed, so I debug, may be by thread, the wxPanell used before init. So, how to solve.it?

User avatar
doublemax
Moderator
Moderator
Posts: 13985
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: How to use Widgets from a dll

Post by doublemax » Thu Aug 29, 2019 8:13 am

In my solution, there are so many wxPanels and wxDialogs, then I decide to write some of them in other project which will be build to dlls.
Why? Do you intend to use them in another project, too? Otherwise i really see no reason to go that extra mile. It's just extra effort (as you already noticed) and it also makes debugging more complicated.

In any case, the "DLL sample" is not the right one for this purpose. It's intended for the special case where the main application does not use wxWidgets and both the main app and the wxWidgets part need to have their own event loop.

In your case, when both the main app and the DLL use wxWidgets, it's important that you use dynamic linking for wxWidgets, so that both use the same wxWidgets instance. Do you do that?

The link T-Rex posted should contain everything you need.
Use the source, Luke!

Post Reply