[英]JNA passing the equivalent of swift pointer from Java Android to C
I am a little stuck trying to pass an implementation of this Swift code in Java, to C.我在尝试将 Java 中的 Swift 代码的实现传递给 C 时有点卡住了。 Would appreciate any advice i am using JNA to integrate between the Java and Matlab generated C code.如果我使用 JNA 在 Java 和 Matlab 生成的 C 代码之间集成,我将不胜感激。
This is the Swift call:这是 Swift 调用:
GetResult(uptime, &result_out)
It Maps to the C code:它映射到 C 代码:
void GetResult(double time, Result_t *Result)
The C struct Result_t is too big for here, however it is a combination of double's and int's. C 结构 Result_t 在这里太大了,但它是 double 和 int 的组合。
I understand &result_out is a swift pointer?我了解 &result_out 是 swift 指针? How is it best to go about implementing the equivalent of the Swift pointer in Java and then be able to pass it to C that is expecting a pointer to the Result_t which as i mentioned is a structure made up of doubles and ints. How is it best to go about implementing the equivalent of the Swift pointer in Java and then be able to pass it to C that is expecting a pointer to the Result_t which as i mentioned is a structure made up of doubles and ints.
The &
denotes the memory address of a variable. &
表示变量的 memory 地址。 In this case, it means the memory address of the Result
structure.在这种情况下,它表示Result
结构的 memory 地址。
In C you can see the *
symbol used to denote the same thing: the variable to be passed as the second argument is the reference/memory address of the underlying data.在 C 中,您可以看到用于表示同一事物的*
符号:作为第二个参数传递的变量是基础数据的引用/内存地址。
These often appear in pairs: if you have a variable foo
defined you can easily pass &foo
to any method expecting the pointer to foo
, while the method itself uses the *
notation to let you know it requires that pointer.这些通常成对出现:如果您定义了一个变量foo
,您可以轻松地将&foo
传递给任何期望指向foo
的指针的方法,而该方法本身使用*
表示法让您知道它需要该指针。
For many JNA structures you need to pay close attention to these symbols.对于许多 JNA 结构,您需要密切注意这些符号。 Knowing whether to pass an int
or IntByReference
, for example, is key.例如,知道是传递一个int
还是IntByReference
是关键。 But fortunately (for ease of writing code, or unfortunately for consistency) the behavior of the JNA Structure
class is different.但幸运的是(为了便于编写代码,或者不幸的是为了一致性)JNA Structure
class 的行为是不同的。 The bottom line is that, by default, when a Structure
is included as an argument to a method/function (the most common application) the pointer, or ByReference
version of the structure is used.底线是,默认情况下,当Structure
作为方法/函数(最常见的应用程序)的参数包含时,将使用指针或结构的ByReference
版本。 So in this particular case, assuming you have mapped a class Result extends Structure
with all the information, you can simply pass result
where result = new Result();
因此,在这种特殊情况下,假设您已经映射了一个class Result extends Structure
以及所有信息,您可以简单地传递result
where result = new Result();
. .
There are ways to use ByReference
and ByValue
to override this default behavior but that's beyond the scope of your question.有多种方法可以使用ByReference
和ByValue
来覆盖此默认行为,但这超出了您问题的 scope 范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.