小编Aha*_*024的帖子

如何在Python中处理来自C++的PyObject*

我创建了用C++编写的DLL,导出函数返回PyObject*.然后我使用ctypes在Python中导入DLL.现在,我怎样才能得到真正的PyObject?

这是c ++代码的一部分:

PyObject* _stdcall getList(){

    PyObject * PList = NULL;
    PyObject * PItem = NULL;
    PList = PyList_New(10);

    vector <int> intVector;
    int i;
    for(int i=0;i<10;i++){
        intVector.push_back(i);
    }

    for(vector<int>::const_iterator it=intVector.begin();it<intVector.end();it++){
        PItem = Py_BuildValue("i", &it);
        PyList_Append(PList, PItem);
    }
    return PList;
}
Run Code Online (Sandbox Code Playgroud)

和一些python代码:

dll = ctypes.windll.LoadLibrary(DllPath)
PList = dll.getList()
Run Code Online (Sandbox Code Playgroud)

*我想获得包含1,2,3,4 ... 10的真正的python列表?* 我是否清楚?谢谢你

c++ python dll ctypes pyobject

1
推荐指数
2
解决办法
3269
查看次数

标签 统计

c++ ×1

ctypes ×1

dll ×1

pyobject ×1

python ×1