Interfacing python program to C++ program instance

Josiah Carlson josiah.carlson at sbcglobal.net
Fri Jul 6 01:19:39 EDT 2007


Jim Langston wrote:
> Windows. Situation: Using a Python program called OpenRPG.  I have a program 
> that displays form data (a character sheet) in C++.  I am able in the C++ 
> program to build a string and copy it into the clipboard, then paste it into 
> the input in the running Python program.

If I remember correctly, OpenRPG uses wxPython for it's GUI.  wxPython 
offers fairly easy access to the clipboard contents, so it could check 
the clipboard for the kind of data it wants.

> I would like to somehow automate this, that is, have the python instance 
> communicate with the C++ instance and vice versa.  I'm still trying to think 
> of a way to do this.  There seems to be a number of options, but I'm not 
> sure which one is best, or there is a better one.
> 
> 1. Write a new C++ program/library with extern C that Python could call. 
> This interface program would open up some shared memory that the C++ 
> application would also open up, and python and C++ could communicate that 
> way.  Python writing requests to the memory, C++ responding with responses.

mmap is the standard "shared memory communication" mechanism available 
on pretty much all platforms of note (Windows, Linux, OSX, BSD, Solaris, 
BeOS, etc.)


> 2. Have the C++ program interface directly into the python form, reading 
> directly from controls.  C++ could write to the input box with reponses.

If OpenRPG uses any custom controls (which are very easy to write with 
wxPython), this won't be easy.  Also, controlling an application that 
wasn't designed for such control can be a beast.


> 3. Have Python write a small file with the request to the HD.  Have the C++ 
> program intermittedly check for the presense of this file. If it exists, it 
> would open the file, read the request, write a response file, then delete 
> the file Python wrote.

This can work, but is unnecessary.  See mmap, socket, etc.


> 4. Find out if python can directly, somehow, open up shared memory and do 
> the same as 1 without the need for the extern C interface program.

import mmap


> 5. Something else I'm not thinking of but you know.

sockets.  In particular, XML-RPC for the call/return using structured 
data in XML format.  There exists XML-RPC server and client libraries in 
Python, and XML-RPC server and client libraries exist for just about 
every language worth discussing.  Alternatively, you can pass your 
queries/string directly via sockets (rather than the clipboard).


  - Josiah



More information about the Python-list mailing list