I've initialized the Python environment by
Py_Initialize();
I have no external Python module imported into the environment and everything works well. But when I need to pass a C string into this environment, I am lost...
I was thinking to add a function in the environment to assign the variable like the following code do.
char *str;
str="\
def assign_var(str):\
global string\
string = str";
PyRun_SimpleString(str);
And then call this function in C and pass the converted C string as the arguments.
I don't think all I mentioned above is a good way solve the problem...
How can I make this work?
Solution:
Finally, here's the solution with Peter Mortensen's help. (Thanks Peter Mortensen!)
As the python environment I've initialized is a pure empty environment(without any imported modules). I use
py_main = PyImport_AddModule("__main__");
to get a hook to the main environment. and then call
PyModule_AddStringConstant(py_main, "string_name", str);
to bring the C string into the python environment.
To verify everything is done, just try:
PyRun_SimpleString("print dir()");
PyRun_SimpleString("print string_name");
and you'll see you "string_name" string appears in the dir() list and make it print by python!