1
votes

I need to develop a Java program with a Delphi DLL, but a problem is:

I mapped in JNA as follows:

- Delphi function:

function send_command (const command : byte; var size : byte; var data : pbyte) : integer; stdcall external 'comunication.dll';

- Java function:

public int send_command(byte command, ByteByReference size, PointerByReference data);

The function returned 1 (Success result) but I can´t get the value in data variable.

How can I get the value in data ?

In Delphi Test Program, the function is called as follows:

const
    cmdREAD_STATUS = $17;

procedure TForm1.ReadStatus1Click(Sender: TObject);
var
    Data,
    p    : pbyte;
    tam  : byte;
    i,
    result: integer;
    saux : string;
begin
    getmem (Data, 40);
    tam:= 0;
    result:= send_comand (cmdREAD_STATUS, tam, Data);
    if result= 1 then
      begin
        memo1.Lines.add ('Command OK');
        i:= 0;
        sAux:= '';
        p:= Data;
        while (i < Tam) do
          begin
            sAux:= sAux + inttohex (p^, 2)+' ';
            if length (sAux) > 59 then
              begin
                memo1.Lines.add (SAux);
                sAux:= '';
              end;
            inc (p);
            inc (i);
          end;
        if sAux <> '' then
          memo1.Lines.add (SAux);
      end
    else
      memo1.Lines.add ('Result: '+inttostr(result)+' in command execution');
    freemem (Data);
end;
1
what do you mean by "can´t get the value in data variable"? - CristiFati
This DLL speaks with an equipment and this function return a array of bytes, in Delphi returns as the example of test software. - Rafael Guerra
I'd still recommend fixing the delphi code - David Heffernan
@DavidHeffernan, changing the variables ? byte to BYTE and pbyte to PBYTE ? - Rafael Guerra
What code is not working exactly? The Delphi code that populates data, or the Java code that tried to read from data? You need to be more specific. - Remy Lebeau

1 Answers

1
votes

Do it using the Memory class.

Memory memory = new Memory(40);
PointerByReference data = new PointerByReference();
data.setValue(memory);
// call the function now
byte[] bytes = new byte[40];
memory.read(0, bytes, 0, 40);

Bear in mind that I don't know Java at all and so there may well be syntax errors in the above. But I'm confident that the basic approach is sound.