Pointer to an array of structures as arguments to the JNA method

I am trying to create a JNA implementation through the SctpDrv library. My problem is that I'm not inclined to pointers to structure arrays. I tried to find a solution, but they were always different from what I needed to know. The JNA view shows only an example with a pointer to an array of a primitive type. There are also different ways to do this, some of which are deprived.

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 to getpaddrs is used to return an array of sockaddr structures. What is the recommended way to declare the appropriate JNA methods and how to prepare the argument and also access it after the call in my java code?

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

+4
source share
1 answer
 // 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); } 
+5
source

Source: https://habr.com/ru/post/1343810/


All Articles