So I'm writing a Windows DLL for use in another program, and I want to have certain C++ functions call Python functions in a Python script. The C++ functions are in an instantiated object; when the object calls the script, the script should be able to call that object's functions as well. I've been trying to do this through Boost.Python but the documentation for Boost.Python is pretty vague on how I'd accomplish this, and the few tutorials I've found have been equivalents of "... and then draw the rest of the owl."
(I've also heard things about using pybind, but I'm restricted to MSVC 9.0 and pre-C11 at this stage of development). It's been a pretty rough slog uphill just to get to where I'm at, which isn't far.
So basically, I'm (very roughly) looking along the lines of:
BOOST_PYTHON_MODULE(PythonModule)
{
class_<CPythonModule, boost::noncopyable>("Functions", boost::python::no_init)
.def("foo", &CPythonModule::foo)
}
void CPythonModule::foo(string message)
{
some_function_that_prints_things(message);
}
int main_module_method()
{
Py_Initialize();
???
call_python_script_function(script.py, bar);
}
And then the script:
import PythonModule
def bar:
PythonModule.Functions.foo("Foo bar")
I assume I'd need to pass the instance of the C++ object to the Python script, and ideally the script or the Python interpreter would be an object that I'd keep around until the DLL is unloaded. I'm also pretty vague about the actual details of calling that script. My searches have been frustratingly fruitless.
EDIT:
With some fiddlework, it looks like my biggest unknown is figuring out how to send my current instance of the object to the Python script and calling that object's functions, without creating a new object within Python. With this line, I can make the object uninitializable:
class_<CPythonModule, boost::noncopyable>("Functions", boost::python::no_init)
However, I also then can't call the functions in the class, because Python requires an instance of the class in order to call them! If the object makes its own copy of the class, it also calls the destructors of it when it dies, causing all kinds of problems in the main program the DLL is run in.