开发者

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开发者_JAVA百科 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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜