1

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.

5
  • maybe this helps : Boost-python How to pass a c++ class instance to a python class Commented Jul 30, 2018 at 21:41
  • 1
    MSVC 9.0 Run, don't walk, outta that place. Commented Jul 31, 2018 at 14:18
  • @Thomas It took me a couple days of rethinking how I was approaching this to realize that link actually helped a ton, turns out I was thinking way too far inside the box. Thanks! Commented Aug 1, 2018 at 18:13
  • @n.m. No joke, in about two years we're finally going to upgrade... to MSVC14 (2015). We might get to MSVC 14.1 by 2025. Commented Aug 1, 2018 at 18:19
  • Nice to hear that it helped. When you do upgrade, you might reconsider using pybind11. Commented Aug 1, 2018 at 19:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.