JNA将等效的Swift指针从Java Android传递给C [英] JNA passing the equivalent of swift pointer from Java Android to C

查看:45
本文介绍了JNA将等效的Swift指针从Java Android传递给C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将Java中的这个Swift代码实现传递给C时,我有点受阻.我将使用JNA在Java和Matlab生成的C代码之间进行集成的任何建议,将不胜感激.

I am a little stuck trying to pass an implementation of this Swift code in Java, to C. Would appreciate any advice i am using JNA to integrate between the Java and Matlab generated C code.

这是Swift调用:

GetResult(uptime, &result_out)

它映射到C代码:

void GetResult(double time, Result_t *Result)

对于这里的C结构Result_t太大,但是它是double和int的组合.

The C struct Result_t is too big for here, however it is a combination of double's and int's.

我知道& result_out是快速指针吗?如何最好地实现与Java中的Swift指针等效的功能,然后将其传递给C,该C需要一个指向Result_t的指针,正如我提到的那样,Result_t是由双精度和整数组成的结构.

I understand &result_out is a swift pointer? 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.

推荐答案

&表示变量的内存地址.在这种情况下,它表示Result结构的内存地址.

The & denotes the memory address of a variable. In this case, it means the memory address of the Result structure.

在C语言中,您可以看到*符号用于表示同一件事:作为第二个参数传递的变量是基础数据的引用/内存地址.

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.

这些通常成对出现:如果定义了变量foo,则可以轻松地将&foo传递给任何需要指向foo指针的方法,而该方法本身使用*表示法来告知您它需要那个指针.

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.

对于许多JNA结构,您需要密切注意这些符号.例如,知道是否通过intIntByReference是关键.但是幸运的是(为了便于编写代码,或者很不幸地是为了保持一致性),JNA Structure类的行为是不同的.最重要的是,默认情况下,当包含Structure作为方法/函数(最常见的应用程序)的参数时,将使用该指针或该结构的ByReference版本.因此,在这种特殊情况下,假设您已将class Result extends Structure与所有信息映射在一起,则可以简单地将result传递给result = new Result();.

For many JNA structures you need to pay close attention to these symbols. Knowing whether to pass an int or IntByReference, for example, is key. But fortunately (for ease of writing code, or unfortunately for consistency) the behavior of the JNA Structure class is different. 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. 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();.

有些方法可以使用ByReferenceByValue覆盖此默认行为,但这超出了您的问题范围.

There are ways to use ByReference and ByValue to override this default behavior but that's beyond the scope of your question.

这篇关于JNA将等效的Swift指针从Java Android传递给C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆