I'm working on embedding Python 2.6 into an existing c++ application. So far I have the Libraries linked in and am able to successfully initialize the Python Interpreter and can also transfer data to Python. I'm having trouble retrieving it, and hope someone can steer me the right direction. I'm working with this:
Py_Initialize();
pModule = PyImport_ImportModule("cBuffers"); // This crashes after 1st call.
pDict = PyModule_GetDict(pModule);
pClass = PyDict_GetItemString(pDict, "rf_pdf");
pMeth = PyString_FromString("main");
if (PyCallable_Check(pClass) && PyClass_Check(pClass)) {
pInstance = PyInstance_New(pClass, NULL, NULL);
pOutput = PyObject_CallMethodObjArgs(pInstance, pMeth, pOpts, pInput, NULL);
}
if (pOutput != NULL) {
string pPdf = PyString_AsString(pOutput);
Py_DECREF(pOutput);
} else {
PyErr_Print();
}
// Cleanup
Py_DECREF(pModule);
Py_DECREF(pModule); // Has an extra reference, not positive why.
Py_DECREF(pMeth);
Py_DECREF(pInstance);
Py_DECREF(pOpts);
Py_DECREF(pInput);
Py_Finalize();
pOpts and pInput are both generated using PyString_FromString
earlier in the code. The trouble I'm having is that when I attempt to retrieve the output using PyString_AsString the return value is NUL Terminated. Unfortunately, because I'm generating PDF Documents, NULs are not only allowed, they're almost guaranteed. Can anyone tell me how I return String Data from Python back to C++ without ending at the first NUL it encounters?
As an additional question, This code can be called multiple times as a part of a background service that's creating PDF Documents from incoming Print Data. The first time this code is called into it works as expected. Any subsequent calls fail at the indicated line just after Py_Initialize()
. Help on how to determine what's going on there would be most appreciated as well. Thanks in advance,