- 论坛徽章:
- 0
|
我的目的是想把python结构体指针传递给c函数(c函数已经编译为python的so库),然后C函数获得结构体指针中指向的数据,
我的问题是我的C无法获取到python传给的指针
[root@localhost gcc]# cat etest.h- typedef struct {
- char name[20];
- int height;
- } Infor;
- //char* fact(Infor* haha);
复制代码 C函数
[root@localhost gcc]# cat example.c- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- typedef struct {
- char name[20];
- int height;
- } Infor;
- int fact(Infor* infor)
- {
- int n;
- char* b;
- n = infor->height;
- b = infor->name;
- printf ("%d\n",n);
- return n;
- }
复制代码 构造函数- #include <Python.h>
- #include "etest.h"
- PyObject* wrap_fact(PyObject* self, PyObject* args)
- {
- Infor* infor;
- int result;
-
- if (! PyArg_ParseTuple(args, "O:fact", &infor))
- return NULL;
- result = fact(infor);
- return Py_BuildValue("i", result);
- }
- static PyMethodDef exampleMethods[] =
- {
- {"fact", wrap_fact, METH_VARARGS, "Caculate N!"},
- {NULL, NULL}
- };
- void initexample()
- {
- PyObject* m;
- m = Py_InitModule("example", exampleMethods);
- }
复制代码 python代码- import os
- import sys
- import struct
- from ctypes import *
- import example
- class Infor(Structure):
- __fields__ = [('name', c_char*20), ('height', c_int)]
- infor = Infor()
- infor.name = "hahah"
- infor.height = 99
- aaa = byref(infor)
- print infor.height
- a = example.fact(byref(infor))
- print a
复制代码 不知道那个环节的传递出了点问题,请教各位高手 |
|