Call Python functions from C[++] as macros

Glen Parker glenebob at nwlink.com
Mon Jan 17 14:58:34 EST 2000


Hi all,

Just getting started with Python (just downloaded the Win32 version last
night :-).  I need to figure out how to get some reasonably advanced things
working.  I'm writing a large app in C++ (BCB4.0) and I want to use Python
as an event handler for executing business rules etc.  So far I've been able
to export a function from C++ that is called from a Python script, and been
able to execute the script from C++, based on the examples.  That's a
usefull start, but I'm not sure where to look now for the next step.  I hope
somebody can give me some good pointers.

I want to have one or a small number of Python files that implement
functions I can call from C++, but I don't believe I have any need to
actually run the file(s) as scripts.  So I need to be able to pre-compile or
pre-import or whatever the file, get function references hooked up, and then
call them from arbritary spots in my C++ code.  Something like this:

void init() {
  Module = ImportPythonFile("ButtonEvents.py");
  Funcs["ButtonPress1"] = GetPythonFunction(Module, "ButtonPress1");
  Funcs["ButtonPress2"] = GetPythonFunction(Module, "ButtonPress2");
}

void Button1PressEvent()
{
    CallPythonFunction(Funcs["ButtonPress1"]);
}

void Button2PressEvent()
{
    CallPythonFunction(Funcs["ButtonPress2"]);
}

I know there are other things to be done here too, like passing args and
parsing return values, but I think I should be OK when it comes time to get
into that phase.

Any small code frags or tips on which Python library functions I'm looking
for here would be greatly appreciated.

TIA
Glen Parker
glenebob at nwlink.com





More information about the Python-list mailing list