How to match this Delphi function with JNA

I have the following Delphi function:

function DoX(const InputBuffer: Pointer; const InputBufferSize: longword; OutputBuffer: Pointer; var OutputBufferSize: longword): longbool; 

OutputBuffer and OutputBufferSize will be set in the function as part of the result, with a logical return to indicate whether the method was successful (InputBuffer and OutputBuffer will be byte arrays).

I managed to display some of my required functions from the dll using JNA and they work fine, however this question gives me problems, any help would be appreciated.

+4
source share
1 answer

Most JNA docs assume you are using C, not Delphi, so start with C equivalent to this function:

 int DoX(const void* InputBuffer, unsigned int InputBufferSize, void* OutputBuffer, unsigned int* OutputBufferSize); 

You will also want to qualify for the conference. By default, Delphi is a register, which is probably not the one you want. Use stdcall instead; this is what every other dll uses.

Java has no unsigned type equivalents for the ones you used, so start by ignoring the unsigned ones. This makes InputBufferSize a int . Your function returns a boolean result, so use boolean for the return type. JNA supports passing types by reference through children of the ByReference class, so use IntByReference for OutputBufferSize .

Finally, pointers. You said these are byte arrays, so I am puzzled by why you are not declaring them this way in your Delphi code. Either use PByte , or declare a new type of PByteArray and use this. (This change will make the implementation of this function more convenient.) In Java, try declaring them as arrays of bytes. So, the final product:

 boolean DoX(byte[] InputBuffer, int IntputBufferSize, byte[] OutputBuffer, IntByReference OutputBufferSize); 
+5
source

All Articles