简体   繁体   中英

Pointer to array of structures as JNA method arguments

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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