简体   繁体   English

C ++嵌入式Python PyBytes_AsString导致DLL崩溃

[英]C++ embedded Python PyBytes_AsString causing DLL to crash

Attempting to get a C++ and Python embedded DLL working and it seems to be stumbling over the PyBytes_AsString component. 试图使C ++和Python嵌入式DLL正常工作,并且似乎在PyBytes_AsString组件上遇到麻烦

Specifically this line: 具体来说这行:

strcpy(buffer, PyBytes_AsString(pValue));

It's trying to copy the return value from the milp_closest.solve function int to char buffer. 它试图将返回值从milp_closest.solve函数int复制到char缓冲区。 But it crashes the DLL almost like they are incompatible types. 但是它几乎使DLL崩溃,就像它们是不兼容的类型一样。

Input format - milp_closest.solve(10, 20, 30, 25) 输入格式 -milp_closest.solve(10,20,30,25)

Output string format - (0, 1, 25.0, [1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1]) 输出字符串格式 -(0,1,25.0,[1,1,1,1,1,0,1,1,1,1,1])

// The Function calls a Python module named milp_closest()
// Input parameters: 4 integers
// Output parameters: a string return by the call to the Python module
extern "C" LPCSTR __declspec( dllexport ) __stdcall TS2Py_Milp(int n, int p1, int p2, int average) 
{
static char buffer[256]={""};

PyObject *pName, *pModule, *pDict, *pfSolve;
PyObject *pArgs, *pValue;

Py_Initialize();

//pName = PyString_FromString("");
/* Error checking of pName left out */

pModule = PyImport_ImportModule("milp_closest"); // which Python module do we wish to load
//Py_DECREF(pName);

if (pModule != NULL)
{
    pfSolve = PyObject_GetAttrString(pModule, "solve"); // which function in the above-loaded module, do we wish use
    /* pfSolve is a new reference */

    if (pfSolve && PyCallable_Check(pfSolve))
    {
        pArgs = PyTuple_New(4);  //The Solve funciton takes  4 input parameters

        pValue = PyLong_FromLong((long) n); // store 1st parameter  (n) for function Solve
        /* pValue reference stolen here: */
        PyTuple_SetItem(pArgs, 0, pValue);

        pValue = PyLong_FromLong((long) p1); // store 1st parameter  (n) for function Solve
        PyTuple_SetItem(pArgs, 1, pValue);

        pValue = PyLong_FromLong((long) p2); // store 1st parameter  (n) for function Solve
        PyTuple_SetItem(pArgs, 2, pValue);

        pValue = PyLong_FromLong((long) average); // store 1st parameter  (n) for function Solve
        PyTuple_SetItem(pArgs, 3, pValue);


        pValue = PyObject_CallObject(pfSolve, pArgs); // call the Python funciton "Solve"
        Py_DECREF(pArgs);

        if (pValue != NULL) 
        {
            strcpy(buffer, PyBytes_AsString(pValue));// copy the RERTURN value(string) form Python function call, in to our return value
            Py_DECREF(pValue);
        }
        else
        {
            Py_DECREF(pfSolve);
            Py_DECREF(pModule);
            PyErr_Print();
            fprintf(stderr,"Call failed\n");
            return "call to function solve() failed";
        }
    }
    else
    {
        if (PyErr_Occurred())
            PyErr_Print();
        fprintf(stderr, "Cannot find function \"solve\"\n" );
    }

    Py_XDECREF(pfSolve);
    Py_DECREF(pModule);
}
else
{
    PyErr_Print();
    fprintf(stderr, "Failed to load \"milp_closest.py\"\n");
    return "Failed to laod module <milp_closest.py>";
}


Py_Finalize();

// we return (or pass on) the string returned from the call to the Python function "solve".
return (LPCSTR) buffer;

Any recommendations on where I might have gone wrong? 关于我可能在哪里出错的任何建议?

1st, make sure you know the exact line that is causing the crash (either using print statements or through a debugger). 首先,请确保您知道导致崩溃的确切行(使用打印语句或通过调试器)。 This will make sure we're debugging the right area. 这将确保我们在调试正确的区域。 Also, try printing the char* that's returned from PyBytes_AsString(). 另外,尝试打印从PyBytes_AsString()返回的char *。

Second, you're passing a tuple to the the milp_closest.solve() function, but your example passing 4 ints. 其次,您将一个元组传递给milp_closest.solve()函数,但是您的示例传递了4个整数。 I think you meant this instead? 我想你是这个意思吗?

PyObject* args = Py_BuildValue("llll", 10, 20, 30, 25);
if (!*args) {
    /* handle error */
    }

Py_Object* pValue = PyObject_CallObject(pfSolve, args);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM