Passing String Arguments to DLL Functions

Strings that are passing into a DLL function or that are returned by a DLL function are treated by default as Unicode Strings. If your DLL function requires ANSI String arguments, use the CharacterSet property of the DllFunctionOptions attribute.


@Dll( "user32.dll" )
public interface IUserDll32Functions {
  int SendMessageA(TestObject obj, int message, int wParam, Object lParam);

Passing a String back from a DLL call as an OutArgument works per default if the String's size does not exceed 256 characters length. If the String that should be passed back is longer than 256 characters, you need to pass an InOurArgument with a String in that is long enough to hold the resulting String.


Use the following code to create a String with 1024 blank characters:
char[] charArray = new char[1024];
Arrays.fill(charArray,' ');
String longEmptyString = new String(charArray);
Pass this InOutArgument as an argument into a DLL function and the DLL function will pass back Strings of up to 1024 characters of length.

When passing a String back from a DLL call as a function return value, the DLL should implement a DLL function called FreeDllMemory that accepts the C String pointer returned by the DLL function and that frees the previously allocated memory. If no such function exists the memory will be leaked.