tags:

views:

629

answers:

4

I have one dll of cpp and i need to call its function which returns char*. Im using String in native Declaration but getting out put like ???? or some crap thing. I just want to knw that do i have to decode the String.i have already set my system property like System.setProperty("jna.encoding","UTF-8"); Im in big mess. Hope to get Some Positive replies from u guys. Thanks in Advance.. Cheers...!

A: 

jna.encoding will have no effect if the original string is unicode. You need to use com.sun.jna.WString instead, or see the example W32API mappings for how to automatically map unicode (wchar_t*) strings to String.

A: 

Did you use Native.toString() to convert the string back to Java?

Rusty
A: 

Try using a Pointer instead of String and then use the getString(long offset) method of pointer to get the string, it implicitly uses the jna.encoding if set.

Aravias
A: 

char* Return:

Pointer myFunc();
Pointer ptr = myFunc();
String str = ptr.getString(0);
str = str.substring(0, str.indexOf(0));  //Remove garbage after null char

char* Param:

void myFunc(Pointer ptr);
String str = "hello world";
Memory mem = new Memory(str.length());
memory.write(0, str.getBytes(), 0, str.length());
myFunc(mem);
Nick