SWIG function with pointer struct

2019-07-31 16:50发布

Im new using SWIG to wrapped C shared library.

I have problem to call a C function with Struct pointer in python.

My files:

ST_Param.h:

typedef struct {
       unsigned int* device_Address;
       ....
       ....
       unsigned int lock;
}ST_param_ST;      


unsigned char ST_Param_Initialize(ST_param_ST * ST_param, unsigned int device_Address);

ST_Param.c

......... Rest of file.............

unsigned char ST_Param_Initialize(ST_param_ST * ST_param, unsigned int device_Address){

    if(ST_param == NULL){
    .......... rest of funtion .......................

    return 0;
}

Within ST_Param_Initialize I confirm that the pointer exists, if not believe

ST_Param.i:

/* File : ST_Param.i */
%module ST_Param

%{
#define SWIG_FILE_WITH_INIT
#include "ST_Param.h"
%}




%include "typemaps.i" 
%include "ST_Param.h"

I compiled and generated a .so file good. In python I can import a library but I cant call a ST_Param_Initialize because a need a ST_Param_ST * parameter:

ST_param_ST * error

How can I do this?

Note: i cant modify a .c and .h file. Only a .i file.

A search in google but I dont understand how to do it

Thanks Regards.

1条回答
Fickle 薄情
2楼-- · 2019-07-31 16:51

You do this essentially the same way you do it in C: You first create a ST_param_ST struct and then pass this to the initialization function ST_Param_Initialize(). Here is an example in Python, assuming your module is called ex:

>>> import ex
>>> ST_Param = ex.ST_param_ST()
>>> ex.ST_Param_Initialize(ST_param, 42)
查看更多
登录 后发表回答