简体   繁体   English

指向结构数组的指针作为JNA方法参数

[英]Pointer to array of structures as JNA method arguments

I am trying to create a JNA implementation over the SctpDrv library. 我试图在SctpDrv库上创建一个JNA实现。 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. JNA dokumentation仅显示一个带有指向基本类型数组的指针的示例。 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 *);

According to the documentation the third argument of getpaddrs is used to return an array of sockaddr structures. 根据文档,getpaddrs的第三个参数用于返回一个sockaddr结构数组。 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? 声明相应JNA方法的推荐方法是什么,以及如何准备参数,以及在我的java代码中调用后访问它?

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);
}

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

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