Question

I am trying to call AutoItX (a closed-source .dll library) from Java using JNA. I used wikipedia, this blog, plus various posts on here to help me through this difficult time.

I started with the .h file and translated that to a Java interface, and started building out all the easy methods - ones that take only int or WString as arguments. Being the paranoid type, I was also building up unit tests to prove that everything is working. All my source can be found on sourceforge.

The problems start when I work with some of the methods that return stuff in one of the passed arguments.

From the header file, I took:

AU3_API void WINAPI AU3_WinGetText(LPCWSTR szTitle, LPCWSTR szText, LPWSTR szRetText, int nBufSize);

The szRetText is where I am going to get my value back. In Java I translated this to:

import com.sun.jna.Memory;
import com.sun.jna.WString;
void AU3_WinGetText(WString szTitle, WString szText, Memory szRetText, int nBufSize);

and I try to call this with:

import com.sun.jna.Memory;
import com.sun.jna.WString;
public String winGetText(String szTitle) {
    Memory szRetText = new Memory(256);
    autoItX.AU3_WinGetText(new WString(szTitle), new WString(""), szRetText, 255);
    return szRetText.getString(0, true);
}

When I try to run this in my unit test by itself, it works fine. When I run the entire suite, the test immediately following this one always crashes the JVM. I tried a few different methods with similar signature, with the same results.

Any advice where to next?

Please note that I am aware of jwinauto (and there may be others); however, I am doing this as a learning exercise.

Was it helpful?

Solution

From @technomage advice, I ended up using:

import com.sun.jna.Memory;
import com.sun.jna.WString;
public String winGetText(String szTitle) {
   Memory szRetText = new Memory(2 * 256);
   autoItX.AU3_WinGetText(new WString(szTitle), new WString(""), szRetText, 256);
   return szRetText.getString(0, true);
}

The only change is to allocate double the Memory(), everything else is the same. I felt this approach was more JNA-friendly than using char[].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top