4 messages in net.java.dev.jna.users[jna-users] C strings as out parameter
FromSent OnAttachments
Vito IngrassiaJul 12, 2007 3:19 am 
Wayne MeissnerJul 12, 2007 6:02 am 
Sébastien BratièresJul 12, 2007 8:33 am 
Timothy WallJul 12, 2007 11:41 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:[jna-users] C strings as out parameterActions...
From:Sébastien Bratières (seba@voice-insight.com)
Date:Jul 12, 2007 8:33:18 am
List:net.java.dev.jna.users

Following up on Vito's question, I also find that WStrings can be passed as input arguments to functions, but will not work as output arguments. I am not speaking of using them as return values here, but as output arguments. I think it would be convenient to have a utility class that would wrap a String and would work both as an in and out argument.

Here is my test case, using revision 158:

My C program reads so (omitting includes etc):

extern "C" { _declspec(dllexport) void __cdecl test(TCHAR * in, TCHAR* out, TCHAR * out_WString) { printf("in: %S \n", in); printf("out: %S \n", out); printf("out_WString: %S \n", out_WString); wcscpy(out, _T("Hélène")); wcscpy(out_WString, _T("Hélène")); printf("after strcpy, out: %S \n", out); printf("after strcpy, out_WString: %S \n", out); } }

Note that test returns void, but that it modifies both out and out_WString in-place. On my platform TCHAR compiles to wchar_t. I compile this to a DLL, which I then access using JNA.

My JNA interface for the library has: void test(WString in, char[] out, WString out_WString);

My Java test code for this interface is, after initializing the lib variable:

WString in = new WString("Sébastien"); char[] out = (char[]) Array.newInstance(char.class, 250); WString out_WString = new WString(new String((char[]) Array.newInstance(char.class, 250)));

System.out.println("before test in:" + in); System.out.println("before test out:" + new String(out)); System.out.println("before test out_WString:" + out_WString); lib.test(in, out, out_WString); System.out.println("after test in:" + in); System.out.println("after test out:" + new String(out)); System.out.println("after test out_WString:" + out_WString);

I need to fill the char[] to allocate sufficient memory space for whatever comes in later.

Output is:

before test in:Sébastien before test out:------etc before test out_WString:------etc in: Sébastien out: out_WString: after strcpy, out: Hélène after strcpy, out_WString: Hélène after test in:Sébastien after test out:Hélène------etc after test out_WString:------etc

where I have replaced the sequence of \u0000 characters by ------etc

As you can see, the WString does not contain the string that I would like to see returned ("Hélène"). Is my workaround the best available, or is this a bug in WString, or elsewhere ?

Thank you, Sebastien