Search code examples
javacjava-native-interfaceswig

how to use SWIGTYPE_p_CHAR class in my main class?


I have used typemaps :

%include "typemaps.i"

%apply SWIGTYPE * {char *deci};

This is the proxy class generated by SWIG:

public class SWIGTYPE_p_char {
    private long swigCPtr;

    protected SWIGTYPE_p_char(long cPtr, boolean futureUse) {
        swigCPtr = cPtr;
    }

    protected SWIGTYPE_p_char() {
        swigCPtr = 0;
    }

    protected static long getCPtr(SWIGTYPE_p_char obj) {
        return (obj == null) ? 0 : obj.swigCPtr;
    }
}

Solution

  • it seems trivial, but have you tried:

    SWIGTYPE_p_char my_p_char;
    

    after all, all that the class contains is a long int representation of the pointer address

    clearly you can only use this for a c function that returns a char* or uses a char* parameter as a pseudo return value

    if you want to use a char* input parameter then you'll most likely have to write a helper function of some sort

    I wrapped this function prototype with SWIG recently:

    int load_config(const char *fn, Config *cfg);
    

    which loaded data into my Config data-structure from a file specified by a string (char*)

    I was able to call it from Java with the following line:

    example.load_config("test.cfg", cfg);
    

    or alternatively:

    String cfg_file = "test.cfg";
    example.load_config(cfg_file, cfg);