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

发布于 2024-10-22 05:41:04 字数 519 浏览 1 评论 0原文

我正在尝试通过 SctpDrv 库创建 JNA 实现。我的问题是我不了解指向结构数组的指针。我试图寻找解决方案,但它们总是与我需要知道的略有不同。 JNA 文档仅显示了一个带有指向原始类型数组的指针的示例。似乎还有不同的方法可以做到这一点,其中一些方法已被贬低。

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

根据文档, getpaddrs 的第三个参数用于返回 sockaddr 结构的数组。声明相应 JNA 方法的推荐方法是什么?如何准备参数,以及如何在 java 代码中调用后访问它?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-10-29 05:41:04
// 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);
}
// 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文