指向结构数组的指针,作为JNA方法参数 [英] Pointer to array of structures as JNA method arguments

查看:116
本文介绍了指向结构数组的指针,作为JNA方法参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 SctpDrv 库上创建JNA实现.我的问题是,我不了解结构数组的指针.我试图寻找解决方案,但它们始终与我需要知道的有所不同. JNA文档仅显示了一个带有指向原始类型数组的指针的示例.似乎也有不同的方法,其中一些已被描述.

I am trying to create a JNA implementation over the SctpDrv library. My problem is that I don't get my head around pointers to structure arrays. I have tried to search for a solution, but they have always been slightly different from what I need to know. The JNA dokumentation only show an example with a pointer to an array of primitive type. There also seem to be different ways of doing this, of which some are depricated.

int  WSAAPI internal_sctp_getpaddrs (SOCKET, sctp_assoc_t, struct sockaddr **);
void WSAAPI internal_sctp_freepaddrs (struct sockaddr *);

根据文档,getpaddrs的第三个参数用于返回sockaddr结构的数组.推荐使用什么方法声明对应的JNA方法?如何准备参数,并在调用Java代码后访问该参数?

According to the documentation the third argument of getpaddrs is used to return an array of sockaddr structures. What is the recommended way to declare the corresponding JNA methods, and how do I prepare the argument, as well as get access to it after the call in my java code?

另外,为了帮助我理解,我将如何声明和使用一个函数,而不是参数是包含指针的数组?

Also, to help me understand, how would I declare and use a function where instead the argument is an array containing pointers?

推荐答案

// Declare the SOCKADDR struct
public class SOCKADDR extends Structure
{
   // Declare fields here

   public SOCKADDR()
   {
      // required for toArray()
   }

   public SOCKADDR(Pointer pointer)
   {
      super(pointer);
   }
}

// Declare these Java methods to be mapped by JNA to the C APIs
public int  internal_sctp_getpaddrs(int socket, int sctp, PointerByReference sockaddrRef);
public void internal_sctp_freepaddrs(SOCKADDR sockaddr);

// Use this code to call internal_sctp_getpaddrs()
// This code assumes the number of SOCKADDRs returned is in the int return value.
{
   PointerByReference sockaddrRef;
   Pointer pointer;
   SOCKADDR sockaddr, sockaddrs[];
   int size;

   sockaddrRef = new PointerByReference();
   size        = internal_sctp_getpaddrs(socket, sctp, sockaddrRef);
   pointer     = sockaddrRef.getValue();
   sockaddr    = new SOCKADDR(pointer);
   sockaddrs   = (SOCKADDR[]) sockaddr.toArray(size);
}

这篇关于指向结构数组的指针,作为JNA方法参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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