Problem Getting Back from Delphi DLL With Java

Asked

Viewed 378 times

1

I am developing a system in java and a part of this system needs to get data that comes from another system in Delphi.

I created two functions one that returns one String and another that returns a int. The function that returns a int works perfectly, but I’m having problems with the function that returns a String. in Delphi it works perfectly, already when I call it in Java 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 you know how to fix it? Note: I am using JNA.

  • In Delphi you are receiving an integer as a parameter (testeString(v:Integer)). Have you tried changing to String?

  • Bruno César, this isn’t really interfering. I managed to return Pchar, but then I have another problem is that in Java at the time of giving an input in the database, the special characters that comes from Delphi, it ignores and includes other totally different.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.