Passing a string in Java (that needs to be modified) to a DLL function that is written in C++ and accessed using JNA

I should note that I am very much a beginner in Java and I am suppose to transfer a project I have written in Delphi to Java. I need to use a DLL that came with the hardware I am using. The DLL have functions that gets and sends information from and to the device which are all written in C++. The device is essentially a controller of some sort. I am using the Java Native Access (JNA) to access and map the DLL functions to Java which is handled automatically for the most part.

The DLL have few functions which requires a string to be passed as a parameter and the function is supposed to modify this string. Here is an example function as described in the DLL documentation file.

int GetVersionStr(char *pcVers, int lMaxLen);

The function takes a string as a parameter, modifies this string with the current version of the device and I can use that string wherever I want. I have initially achieved this while writing this project in Delphi by just passing a string. However in Java, as I have found out, strings are immutable. I learned that usually StringBuilder gets the job done. However JNA can’t seem to match StringBuilder to the C++ equivalent class of string or char*. If I could just interfere with JNA’s class matching code to do something like this

my_c++_string = my_java_string_build.toString()

and equalize my StringBuilder object to the C++ string object using the “toString()” method of the StringBuilder, the problem would most likely be solved. But I don’t really have a clue how to do this. Any and all solutions would be welcome. Thanks in advance.

Source: dll

Leave a Reply