java JNA 结构和指针映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/494325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:38:45  来源:igfitidea点击:

JNA Struct and Pointer mapping

javajna

提问by Brian

How does one map the function below to java?

如何将下面的函数映射到java?

VOID WriteToStruct(BOOL *Status, STRUCT_MSG RecBuff)

VOID WriteToStruct(BOOL *Status, STRUCT_MSG RecBuff)

What this function does:
1) Populates the struct RecBuff
2) Updates status

此函数的作用:
1) 填充结构 RecBuff
2) 更新状态

How do I map to a boolean pointer in Java and access the struct data updated by the function?

如何映射到 Java 中的布尔指针并访问由函数更新的结构数据?

回答by Brian

I was searching for another issue concerning JNA and structs, and Google redirected me here. I hope this helps.

我正在寻找关于 JNA 和结构的另一个问题,谷歌将我重定向到这里。我希望这有帮助。

From JNA API

来自JNA API

To pass a structure by value, first define the structure, then define an empty class from that which implements Structure.ByValue. Use the ByValue class as the argument or return type.

// Original C code
typedef struct _Point {
  int x, y;
} Point;

Point translate(Point pt, int dx, int dy);

// Equivalent JNA mapping
class Point extends Structure {
    public static class ByValue extends Point implements Structure.ByValue { }
    public int x, y;
}
Point.ByValue translate(Point.ByValue pt, int x, int y);
...
Point.ByValue pt = new Point.ByValue();
Point result = translate(pt, 100, 100);

要按值传递结构,首先定义结构,然后从实现 Structure.ByValue 的类中定义一个空类。使用 ByValue 类作为参数或返回类型。

// Original C code
typedef struct _Point {
  int x, y;
} Point;

Point translate(Point pt, int dx, int dy);

// Equivalent JNA mapping
class Point extends Structure {
    public static class ByValue extends Point implements Structure.ByValue { }
    public int x, y;
}
Point.ByValue translate(Point.ByValue pt, int x, int y);
...
Point.ByValue pt = new Point.ByValue();
Point result = translate(pt, 100, 100);

回答by Brian

You can use the ByReference class to pass values by reference. Presuming BOOL is an int you can use IntegerByReference.

您可以使用 ByReference 类通过引用传递值。假设 BOOL 是一个整数,您可以使用 IntegerByReference。