How to map C++ char**, int* and Enum types in Java?

Davanum Srinivas


Answer by Alexander Krapf Re: How to map C++ char**, int* and Enum types in Java? Hi Vani,

it depends mostly on what the char** stands for. If it represents an array of character strings, your Java method should look like this:

  <method>( String[] arg1, IntHolder arg2, int arg3 )

In your native method you would have to extract the string bytes anf build a C-array of strings. The IntHolder assumes that you use this argument as an inout argument and that you have to read a result. IntHolder basically just wraps around a primitive int field. Enums map to ints or to enumeration classes. There are some articles available on how to represent enumeration types in Java, but that's is a whole different issue.

You might want to check out our JunC++ion product at http://www.codemesh.com. It would simplify the native method implementation drastically by allowing you to write code like this:

  <method>( ..., const String::array1D & arg1, IntHolder & arg2, jint arg3 )
  {
     char ** arr = new char* [ arg1.length ];
     int     inout = arg2.value;
     myenum  en = (myenum)arg3;

     ...

     call_cpp( arr, &inout, en );
  }

Much nicer than having to write all that JNI code...

Good luck,

Alex
Answer by vani hemmige Re: Re: How to map C++ char**, int* and Enum types in Java? Thanks Alex. You guessed it right. I had to use an array of Strings (actually the command line arguments). I tried using String[] on the Java side(as you have suggested) and on the C++ side, I got a jobjectarray from which I had to construct a char**. Yes, it involves using some jni functions. But it worked. Regarding the enum, I just passed an integer. (Anyway, the enum was not too big. It was just 0 or 1.) Thanks again. -Vani

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact