How to get the output struct parameter by extending C function?

jammy huzaigang at
Tue Jul 28 05:06:06 CEST 2009


I an just starting to use the ctypes module.
Now I want to expentd C in python.
In the function of C, I want to get the  output struct parameter which 
is chaged in the function.
following is my code:

----------- 1.Header file of C(information.h)-----------
typedef struct {
 char  name[20];
 int   height;
 int   weight;
} Infor;
int getInfor( char* name, int height, int weight, Infor* infor);

----------- 2.Source file of C(information.c)-----------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "Extest.h"
int getInfor( char* name, int height, int weight, Infor* infor)
    if(name == NULL || infor == NULL)
        return (-1);

    printf("  [%s] get params: name[%s] height[%d] weight[%d]\n", 
__FUNCTION__, name, height, weight);
    strcpy(infor->name, name);
    infor->height = height;
    infor->weight = weight;
    return (1);

----------- 3.information.c's warp file for Python calling(information_wrap.c)----------- 

#include "Python.h"
#include "ceval.h"
#include "Extest.h"

static PyObject * Extest_getInfor(PyObject *self, PyObject *args)
    char* name;
    int height;
    int weight;
    Infor* infor;
    int res;

    if(!PyArg_ParseTuple(args, "siiO:infor", &name, &height, &weight, 
         return NULL;

    printf(" ***** Before Call:[%s] infor.height[%d] 
infor.weight[%d] *****\n",infor->name, infor->height, 
infor->weight);//[1]initial value
    res = getInfor(name, height, weight, infor);
    printf(" ***** After Call:[%s] infor.height[%d] 
infor.weight[%d] *****\n", infor->name, infor->height, 
infor->weight);//[2]changed value

    return (PyObject*)Py_BuildValue("i", res);

static PyMethodDef ExtestMethods[] = {
    { "getInfor", Extest_getInfor, METH_VARARGS },
    { NULL, NULL },

void initExtest(void)
    PyObject *m;
    m = Py_InitModule("Extest", ExtestMethods);

----------- 4.Makefile-----------
# Makefile of Extend C for Python
OBJS          = Extest.o ExtestWrappers.o
TARGET      =
INCLUDES   = -I /usr/local/include/python2.6/
MAKEFILE   = Makefile
CC             = gcc
CFLAGS      = -fPIC -Wall -Wstrict-prototypes -Winline \
                    ${INCLUDES} -O2
all : ${TARGET}
    ${CC} -shared -o ${TARGET} ${OBJS}
    rm -f ${OBJS}
clean :
    rm -f *.o *.Z* *~ ${TARGET}

----------- 5.test file of Python(
#!/usr/bin/env python2.6

import Extest
from ctypes import *

class Infor(Structure):
    _fields_= [('name', c_char*20),
    ('height', c_int),
    ('weight', c_int)]

def main():
    infor = Infor() = 'NoName'
    infor.height = -1
    infor.weight = -1

    name = 'TestName'
    height = 175
    weight = 160

    print 'Before call getInfor(),  Infor.height=%d 
Infor.weight=%d' % (, infor.height, infor.weight)
    Extest.getInfor(name, height, weight, byref(infor))
    print 'After call getInfor(),  Infor.height=%d 
Infor.weight=%d' % (, infor.height, infor.weight) [3]want to 
get value of infor changed
    print ' ----- all DONE -----'

if __name__ == '__main__':

----------- 6.result of running -----------
[root at localhost obj_struct_test]# ./
Before call getInfor(),  Infor.height=-1 
sizeof(*infor)=28, sizeof(Infor)=28
        ***** [Extest_getInfor] Before Call:[] 
infor.height[0] infor.weight[0] *****
                [getInfor] get params: name[TestName] height[175] 
        ***** [Extest_getInfor] After Call:[TestName] 
infor.height[175] infor.weight[160] *****
After call getInfor(),  Infor.height=-1 
 ----- all DONE -----

Looking for the printed information, in the file of 
information_wrap.c(setp 3), In the comment 's tag[2], printed value is 
changed valuse.
Why  is the printed Information initial value atfer calling getInfor() 
Thanks for your help!

                       yours: jammy 

More information about the Python-list mailing list