I'm developing a system in java and a part of that system needs to get data coming from another system in Delphi.
I created two functions one that returns a String and another that returns an int . The function that returns an int works perfectly, but I'm having trouble with the function that returns a String . in Delphi it works perfectly, since when I call it in Java it gives the following error:
Exception in thread "main" java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokePointer(Native Method)
at com.sun.jna.Function.invokePointer(Function.java:471)
at com.sun.jna.Function.invokeString(Function.java:652)
at com.sun.jna.Function.invoke(Function.java:396)
at com.sun.jna.Function.invoke(Function.java:316)
at com.sun.jna.Library$Handler.invoke(Library.java:232)
at com.sun.proxy.$Proxy0.testeString(Unknown Source)
at br.com.Teste.TesteDLL.main(TesteDLL.java:20)
//Classe Java que estende de StdCallLibrary
import com.sun.jna.Native;
import com.sun.jna.win32.StdCallLibrary;
public interface LeituraDLL extends StdCallLibrary {
LeituraDLL leituraDLL = (LeituraDLL) Native.loadLibrary(
"MinhaDLL", LeituraDLL.class);
public int teste(int valor);
public String testeString(String texto);
}
// Function em Delph
library MinhaDLL;
uses
SysUtils,
Classes;
{$R *.res}
function teste(v:Integer):Integer; stdcall;
begin
Result := v+5;
end;
function testeString(v:Integer):String; stdcall;
begin
Result := 'TesteString Ok';
end;
exports teste;
exports testeString;
end.
//Chamada da Classe que estende de StdCallLibrary
import com.sun.jna.Native;
public class TesteDLL {
public static void main(String[] args) {
LeituraDLL leituraDLL = (LeituraDLL) Native.loadLibrary(
"MinhaDLL", LeituraDLL.class);
int teste = leituraDLL.teste(10);
String testeString = leituraDLL.testeString("Ola Mundo");
System.out.println("Retorno Function Teste: " + teste); // Retorna o valor: 15
System.out.println("Retorno Function TesteString: " + testeString); // Exibe o erro Invalid memory access
}
}
Has anyone ever had this problem and knows how to solve it? Note: I'm using JNA.