Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8593149
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:55:21+00:00 2026-06-11T23:55:21+00:00

I use JNA to call a c function from java. The function writes a

  • 0

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);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T23:55:23+00:00Added an answer on June 11, 2026 at 11:55 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application which makes use of JNA to call functions from a
How can I call a method with this method signature in C from JNA?
Edited Question: Trying to use a .dll file from Java using JNA. I've managed
I'm trying to use the librsync library in my java program trough the JNA.
I have a byte[] I captured from Kinect using OpenKinect and it's Java JNA
With Delphi I wrote a DLL which can be called from Java via JNA
JNA seems a fair bit easier to use to call native code compared to
I use the following code to listen to global key events: Win32HookManager.java import com.sun.jna.platform.win32.Kernel32;
I am using JNA and need to use the MapVirtualKeyEx function. Here is the
I am currently converting some OpenCV code from C++ to Java. I can't use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.