Passing a string from c++ to Python function and vice-versa:
Initialize all required variables first.
Py_Initialize();
object main_module = import("__main__");//boost::python objects
object dictionary = main_module.attr("__dict__");
Run a code to create a variable and set an initial value and print it inside python.
boost::python::exec("resultStr = 'oldvalue'", dictionary);
PyRun_SimpleString("print resultStr");//new value will reflect in python
read the same variable from c++.
boost::python::object resultStr = dictionary["resultStr"];//read value from python to c++
std::string &processedScript = extract<std::string>(resultStr);
Above dictionary object is like a shared map. you can set a variable from c++. Then check the new value from python.
dictionary["resultStr"] = "new value";//set the variable value
PyRun_SimpleString("print resultStr");//new value will reflect in python
Have fun coding. Thanks.
No comments:
Post a Comment