Using wxWidgets in the loadable DLL / so portable way

278 views
Skip to first unread message

Mikhail Tchernychev

unread,
Jul 6, 2012, 1:41:37 PM7/6/12
to wx-u...@googlegroups.com
Hello List,

I am looking for an advise.
I am trying to implement the following scenario:

1. Main application (whatever toolkit is used) loads library dynamically
using LoadLibrary() under MSW or loading shared object under Linux.

2. If DLL is loaded, main program starts new thread using pthreads library
or its port under windows. For simplicity, main application may block
and wait till thread terminates.

3. Newly created thread call wxWidget application. It appeared to me
the that it is
enough to replace (example)

IMPLEMENT_APP(MyApp)

with

wxIMPLEMENT_APP_NO_MAIN(MyApp);

int main_test() {
int argc =0;
char *argv[1];
argv[0] = "app";
::wxEntry(argc, argv);
return 0;
}

now thread calls main_test() and it makes complete winWidgets
application appear on the
screen.

4. There is no interaction between main thread and wxWidgets trhread.

I compiled the sample using "popup" example from wxWidgets distribution
and it apparently works
as expected.


My main concern is: Is it safe to do it this way?

I did look into provided "dll" example in the wxWidgets 2.3.9 samples
folder and it is only applicable to windows.
It also seems to me that ::wxEntry(int argc, char **argv) should take
cake about HINSTANCE for the thread, as
well as for clean up.

Thank you for your advise,

Best Regards
Mikhail









Vadim Zeitlin

unread,
Jul 6, 2012, 2:53:23 PM7/6/12
to wx-u...@googlegroups.com
On Fri, 6 Jul 2012 10:41:37 -0700 Mikhail Tchernychev wrote:

MT> 1. Main application (whatever toolkit is used) loads library dynamically
MT> using LoadLibrary() under MSW or loading shared object under Linux.
MT>
MT> 2. If DLL is loaded, main program starts new thread using pthreads library
MT> or its port under windows. For simplicity, main application may block
MT> and wait till thread terminates.
MT>
MT> 3. Newly created thread call wxWidget application. It appeared to me
MT> the that it is enough to replace (example)
MT>
MT> IMPLEMENT_APP(MyApp)
MT>
MT> with
MT>
MT> wxIMPLEMENT_APP_NO_MAIN(MyApp);
MT>
MT> int main_test() {
MT> int argc =0;
MT> char *argv[1];
MT> argv[0] = "app";
MT> ::wxEntry(argc, argv);
MT> return 0;
MT> }
MT>
MT> now thread calls main_test() and it makes complete winWidgets
MT> application appear on the screen.
MT>
MT> 4. There is no interaction between main thread and wxWidgets trhread.
MT>
MT> My main concern is: Is it safe to do it this way?

Yes.

MT> I did look into provided "dll" example in the wxWidgets 2.3.9 samples
MT> folder and it is only applicable to windows.

Yes, because there are extra problems with HINSTANCE and such there which
don't exist under Unix. But in principle the same approach should work
under Unix too and it would be nice to modify the sample to build there
too.

MT> It also seems to me that ::wxEntry(int argc, char **argv) should take
MT> cake about HINSTANCE for the thread, as well as for clean up.

HINSTANCE is global, not per-thread, so I'm not sure what do you mean.
If you can pass HINSTANCE of the DLL in the main application itself (you
obtain it from LoadLibrary()) then you could avoid the call to
MSWGetModuleHandle() in the DLL code. But OTOH doing it like the sample
does makes it self-contained and doesn't have any real drawbacks.

Regards,
VZ

--
TT-Solutions: wxWidgets consultancy and technical support
http://www.tt-solutions.com/

Mikhail Tchernychev

unread,
Jul 6, 2012, 3:16:16 PM7/6/12
to wx-u...@googlegroups.com
Thank you for answering, I really appreciate it.

So if this sample code is as it is, would global HINSTANCE for wxWidgets
set to HINSTANCE of the main application or it would be HINSTANCE for the
DLL ( the same as obtained via LoadLobrary())?

I think it would be safer to set it to HINSTANCE of the DLL, so as you said
using MSWGetModuleHandle() or by using msw specific form of wxEntry()

Best Regards,
Mikhail

Vadim Zeitlin

unread,
Jul 6, 2012, 7:52:41 PM7/6/12
to wx-u...@googlegroups.com
On Fri, 6 Jul 2012 12:16:16 -0700 Mikhail Tchernychev wrote:

MT> So if this sample code is as it is, would global HINSTANCE for wxWidgets
MT> set to HINSTANCE of the main application or it would be HINSTANCE for the
MT> DLL ( the same as obtained via LoadLobrary())?

Sorry, you're right, I overlooked this problem. In your code it would
indeed use the HINSTANCE of the main application which is wrong. You do
need to pass it the correct HINSTANCE using wxMSW-specific wxEntry()
overload.

MT> I think it would be safer to set it to HINSTANCE of the DLL, so as you said
MT> using MSWGetModuleHandle() or by using msw specific form of wxEntry()

Yes, you're right, I was imprecise, sorry.

Mikhail Tchernychev

unread,
Jul 6, 2012, 8:12:17 PM7/6/12
to wx-u...@googlegroups.com
Thank you for clarification.

I am trying the same code under Linux with no luck so far.
I was first using stock version 2.8.11 but it produced weird
problem: first time it runs, then when I try to run it second time
it crashes, even if I unload the library and load it again. Stack
trace is not very informative but it is something with pthread.

I am now trying latest 2.9 version. I let you know how it goes.

Best Regards,
Mikhail
Reply all
Reply to author
Forward
0 new messages