Sto lavorando per chiamare le funzioni da un file * .so compilato da Delphi da un programma Java. Dopo alcune ricerche sembra che JNA sia la strada da percorrere. Prima di immergermi in un codice Delphi complesso, sto provando a giocare con il codice "Hello World", ma sto avendo problemi nel trovare una stringa restituita da una funzione Delphi.Come posso chiamare una funzione Delphi che restituisce una stringa usando JNA?
Il codice Delphi (helloworld.pp):
library HelloWorldLib;
function HelloWorld(const myString: string): string; stdcall;
begin
WriteLn(myString);
Result := myString;
end;
exports HelloWorld;
begin
end.
compilo dalla riga di comando con "FPC helloworld.pp -Mdelphi", che produce libhelloworld.so.
Ora la mia classe Java:
import com.sun.jna.Library;
import com.sun.jna.Native;
public class HelloWorld {
public interface HelloWorldLibrary extends Library {
HelloWorldLibrary INSTANCE = (HelloWorldLibrary) Native.loadLibrary("/full/path/to/libhelloworld.so", HelloWorldLibrary.class);
String HelloWorld(String test);
}
public static void main(String[] args) {
System.out.println(HelloWorldLibrary.INSTANCE.HelloWorld("QWERTYUIOP"));
}
}
Tuttavia quando si esegue questo codice Java ottengo:
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007f810318add2, pid=4088, tid=140192489072384
#
# JRE version: 7.0_10-b18
# Java VM: Java HotSpot(TM) 64-Bit Server VM (23.6-b04 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [libhelloworld.so+0xbdd2] HelloWorld+0x6fea
Nota che se cambio il mio metodo Delphi (e l'interfaccia Java associata) per tornare un intero hardcoded, tutto funziona alla grande: la stringa che passo viene stampata e ottengo l'int back come previsto.
Stranamente, se il metodo Delphi restituisce un carattere, devo scrivere il mio proxy JNA come restituendo un byte e trasferirlo manualmente in char (se dichiaro che la mia interfaccia restituisce un carattere, stampa un carattere inutile).
Qualche idea di cosa sta andando storto qui?
FYI, Sono su Ubuntu 12.04, 64 bit, utilizzando Sun JDK 1.7.0_10-b18, JNA 3.5.1 e Free Pascal Compiler versione 2.4.4-3.1.
È possibile restituire altri 'String's dalla funzione nativa rispetto a quella passata? – JimmyB
@HannoBinder: Ottengo lo stesso errore se cambio il mio codice Delphi per fare "Risultato: = 'HELLO';". :( –