How to use SWIG Generated C structures in Java as input to C functions via SWIG/JNI

霸气de小男生 提交于 2019-12-04 14:57:52

Without being given any extra information SWIG defaults to assuming you want things wrapped as a type that can be returned and passed from one function to another, even if it can't be wrapped meaningfully in the target language.

Every time you see a type that begins SWIGTYPE_... it means SWIG didn't know how to generate a better wrapper and this was the best it managed to come up with.

SWIG does provide default typemaps that can help you here though:

  • For setPhy_idx(uint32_t value); all you need to add in your interface is:

    %include "stdint.i"
    
    void setPhy_idx(uint32_t value);
    

    and a type which can represent a uint32_t will be used in the target language.

  • For setId(unsigned char *value); it depends quite what value actually is - if it's a NULL terminated string you can do something like:

    %apply char * { unsigned char * };
    
    void setId(unsigned char *value);
    

    and a String will be used in your target language.

    If you wanted to pass the pointer as an integer type you could use something like:

    %apply unsigned long { unsigned char * };
    
    void setId(unsigned char *value);
    

    instead.

    If unsigned char *value is a pointer to a single unsigned char you could do:

    %include "typemaps.i"
    %apply unsigned char *INPUT { unsigned char *value };
    
    void setId(unsigned char *value);
    

    which instructs SWIG to treat the poitner as a single pointer. (This could be applied for the uint32_t if it was a pointer too)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!