简体   繁体   中英

Passing string writable array from Java to C (using JNA)

I use JNA to call ac 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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM