1

I use JNA to call a c function from java. The function writes a list of strings to user provided memory and its signature reads:

void c_getStrings(char *buf, size_t bufSize, char *strings[], size_t *stringsCount)

with the Java version:

public interface TestCaseDLL extends Library
{
    int c_getStrings(byte[] buf, int bufSize, Memory strings, IntByReference stringCount);
}

public class TestCase
{
    public static void main(String[] args)
    {
        byte[] buf = new byte[100];
        Memory strings = new Memory(Memory.SIZE * 10);
        IntByReference stringCount = new IntByReference(10);

        // c_getStrings() will write the strings continuously to 'buf' and
        // additionally return a list of starting addresses through the
        // 'strings' parameter (that is 'strings' point into 'buf').
        // 'stringCount' holds the initial array size of 'strings' and will
        // return the count of returned strings.
        TestCaseDLL.INSTANCE.c_getStrings(buf, buf.length, strings, stringCount);

        System.out.println(strings.getPointer(0).getString(0));
        System.out.printf("%c\n", buf[0]);  // how can this line change 'strings'?
        System.out.println(strings.getPointer(0).getString(0));

        for (byte b: buf) {
            System.out.print((char) b);
        }
        System.out.println("");
        for (byte b: buf) {
            System.out.printf("%#x ", b);
        }
        System.out.println("");
    }
}

Output

??llo world!
H
?

Hello world! Hallo Welt! Ciao a tutti!
0x48 0x65 0x6c 0x6c 0x6f 0x20 0x77 0x6f 0x72 0x6c 0x64 0x21 0x0 0x48 0x61 0x6c 0x6c 0x6f 0x20 0x57 0x65 0x6c 0x74 0x21 0x0 0x43 0x69 0x61 0x6f 0x20 0x61 0x20 0x74 0x75 0x74 0x74 0x69 0x21 0x0 ...

I ran into the following problems:

  • The returned string is broken. It should return "Hello World!" instead of "??llo world!"
  • Printing buf[0] changes the returned string. I have now clue what is happening here, since I am only reading its value.

Is my type mapping broken or am I missing something fundamental?

Update

I now use use

void c_getStrings(Memory buf, int bufSize, String[] strings, IntByReference stringCount);

If I would redo it, I would split it into two functions as suggested by technomage:

void c_fill(char *buf, size_t bufSize);
void c_parseToStringArray(const char *buf, const char *strings[], size_t stringsSize);
3
  • @technomage What C API would you use to return a list of strings? Commented Sep 27, 2012 at 7:23
  • Currently c_getString doesn't allocate memory (which I think is a good thing). Although returning only the buffer buf is sufficient, having the string array strings simplifies using the function. Do you disagree? Commented Sep 27, 2012 at 7:32
  • I'd split it in two, one function to fill the buffer with strings separated by a given token, then another to parse the buffer into an array of strings. Commented Sep 27, 2012 at 13:35

1 Answer 1

1

First several technical points:

  • Since your third argument references the first, you cannot use a primitive array. Since it explicitly saves native pointers, it'll be awkward to use an NIO buffer. That leaves you with Memory. If, however, you just need the resulting String values and don't care about the pointers themselves, then byte[], NIO buffers, or Memory will work, provided the third argument is of type String[].
  • Your third argument appears to be intended to be a writable array of pointers. You could use Pointer[] or String[]. You can also use Memory, provided it's allocated big enough to hold as many pointer values as would be written.

Then the larger questions:

  • Why do you need both a buffer (presumably with embedded NUL characters) and individual pointers into that buffer?
  • When mapped from JNA, this just looks silly since JNA does its own string allocation. If you're just providing the memory buffer as pooled string storage, you don't need it once you obtain the strings from the String[] third argument. If you're intending to manipulate stuff in the buffer, such changes won't be reflected in the "returned" strings.
Sign up to request clarification or add additional context in comments.

3 Comments

There was some success when changing to int c_getStrings(byte[] buf, int bufSize, String[] strings, IntByReference stringCount); ... String[] strings = new String[10]; TestCaseDLL.INSTANCE.c_getStrings(buf, buf.length, strings, stringCount); The result is still '??llo world!', but reading buf[0] no longer corrupts strings
I only care about the string array. The buffer is there, so c_getStrings doesn't have to allocate memory. (I don't intend to manipulate the buffer afterwards.)
You can't use byte[], you have to use Memory or an NIO Buffer. The native memory for byte[] referenced within the function is only valid for the duration of the native call, and the String[] conversion from the returned pointer array happens after.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.