0

I have created swig interface file to create JNI for my C++ Files. but some of my C++ Files include functions which accept pointer as argument like (void*) , C++ BOOL and Swig converts it into type like SWIGTYPE_p_int32_t how to pass such kind of data type from java ?

for example one of the function's actual prototype is like below in C++

DLL_API void DLL_CALLCONV FreeImage_Initialise(BOOL load_local_plugins_only FI_DEFAULT(FALSE));

which is converted in Java file with swig

public static void FreeImage_Initialise(SWIGTYPE_p_int32_t load_local_plugins_only)

how to pass such value from java ?

I have many classes which include such kind of arguments in function. is there any way so that I can apply solution to bulk files for handle datatype as simple as possible.

I have read one way to do that is create helper function but I cannot go for it because i have many c++ classes and for each and every function creating helper function for returning pointer is not way to go.

Please suggest any other way if possible.

Kirtan
  • 1,782
  • 1
  • 13
  • 35

4 Answers4

1

Solved myself. Swig was not be able to recognize all the typedef in header files and hence it was producing that kind of Types in wrapper. I need to redefine them in Interface file.

Kirtan
  • 1,782
  • 1
  • 13
  • 35
  • SWIG doesn't recurse into #include definitions by default, and you normally don't want it to. See my answer for letting SWIG know about Windows types such as BOOL. – Mark Tolonen Nov 15 '13 at 21:11
0

This is because of the weird declaration of BOOL type.

In this particular case, you can pass an int there from Java side. Either 0 or 1 will do the right job.

Or you can change BOOL to bool in your native code by creating a small wrapper for this library.

Sergey K.
  • 24,894
  • 13
  • 106
  • 174
  • I am unable to use 0,1 from java side in place of SWIGTYPE_p_int32_t. and What about case where function is returning SWIGTYPE_p_int32_t and void* kind of pointers ? – Kirtan Nov 14 '13 at 10:57
  • But you can patch this type to a simple ``int``. If your functions return ``void*`` you cannot do much with it in Java. However, you can store it in ``int`` and pass it back to native code where it accepts ``void*``. – Sergey K. Nov 14 '13 at 10:59
  • I can patch it for one function sir but I am trying to create wrapper of third party library which include these type at many places in the code not just single function. – Kirtan Nov 14 '13 at 11:05
  • You can ``#define SWIGTYPE_p_int32_t int`` – Sergey K. Nov 14 '13 at 11:27
0

About booleans, I would recommend using boolinstead of BOOL because JNI directly provides a type signature for it: Z (you can check signatures at: JNI Types and Data Structures)

About pointers, if are pointer to basic types, this is declared by adding [in front of corresponding signature, this is: int* --> [I. If it's a pointer to a own class, swig will create a fully qualified class.

Hope this helps.

jcm
  • 2,568
  • 14
  • 18
0

SWIG doesn't know about windows types. Add %include <windows.i> to the interface definition.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251