Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically loading a python module from a DLL with Boost.Python

I have a DLL written in C++ that uses Boost.Python to run Python code. The Python code should be able to interact with the actual application (in the DLL). Is it possible to have both in one library or do I need to seperate the two?

So, DLL X calls Python code, Python code imports module Y, which is also located in DLL X.

Is this possible using the Python C API or (preferrably) Boost.Python? Something like load_python_module("test", my_dll_handle)...

I saw that the DLL exports PyInit_test, but I don't know if it's a good idea to just call this, and I don't even know what arguments it expects.

like image 488
pezcode Avatar asked Apr 20 '26 21:04

pezcode


1 Answers

The Python C-API docs strongly imply that this is possible (though I've never tried it myself), using the Python C-API PyImport_AppendInittab function. You call that function before Py_Initialize with the names and pointers-to-init-functions of the modules you want to import, and they'll be made available to the embedded code as if they were built-in modules (they still have to be imported).

Because the BOOST_PYTHON_MODULE(foo) macro just creates an initfoo() function, you should be able to use this method with Boost.Python as well.

like image 167
jbosch Avatar answered Apr 22 '26 10:04

jbosch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!