简体   繁体   中英

How to map struct wtih array[1+1] and method with FAR PASCAL from C to Java using JNA?

I need to create Java interface to invoke methods from DLL. I'm not sure if the java code below is correct.

I'm already using JNA 5.4 and Java 11.

Struct in C:

typedef struct
{
    char CarId[8 + 1];
    char CarCode[5 + 1];
    char PrimaCarStatus[24 + 1];
}Cars;

Method in C:

extern "C" __int32 _stdcall FAR PASCAL  DG18_Search(Cars *CarsData);

Should it looks like this?

Java class

public class Cars extends Struct {
    public byte carId[] = new byte[8];
    public byte carCode[] = new byte[5];
    public byte primaCarStatus[] = new byte[24];
}

Java method

public int DG18_Search(Pointer pointer); 

What's exactly array[1+1] and FAR PASCAL means in C?

Welcome to StackOverflow, JustCoding .

The +1 you see is just simple addition, so you need to add one to the values of your byte arrays to map correctly. I suspect the +1 is intended to show that there is room for a certain number of 1-byte characters, plus one space for a null terminator if required.

Your Java syntax for the mapping is wrong, though, you need to declare the array type, so the correct mapping is:

public class Cars extends Structure {
    public byte[] carId = new byte[9];
    public byte[] carCode = new byte[6];
    public byte[] primaCarStatus = new byte[25];
}

(Or you can preserve the 8 + 1 style numbers if you like.) Note the JNA class to extend is Structure not Struct .

As for the function/method mapping, you can read Fifi's link about FAR PASCAL, but unless you're building for really ancient operating systems you can safely ignore them. While a Pointer would work for the mapping, it would allow you to pass any Pointer or Structure to the method. It is better to use type safety and put the Cars variable in that function call: JNA will automatically use the structure's pointer ( Cars.ByReference ) in the method argument, and auto-read the results into your local variable, so:

public int DG18_Search(Cars cars); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM