python callbacks and windows

Bengt Richter bokr at oz.net
Sun Sep 11 18:14:41 EDT 2005


On 9 Sep 2005 03:20:09 -0700, davidstummer at gmail.com wrote:

>I was wondering if anyone could point me to an example. Currently i
>have a c++ program which calls and c++ dll (i created both). The dll
>uses SendMessage to pass messages back to the calling .exe, and the
>.exe process the messages in it's  Windows Procedure function
>WindProc().
>
This sounds like your main program is ordinary C++ GUI and the dll could
be anything, but you probably cause it to be loaded and initialized at
least from the GUI main prog, maybe just by automatic dependency loading.
It is not clear whether you have a lot of code and would like to use python
for some aspect of processing special messages, or whether you have a tentative
start in C++ for an application you'd rather develop entirely in python.
What you want to do next will probably depend on what you have.

>What i want is to receive these messages ( the contents of each message
>will be plain text), in python using a callback (i think this is what i
>need).
I would guess that if you want to use windows messages, it might be easiest
to receive them in the main message loop and to recognize the special messages
there (vs all the standard windows stuff) and at that point call from C++ to
python to process the plain text contents. The question is what do you want to
get back, and what side effects and state-ful processing might you want to do,
and if there is state, do you want to maintain it in python or C++, and how
to you want to access from the other side if you need to.

Calling from C++ to python and vice versa is discussed in the docs re extensions
and embedding etc. (See http://python.org/doc/ links to "Extending and Embedding"
and "Python/C API")
>
>I don't know whether (or if it's possible) to implement the windows
>procedure and stuff in python, eliminating the need for the .exe
>altogether.
Have you looked at tkinter? It will let you make a GUI that runs under windows,
and then you can code in python.
>
>or should i do it the other way, to add a callback function in python,
>and for the windows procedure in the .exe to call this python callback?
>
>in any case, some simple code examples would be great.
>
I would suggest downloading the python sources zip or tgz in addition
to the standard installation (which presumably you already have). The
reason is that the sources contain a demo subdirectory with useful examples
of what can be done with tkinter (and other stuff as well). Some of it
could stand to be updated to take advantage of current python, but you
can run the demo examples and modify them to learn a lot about what can be done.

<aside to python maintainers>
BTW, does python.org have a directory tree somewhere with a duplicate of a source installation
that people could browse with ftp:// ? That way, people could get access to the
demos and other sources without having to download the whole thing,
(e.g., ftp://python.org/pub/python/2.4.1/Python-2.4.1.tgz is ~9mb)
and people could also post with url references to particular sources.
Seems like providing source trees could be automated, so there would
only be an initial effort involved.
</aside>

Regards,
Bengt Richter



More information about the Python-list mailing list