Page 1 of 1

How to use Widgets from a dll

Posted: Tue Aug 27, 2019 10:22 am
by ifvimy
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.

Re: How to use Widgets from a dll

Posted: Tue Aug 27, 2019 10:50 am
by PB
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"?

Re: How to use Widgets from a dll

Posted: Wed Aug 28, 2019 1:12 am
by ifvimy
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?

Re: How to use Widgets from a dll

Posted: Wed Aug 28, 2019 1:05 pm
by doublemax
Can you explain the use case a bit more? What about the host application? Will it use wxWidgets, too?

Re: How to use Widgets from a dll

Posted: Wed Aug 28, 2019 1:13 pm
by T-Rex
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

Re: How to use Widgets from a dll

Posted: Thu Aug 29, 2019 1:14 am
by ifvimy
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?

Re: How to use Widgets from a dll

Posted: Thu Aug 29, 2019 8:13 am
by doublemax
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.