简体   繁体   English

如何通过JNA通过引用传递结构

[英]How to pass structure by reference with JNA

I want to call this function : 我想调用这个函数:

extern "C" xxx_SERVICE_API int  initSocket(socketStruct* skStruct); 

defined in a c++ dll.socketStruct is a structure defined below: 在c ++ dll.socketStruct中定义的结构如下:

typedef struct 
{
    int a;
    int b;
} mystruct;

equivalent code in java is : java中的等效代码是:

public interface MyDllClass extends Library {

    public static class MyStructure extends Structure {

        public static class ByReference extends MyStructureRef
        implements Structure.ByReference{ 


        }
        @Override
        protected List<String> getFieldOrder() {    

            return Arrays.asList(new String[]{"a","b"});
        }
        public int a;
        public int b;
    }
    //Native function
    int initSocket (MyStructure.ByReference sks); 
    MyDllClass INSTANCE = (MyDllClass)Native.loadLibrary("Mydll",  
    MyDllClass.class); 
}

How can i call this native fucntion with the structure by reference as parameter? 如何通过引用将结构作为参数来调用此本机功能? Thank you 谢谢

Thank you for your answer. 谢谢您的回答。 According with you i add a contructor to my structure: 根据您的说法,我在我的结构中添加了一个构造器:

public interface MyDllClass extends Library {

    public static class MyStructure extends Structure {

       public MyStructure() {

            idModule = 0;
            nbModules = 0;
            nbQueueInModule = 3;
            portList = new int[128];
            serverList = new char[128][20]; 
        }
        @Override
        protected List<String> getFieldOrder() {    

            return Arrays.asList(new String[]{"a","b","portList","serverList"});
        }
        public int a;
        public int b;
        public int[] portList;
        public char[][] serverList;
    }
    //Native function
    int initSocket (MyStructure.ByReference sks); 
    MyDllClass INSTANCE = (MyDllClass)Native.loadLibrary("Mydll",  
    MyDllClass.class); 
}

In the main I wrote this code : 我主要编写了以下代码:

MyStructure mySocket = new MyStructure();
MyDllClass.INSTANCE.initSocket(mySocket);

This code fail because of Null pointer exception when calling the native function. 调用本机函数时,此代码由于Null指针异常而失败。 Do you have an idea ? 你有想法吗 ?

All JNA Structure parameters default to by reference ( struct* ) semantics. 所有JNA Structure参数默认为按引用( struct* )语义。

When appearing as structure members, they default to by value semantics. 当显示为结构成员时,它们默认为按值语义。

The ByReference and ByValue tagging interfaces are provided for use where you want the complementary behavior. 提供了ByReferenceByValue标记接口,以用于需要补充行为的地方。 If the default behavior is what you want, you don't need the extra typing. 如果默认行为是您想要的,则不需要额外的输入。

You should always provide a Pointer -based constructor to Structure s you define, it avoids superfluous memory allocation by JNA. 您应该始终为您定义的Structure提供基于Pointer的构造函数,这样可以避免JNA分配过多的内存。

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

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