Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用JNA访问包含向量的结构<;字符*>; 我有一个C++库,它使用包含向量的结构。我很难确定通过JNA从Java访问它的正确方法 我的C++结构: #include <vector> struct topic { char* src_id; char* dest_id; int32_t num; std::vector<char*> names; }; #包括 结构主题{ char*src_id; 字符*dest_id; int32_t num; std::矢量名称; };_Java_C++_Java Native Interface_Jna - Fatal编程技术网

使用JNA访问包含向量的结构<;字符*>; 我有一个C++库,它使用包含向量的结构。我很难确定通过JNA从Java访问它的正确方法 我的C++结构: #include <vector> struct topic { char* src_id; char* dest_id; int32_t num; std::vector<char*> names; }; #包括 结构主题{ char*src_id; 字符*dest_id; int32_t num; std::矢量名称; };

使用JNA访问包含向量的结构<;字符*>; 我有一个C++库,它使用包含向量的结构。我很难确定通过JNA从Java访问它的正确方法 我的C++结构: #include <vector> struct topic { char* src_id; char* dest_id; int32_t num; std::vector<char*> names; }; #包括 结构主题{ char*src_id; 字符*dest_id; int32_t num; std::矢量名称; };,java,c++,java-native-interface,jna,Java,C++,Java Native Interface,Jna,我的Java类: public final class Topic extends Structure { public String src_id; public String dest_id; public int num; public String[] names; // This doesn't work public Topic() { } @Override protected List<String&g

我的Java类:

public final class Topic extends Structure {

    public String src_id;
    public String dest_id;
    public int num;
    public String[] names; // This doesn't work

    public Topic() {

    }

    @Override
    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { "src_id", "dest_id", "num", "names" });
    }

}
公共最终类主题扩展结构{
公共字符串src_id;
公共字符串dest_id;
公共整数;
公共字符串[]名称;//这不起作用
公共话题(){
}
@凌驾
受保护列表getFieldOrder(){
返回Arrays.asList(新字符串[]{“src_id”、“dest_id”、“num”、“names”});
}
}

这个解决方案对我很有效:

package com.example;

import java.util.Arrays;
import java.util.List;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.StringArray;
import com.sun.jna.Structure;

public class Example {

    public interface CLibrary extends Library {

        public static class Topic extends Structure {
            public static class ByReference extends Topic implements Structure.ByReference {
            }

            public String src_id;
            public String dest_id;
            public int num;

            public int numVals;
            public Pointer names; // char**

            @Override
            protected List<String> getFieldOrder() {
                return Arrays.asList(new String[] { "src_id", "dest_id", "num", "numVals", "names" });
            }
        }

        public void sendTopic(Topic.ByReference pVal);
    }

    public static void main(String[] args) {

        final CLibrary clib = Native.loadLibrary("example.dll", CLibrary.class);

        final String[] myArray = new String[5];
        myArray[0] = "one";
        myArray[1] = "two";
        myArray[2] = "three";
        myArray[3] = "four";
        myArray[4] = "five";

        CLibrary.Topic.ByReference ref = new CLibrary.Topic.ByReference();
        ref.numVals = 5;
        ref.names = new StringArray(myArray);

        clib.sendTopic(ref);
    }
}
package.com.example;
导入java.util.array;
导入java.util.List;
导入com.sun.jna.Library;
导入com.sun.jna.Native;
导入com.sun.jna.Pointer;
导入com.sun.jna.StringArray;
导入com.sun.jna.Structure;
公开课范例{
公共接口CLibrary扩展库{
公共静态类主题扩展结构{
公共静态类ByReference扩展了主题实现结构。ByReference{
}
公共字符串src_id;
公共字符串dest_id;
公共整数;
公共国际货币基金组织;
公共指针名称;//字符**
@凌驾
受保护列表getFieldOrder(){
返回Arrays.asList(新字符串[]{“src_id”、“dest_id”、“num”、“numVals”、“names”});
}
}
公共void sendTopic(Topic.ByReference pVal);
}
公共静态void main(字符串[]args){
final CLibrary clib=Native.loadLibrary(“example.dll”,CLibrary.class);
最终字符串[]myArray=新字符串[5];
myArray[0]=“一”;
myArray[1]=“两个”;
myArray[2]=“三”;
myArray[3]=“四”;
myArray[4]=“五”;
CLibrary.Topic.ByReference ref=新建CLibrary.Topic.ByReference();
参考数值=5;
ref.names=新的StringArray(myArray);
clib.sendTopic(参考文献);
}
}

您可以使用前面提到的
公共StringArray(String[]strings)
使用指针和传递StringArray


也许有帮助?很遗憾,我对c++不是很熟悉,谢谢,但这对我来说并不适用。我需要让它与JNA一起工作。另外,我注意到C++中使用了<代码>向量,但是java中的数组,当你在C++中使用数组时会发生什么?@ LINO不重要,因为vector是托管数组。它只知道额外的信息,如大小,并有额外的能力。但当涉及到访问内存内容时,两者都很重要similar@SolidMercury这就像java中的
ArrayList
,我在数组背后的想法是,我不完全确定JNI是否会知道如何直接从
std::vector
转换为数组。只是一种猜测,这是否意味着我的建议对你有所帮助?
public String src_id;
public String dest_id;
public int num;
public Pointer names;

String[] namesArray= new String[2];
//Assign two names to namesArray

//Construct StringArray with namesArray and pass it to Pointer
names = new StringArray(namesArray);