Skip to main content
gamedev.net
🔒 Locked

control C++ app from python

Started by ChaosEngine Oct 20, 2005 at 7:25 PM 3 replies 900+ views
Original Post
ChaosEngine
ChaosEngine
Hi all, I have an mfc GUI application, which I need to control externally. What I'd like to do is provide a python api (using the excellent boost.python library) and control it using a python script. I have the api defined, but I need some way of tying the script to a running instance of the app, so that api calls affect the running instance of the application. so what I have is app.exe, appAPI.dll (this exposes the python interface) and a python script that calls methods of the api. what I need is something like

class PyAPI // exported to python
{
   AppDlg* appInst; 
   PyAPI()
   {
       appInst = GetPointerToRunningInstanceOfApp();
   }

   void dosomething()
   {
      appInst->dosomething();
   }
}
any ideas? is this even possible? would I be better off using some sort of IPC? all suggestions appreciated.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
SiCrane
SiCrane
Do you want the script to be an external process, or do you want to embed Python in your application?
ChaosEngine
ChaosEngine
Quote:
Original post by SiCrane
Do you want the script to be an external process, or do you want to embed Python in your application?


preferrably an external process, but I'm open to ideas.
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
SiCrane
SiCrane
If you want the python script to be an external process, then your best bet is probably to automate your program using COM. Since you are using MFC it shouldn't be too difficult, just annoying. Look up "Automation Servers" and "IDispatch" and the "AUTOCLIK" sample MFC application. Your Python script will then need to use win32com to dispatch to the automation server.

On the other hand, if you want your python scripts to be run in the same process, things are significantly simpler. Just embed Python in your application, and take the API that you wanted to expose and wrap that in a normal BOOST_PYTHON_MODULE declaration. Remember to initialize the module after you call Py_Initialize() and you should be set.
ChaosEngine
ChaosEngine
sweet. thanks, I'll give that a try when I get to work on tuesday (long weekends rule).
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.