返回和访问结构对象的数组中JNA [英] Returning and accessing array of object of structure in jna

查看:428
本文介绍了返回和访问结构对象的数组中JNA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些C字符指针的结构:

 结构inputsheet
{
    字符* TestCaseID [MAX_TEST_CASES]
    字符*说明[MAX_TEST_CASES]
};

我有一个函数,结构对象的数组返回:

 结构inputsheet * getapi(字符* DOCNAME);

现在我想在Java中使用它。我该如何处理这种阵列结构的对象?我能够处理一个单一的对象而不是数组。

有关单个对象用C我的code是在这里:

 公共类STR3扩展com.sun.jna.Structure实现com.sun.jna.Structure.ByReference {
   公共指针A1;
   公共指针B2;
   公共指针C3 [] =新指针[10];
}

访问它:

  STR2 S2 = CLibrary.INSTANCE.parseid(xmlFile1);
对于(指针P1:s2.testCaseID){
    如果(P1!= NULL)
    {
        的System.out.println(p1.getString(0));
    }
}

修改

  \\\\ C $ C $Ç
结构STR3 {
字符*一个;
字符* B;
字符* AB [10];
}\\\\ JNA实现
包分析器;
进口com.sun.jna.Pointer;
公共类STR3扩展com.sun.jna.Structure实现com.sun.jna.Structure.ByReference {
公共指针A1;
公共指针B2;
公共指针C3 [] =新指针[10];
}
\\\\叫它
进口com.sun.jna.Native;
进口com.sun.jna.Pointer;
 AB类{
    接口扩展CLibrary图书馆{
    CLibrary INSTANCE =(CLibrary)Native.loadLibrary(chardll
    CLibrary.class);
    STR3 getStruct();
    }公共静态无效的主要(字串[] args){
    INT大小= 5;
    STR3一个= CLibrary.INSTANCE.getStruct();
    STR3 [] = AB(STR3 [])a.toArray(大小);
    的System.out.println(AB [0] .a1.getString(0));
    }
}

这是给垃圾值作为输出,,在那里我有更新我的code,以获得正确的输出。


解决方案

的javadoc


  

返回结构数组


  
  

声明方法返回适当类型的结构,然后调用 Structure.toArray(INT)转换为数组
  适当大小的初始化结构。请注意,您
  结构类必须有一个无参数的构造函数,你是
  负责释放返回的内存,如果适用于任何
  方法是适当的被调用的功能。

  //原来的C code
结构显示* get_displays为(int * pcount);
无效free_displays(结构显示*显示器);//等效JNA映射
显示get_displays(IntByReference pcount);
无效free_displays(显示[]显示器);
...
IntByReference pcount =新IntByReference();
显示器D = lib.get_displays(pcount);
显示[] =显示(显示[])d.toArray(pcount.getValue());
...
lib.free_displays(显示器);


修改

名义上,你的结构应该是这样的(基于您的本地定义):

 类inputsheet扩展结构{
    公共指针[] = TestCaseID新指针[MAX_TEST_CASES]
    公共指针[] =说明新指针[MAX_TEST_CASES]
}公共inputsheet getapi(字符串DOCNAME);INT大小= ...; //不管你做什么要弄清楚你返回数组的大小
inputsheet表= INSTANCE.getapi(一些-DOC);
inputsheet [] =表(inputsheet [])sheet.toArray(大小);

如果调用者不需要写 inputsheet 字段,你应该使用字符串而不是指针

I have a structure that contains some char pointers in C:

struct inputsheet
{
    char* TestCaseID[MAX_TEST_CASES];
    char*  Description[MAX_TEST_CASES];
};

I have a function that is returns array of object of structure:

struct inputsheet* getapi(char *docname);

Now I want to use this in Java. How can I handle this array of object of structure? I am able to handle a single object but not the array.

For single object my code in C is here:

public class str3 extends com.sun.jna.Structure implements com.sun.jna.Structure.ByReference {  
   public Pointer a1;
   public Pointer b2;
   public Pointer c3[]=new Pointer[10];
}

Accessing it:

str2 s2=CLibrary.INSTANCE.parseid(xmlFile1);
for(Pointer p1:s2.testCaseID) {
    if(p1!=null)
    {
        System.out.println(p1.getString(0));
    }
}

EDIT

\\c code
struct str3{
char *a;
char *b;
char *ab[10];
}

\\jna implementation


package parser;
import com.sun.jna.Pointer;
public class str3 extends com.sun.jna.Structure implements     com.sun.jna.Structure.ByReference{ 
public Pointer a1;
public Pointer b2;
public Pointer c3[]=new Pointer[10];
}


\\calling it
import com.sun.jna.Native;
import com.sun.jna.Pointer;
 class ab{
    interface CLibrary extends Library{
    CLibrary INSTANCE = (CLibrary) Native.loadLibrary("chardll",
    CLibrary.class);
    str3 getStruct();
    }

public static void main(String[] args) {
    int size = 5;
    str3 a=CLibrary.INSTANCE.getStruct();
    str3[] ab=(str3[])a.toArray(size);
    System.out.println(ab[0].a1.getString(0));          
    }
}

It is giving Garbage value as output,, where I have to update my code to get the right output..

解决方案

From the javadoc:

Returning an Array of struct

Declare the method as returning a Structure of the appropriate type, then invoke Structure.toArray(int) to convert to an array of initialized structures of the appropriate size. Note that your Structure class must have a no-args constructor, and you are responsible for freeing the returned memory if applicable in whatever way is appropriate for the called function.

// Original C code
struct Display* get_displays(int* pcount);
void free_displays(struct Display* displays);

// Equivalent JNA mapping
Display get_displays(IntByReference pcount);
void free_displays(Display[] displays);
...
IntByReference pcount = new IntByReference();
Display d = lib.get_displays(pcount);
Display[] displays = (Display[])d.toArray(pcount.getValue());
...
lib.free_displays(displays);

EDIT

Nominally, your structure would look like this (based on your native definition):

class inputsheet extends Structure {
    public Pointer[] TestCaseID = new Pointer[MAX_TEST_CASES];
    public Pointer[] Description = new Pointer[MAX_TEST_CASES];
}

public inputsheet getapi(String docname);

int size = ...; // whatever you do to figure out the size of your returned array 
inputsheet sheet = INSTANCE.getapi("some-doc");
inputsheet[] sheets = (inputsheet[])sheet.toArray(size);

If the caller doesn't need to write to the inputsheet fields, you should use String instead of Pointer.

这篇关于返回和访问结构对象的数组中JNA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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