如何获取Java中的一个新的指针? [英] How to obtain a new Pointer in Java?

查看:224
本文介绍了如何获取Java中的一个新的指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能调用方法与JNA用C这个方法的签名?

  INT open_device(上下文CTX *,**的设备开发,INT指数);

在C法的最后两行是这样的:

  *开发= PDEV;
返回0;

这是该方法只使用开发的。这意味着,我有一个poiner传递到一个空指针的方法,对吗?然后,该方法填补了空指针以设备对象的地址,我可以将指针传递到设备的其他方法。

我的问题是:这是这样做的正确方法?如果是,我怎么从Java分配一个新的指针?


根据公认的答案,我这样做:

 内存P =新的内存(Pointer.SIZE);
内存P2 =新的内存(Pointer.SIZE);
p.setPointer(0,P2);
nativeLib.open_device(CTX,P,索引);
返回P2;


解决方案

看来,JNA的 指针 类有 setPointer getPointer 方法,以允许多个间接和 内存 类实际上是分配本地对象。所以,你应该能够做到这样的:(我只是从JNA文档猜测,我没有测试这个)

 指针专指=新的内存(Pointer.SIZE); //分配空间容纳一个指针值
//传递给专指open_device
指针开发= pDev.getPointer(0); //检索指针存放在专指

How can I call a method with this method signature in C from JNA?

int open_device(context *ctx, device **dev, int index);

The last two lines of the C method look like this:

*dev = pdev;
return 0;

That's the only use of dev in that method. That means that I have to pass a poiner to an empty pointer to the method, right? The method then fills the empty pointer with the address of a device object and I can pass the pointer to the device to other methods.

My question is: Is this the right way to do that? If it is, how do I allocate a new pointer from Java?


Based on the accepted answer, I did this:

Memory p = new Memory(Pointer.SIZE);
Memory p2 = new Memory(Pointer.SIZE);
p.setPointer(0, p2);
nativeLib.open_device(ctx, p, index);
return p2;

解决方案

It appears that the JNA Pointer class has setPointer and getPointer methods to allow for multiple indirection, and the Memory class to actually "allocate" native objects. So you should be able to do something like: (I'm just guessing from the JNA docs, I've not tested this)

Pointer pDev = new Memory(Pointer.SIZE); // allocate space to hold a pointer value
// pass pDev to open_device
Pointer dev = pDev.getPointer(0);        // retrieve pointer stored at pDev

这篇关于如何获取Java中的一个新的指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆