[Q] ctypes callbacks with Delphi

Jimmy Retzlaff jimmy at retzlaff.com
Sun Oct 19 08:04:55 EDT 2003


achrist at easystreet.com wrote:
> I've tracked this down to some kind of an interaction between
> ctypes and the GUI'S.
> 
> Python calls Delphi through ctypes.  Delphi opens a form on the
> screen.  When Delphi calls back to python through ctypes, the
> Delphi form is still open.
> 
> If the called-back function in python uses its own dialog to
> get the callback answers (I'm using wxPython), then the return
> value back to the Delphi dll gets messed up and everything crashes
> as previously described.  If the called back function figures
> out a value without doing anything GUI, it works fine.
> 
> I suppose that the source of this problem is something way deep
> down in ctypes where it is handling threads or something, IDK.
...

The extent of ctypes' involvement in the problem is probably that it
lets you make calls between Python and Delphi. It's more likely that the
problem stems from the fact that you are using two different GUI
toolkits that aren't designed to integrate with each other in the same
application.

A Windows GUI application is expected to have one mechanism which
responds to UI and system events (typically referred to as the "message
pump"). This message pump takes messages from Windows (paint events,
mouse events, etc.) and dispatches them to the correct handlers. If you
wrote a GUI application using the Win32 API directly then you'd be
responsible for setting up the message pump and registering all the
appropriate handlers with it. Delphi and wxPython do this for you, but
each does it in its own way.

I don't know Delphi at all, but to bring up a form it would need to
start pumping messages for anything more complicated than a standard
Windows message box. wxPython also needs to pump messages when you start
putting up windows with it. At this point they'll each be pumping some
of their own messages and some messages intended for the other which
will likely confuse both of them (if it doesn't confuse Windows first).
If you had access tp enough of the source code, you could do a bunch of
integration work to create a master message pump which would dispatch
events to Delphi and wxPython as appropriate, but this would be a ton of
work. I'm not sure you'd even have enough access to the innards of
Delphi to do such a thing without resorting to hooking system DLLs and
you'd have to go deep into C++ to do it in wxPython.

The most straightforward approach would be to do all of your UI code in
one or the other of Delphi and wxPython. As you've found, you can mix
non-GUI code pretty easily. Another approach might be to spawn two
executables, one for your wxPython windows, and another for your Delphi
forms, and use an IPC mechanism to communicate between them.

Jimmy





More information about the Python-list mailing list