Ron
Ron

Reputation: 1

pass string by reference when use JNA to access .dll

I need to use JNA to call dll from Java.

the definition of the function in the header file of the dll that I need to invoke is like this:

bool DmgrGetVersion(char * szVersion);

So I need to redefine an interface in Java, and the function becomes:

boolean DmgrGetVersion(String szVersion);

But, I need pass string by reference, which means my pass-in string variable needs to recieve a new value from the argument of the function. How do I achieve this? (for example if I call DmgrGetVersion(ver) and szVersion in the function is assigned "1.0.1" at the end of the function, ver needs to be assigned "1.0.1" as well)

I heard many people said that String[] str = new String[1], StringBuilder or StringBuffer can be used, but not for my case because I cannot change the content of the function since i do not have the source code of the dll. (all I have is the header file and .lib and .dll files)

BTW in the JNA documentation there's no type conversion for char*. (only char and const char* and char**)

So is it still possible for me to achieve my need? thanks guys

Upvotes: 0

Views: 1318

Answers (2)

technomage
technomage

Reputation: 10069

Pass in an instance of PointerByReference, then use PointerByReference.getValue() to retrieve the "returned" pointer. Pointer.getString(0) will then provide the referenced string.

While String[] may work, it leaves you with no way of referencing the "returned" pointer, should you need to free it.

Upvotes: 0

sibnick
sibnick

Reputation: 4305

String is immutable object in Java. You can't change content of String object. String[] str = new String[1] is analog for char**

Upvotes: 1

Related Questions