Sign in to follow this  
ChaosEngine

control C++ app from python

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this