So, I\'m trying to port a C library (libnfc) to Java using SWIG.
I\'ve got to the point of having a compiled shared library, and a basic \"nfc_version()\" method cal
So Flexo's answer is the proper way to solve this problem but SWIG also offers the "cpointer.i" module (described here: http://www.swig.org/Doc1.3/SWIGDocumentation.html#Library_nn3) which allowed me to hack together a quick solution so that I could test I had the basic library working. I thought i would put in this answer just for completeness and offer an alternative to anyone who stumbles across this question.
The need for this is because of what I would describe as an asymmetry in what is generated by SWIG. A base type object has a property, swigCPtr, which is the memory address of that object (a "self pointer"). Then to make a pointer, you just get swigCptr from the base type and pass it in the constructor for the pointer type (SWIGTYPE_p_X) that swig generated. BUT the pointer type ONLY exists in Java and just holds the swigCptr value. There is no 'c' block of memory holding that pointer. Hence, there is no equivalent of the swigCPtr in the pointer type. Ie there is no self pointer stored in the pointer type, in the same way that there IS a self pointer stored in the base type.
So you can't make a pointer to a pointer (SWIGTYPE_p_p_X) as you dont have an address of the pointer to pass it when constructing it.
My new '.i' file is as follows:
%module nfc
%{
#include
#include
#include
%}
%include
%include
%include
%include "cpointer.i"
%pointer_functions(nfc_context*, SWIGTYPE_p_p_nfc_context)
What this last macro does is provide 4/5 functions for making pointers. The functions all take and return the types that swig should already have generated. The new The usage in Java to get the nfc_init and nfc_open commands to work:
SWIGTYPE_p_p_nfc_context context_p_p = nfc.new_SWIGTYPE_p_p_nfc_context();
nfc.nfc_init(context_p_p);
//get the context pointer after init has set it up (the java doesn't represent what's happening in the c)
SWIGTYPE_p_nfc_context context_p = nfc.SWIGTYPE_p_p_nfc_context_value(context_p_p);
SWIGTYPE_p_nfc_device pnd = nfc.nfc_open(context_p, null);
notice that I have to get the pointer from the double pointer after the init command has completed, as the information stored in the java pointer object is separated from the C 'world'. So retrieving the 'value' of context_p_p will give a context_p with the correct value of its pointer.