0

I want to use JNA to call a function written with C, how do I create a pointer to array of C-style strings, aka char**?

If I wrote my program in C, char** would be used like this:

param_names= (char ) malloc( 3000* sizeof( char * ) );

for( i = 0; i < 3000; i++ ) { 
    param_names[i] = GetName( i ); 
    fprintf( stderr, "%s\n", param_names[i] );
} 

if( light( &param_names[1],1, &numPoints, &data ) != SUCCESS

Here, light is a function implemented in C and uses C calling conventions.

How can I write code that allocates these strings and array in Java and calls same light function implemented in C?

How to allocate C-style strings in Java/JNA?

How to allocate and assign C-style arrays in Java/JNA?

What type should I use to represent pointer to such array, aka char** in Java/JNA?

when i try to do string[] as argument (in java) i have this error :

Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:383)
at com.sun.jna.Function.invoke(Function.java:315)
at com.sun.jna.Library$Handler.invoke(Library.java:212)
at $Proxy0.light(Unknown Source)
at ApicallI.getpar.main(getpar.java:429)
BenMorel
  • 34,448
  • 50
  • 182
  • 322
user3419252
  • 61
  • 1
  • 6
  • What have you tried so far ? Note : the equivalent of `char*` is a String in Java. So `char**` is a `String[]` (Or if you really want to work with arrays : `char[][]`). – Arnaud Denoyelle Mar 31 '14 at 09:03
  • Can you explain `jna` tag? Do you mean to create a bunch of C strings using Java JNA same as C code does that you plan to pass to some library that requires a C-tyle array? Or are you looking to create an array of Java string and give C code as an example? – Dima Tisnek Mar 31 '14 at 09:07
  • Thank you all for your speed responses, sorry i am from france so i have not good english. to @ArnaudDenoyelle, in fact i use JNA to call code C, i have many functions that i call with success, but this one i can't call it, this function in c is writed lik this : light(char** paramnames, int num param, int* nump , structdata***data). whene i call this function in java i have error that it dont found the parameter and this parameter is valid. the problem i think in char** – user3419252 Mar 31 '14 at 09:32
  • @qarma, thank you, i tried to create code JAVA to call fuctions writed in c, using JNA, as i explained to arnaud, i don't now how i can convert char** in java – user3419252 Mar 31 '14 at 09:35
  • Just to be clear, did you choose JNA over JNI (similar but not same) for some specific reason, or was that your first guess? Key differences are described here http://stackoverflow.com/a/1556637/705086 – Dima Tisnek Mar 31 '14 at 14:41
  • Question edited, please reopen. – Dima Tisnek Mar 31 '14 at 14:41
  • hi, thank you for the editing, i use JNA, after have did some researche, i think i have a problem with encoding – user3419252 Apr 02 '14 at 12:33
  • @user3419252 if you provide some details on the function you want to call, `light`, the question will be easier to understand – Dima Tisnek Apr 02 '14 at 15:14

0 Answers0