Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用ctypes将结构从C传递到Python_Python_C_Ctypes - Fatal编程技术网

使用ctypes将结构从C传递到Python

使用ctypes将结构从C传递到Python,python,c,ctypes,Python,C,Ctypes,我试图将struct从C传递到Python,但当某个属性是char* 测试h typedef struct _FOO { int field1; char field2[5]; int whatever; } FOO, *PFOO; void CallPythonFunction(PFOO foo); 测试c PyObject *module_name, *plugin, *PyTargetFunction, *ppresult, *pargs; void C

我试图将
struct
从C传递到Python,但当某个属性是
char*

测试h

typedef struct _FOO 
{
    int field1;
    char field2[5];
    int  whatever;
 } FOO, *PFOO;

 void CallPythonFunction(PFOO foo);
测试c

PyObject *module_name, *plugin, *PyTargetFunction, *ppresult, *pargs;
void CallPythonFunction(PFOO foo)
{
    // Set PYTHONPATH TO working directory
    setenv("PYTHONPATH",dir_python_modules,1);

    // Initialize the Python Interpreter
    Py_Initialize();
    module_name = PyString_FromString((char*)"test");

    // Load the module object
    if ((plugin = PyImport_Import(module_name)) == NULL) {
        PyErr_Print();
        printf("Error: PyImport_Import\n");
        return -1;
    }

    PyTargetFunction = PyObject_GetAttrString(plugin, (char*)"some_function");
    pargs = PyTuple_Pack(1
        //, PyLong_FromUnsignedLong((unsigned int) validacion)
        , PyLong_FromVoidPtr(foo)
        );

    ppresult = PyObject_CallObject(PyTargetFunction, pargs);
}
test.py

import ctypes
POINTER = ctypes.POINTER

class _PyFoo(ctypes.Structure):
    _fields_ = [
        ('field1', ctypes.c_int),
        ('field2', ctypes.c_char_p),
        #('field2', POINTER(ctypes.c_char), # not work either
        ('whatever', ctypes.c_int)
        ]

def some_function(foo):
    foo_view = _PyFoo.from_address(foo)

    print("foo.field1: ", foo_view.field1)
    print("foo.field2: ", foo_view.field2.value)
    print("foo.whatever: ", foo_view.whatever)
    pass
main.c

int main(int argc, char *argv[])
{
    PFOO foo = malloc(sizeof(FOO));
    foo->field1 = 5;
    sprintf(foo->field2, "hello");
    foo->whatever = 3;
    CallPythonFunction(foo);

    return 0;
}
我需要得到这个输出:

('foo.field1: ', 5)
('foo.field2: ', 'hello')
('foo.whatever: ', 3)

test.py
中,
field2
的类型不正确
ctypes.c_char*5
是c
char[5]
的正确ctypes语法

同样在
test.py
中,将
foo_view.field2.value
更改为
foo_view.field2
,因为它不是指针。如果没有这一更改,Python代码将抛出一个当前未由
test.c
代码处理的异常,并且它将在第一次
打印后停止


main.c
中,
sprintf(foo->field2,“你好”)将发生缓冲区溢出。

test.py
中,
字段2的类型不正确
ctypes.c_char*5
是c
char[5]
的正确ctypes语法

同样在
test.py
中,将
foo_view.field2.value
更改为
foo_view.field2
,因为它不是指针。如果没有这一更改,Python代码将抛出一个当前未由
test.c
代码处理的异常,并且它将在第一次
打印后停止


main.c
中,
sprintf(foo->field2,“你好”)将出现缓冲区溢出。

如您所说的那样进行了更改,效果非常好。谢谢@mark tolonen。关于缓冲区溢出,是的,你是对的。我把我的“hola”翻译成“hello”发到这里。就像你说的那样改变了,效果很好。谢谢@mark tolonen。关于缓冲区溢出,是的,你是对的。我把我的“你好”翻译成“你好”,然后把它贴在这里。