简体   繁体   English

如何使用JNA将带有FAR PASCAL的结构与array [1 + 1]和方法从C映射到Java?

[英]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. 我需要创建Java接口以从DLL调用方法。 I'm not sure if the java code below is correct. 我不确定下面的Java代码是否正确。

I'm already using JNA 5.4 and Java 11. 我已经在使用JNA 5.4和Java 11。

Struct in C: 在C中构造:

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

Method in C: C语言中的方法:

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

Should it looks like this? 应该看起来像这样吗?

Java class Java类

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 Java方法

public int DG18_Search(Pointer pointer); 

What's exactly array[1+1] and FAR PASCAL means in C? 在C中,array [1 + 1]和FAR PASCAL到底是什么意思?

Welcome to StackOverflow, JustCoding . 欢迎使用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. 您看到的+1只是简单的加法,因此您需要在字节数组的值上加1才能正确映射。 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. 我怀疑+1的目的是为了显示一定数量的1字节字符的空间,如果需要的话,还可以为空终止符加上一个空格。

Your Java syntax for the mapping is wrong, though, you need to declare the array type, so the correct mapping is: 但是,您映射的Java语法错误,但是您需要声明数组类型,因此正确的映射是:

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 . (或者,您可以根据需要保留8 + 1样式编号。)请注意,要扩展的JNA类是Structure而非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. 至于功能/方法映射,您可以阅读有关FAR PASCAL的Fifi链接,但是除非您是为真正的古老操作系统而构建,否则可以放心地忽略它们。 While a Pointer would work for the mapping, it would allow you to pass any Pointer or Structure to the method. 虽然Pointer适用于映射,但它允许您将任何Pointer或Structure传递给方法。 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: 最好使用类型安全性并将Cars变量放入该函数调用中:JNA将在方法参数中自动使用结构的指针( Cars.ByReference ),并将结果自动读取到本地变量中,因此:

public int DG18_Search(Cars cars); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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