请教大家一个关于jna调用中,遇到结构体指针的问题。

jane_zhao 2014-06-20 09:49:51
// Original C code
typedef struct _Point
{ int x, y;
} Point;

Point* translate(Point* pt, int dx, int dy);

// Equivalent JNA mapping

class Point extends Structure
{ public int x, y; }

Point translate(Point pt, int x, int y);
...
Point pt = new Point();
Point result = translate(pt, 100, 100);

以上是JNA API Documentation 里的实现方式,依次类推,如果C这边的函数类似:
struct nn_pollfd {
int fd;
short events;
short revents;
};
int nn_poll (struct nn_pollfd *fds, int nfds, int timeout);

那我写的java代码则为:
public class nn_pollfd extends Structure{
public int fd;
public short events;
public short revents;
}
int nn_poll (nn_pollfd fds, int nfds, int timeout);


我的问题出来了,C这边为结构体指针,可以传入多组结构体值,例如:
struct nn_polld pfd [2];
pfd [0].fd = s1;
pfd [0].events = NN_POLLIN | NN_POLLOUT;
pfd [1].fd = s2;
pfd [1].events = NN_POLLIN;
rc = nn_poll (pfd, 2, 2000);
if (rc == 0) {
printf ("Timeout!");
exit (1);
}
if (rc == -1) {
printf ("Error!");
exit (1);
}
if (pfd [0].revents & NN_POLLIN) {
printf ("Message can be received from s1!");
exit (1);
}

但对应到java这边,第一个参数只是一个类而非类的数组,那只能传入一组值,java这边怎么办?
在线等,谢谢!

...全文
293 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
soonerfast1 2014-09-04
  • 打赏
  • 举报
回复
我觉得int nn_poll (nn_pollfd fds, int nfds, int timeout); 错误了,声明应该为int nn_poll (Pointer fds, int nfds, int timeout);或者int nn_poll (nn_pollfd.Byreference fds, int nfds, int timeout);
jane_zhao 2014-06-20
  • 打赏
  • 举报
回复
有人在吗?怎么没人帮我啊?谢谢!

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧