简体   繁体   English

带有指针结构的SWIG函数

[英]SWIG function with pointer struct

Im new using SWIG to wrapped C shared library. 我使用SWIG来包装C共享库。

I have problem to call a C function with Struct pointer in python. 我在用Python中的Struct指针调用C函数时遇到问题。

My files: 我的文件:

ST_Param.h: 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 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_Initialize中,如果不相信该指针,则确认该指针存在

ST_Param.i : 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. 我编译并生成了一个.so文件。 In python I can import a library but I cant call a ST_Param_Initialize because a need a ST_Param_ST * parameter: 在python中,我可以导入一个库,但是我不能调用ST_Param_Initialize,因为需要一个ST_Param_ST *参数:

ST_param_ST * error ST_param_ST *错误

How can I do this? 我怎样才能做到这一点?

Note: i cant modify a .c and .h file. 注意:我无法修改.c和.h文件。 Only a .i file. 仅.i文件。

A search in google but I dont understand how to do it 在谷歌搜索,但我不知道该怎么做

Thanks Regards. 感谢和问候。

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() . 这样做的方式基本上与在C语言中执行的方式相同:首先创建一个ST_param_ST结构,然后将其传递给初始化函数ST_Param_Initialize() Here is an example in Python, assuming your module is called ex : 这是Python中的示例,假设您的模块名为ex

>>> import ex
>>> ST_Param = ex.ST_param_ST()
>>> ex.ST_Param_Initialize(ST_param, 42)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM