9 messages in net.java.dev.jna.usersAW: [jna-users] Callback
FromSent OnAttachments
Novatchkov HristoMar 19, 2009 6:59 am 
Timothy WallMar 19, 2009 7:08 am 
Novatchkov HristoMar 19, 2009 8:19 am 
Timothy WallMar 19, 2009 8:52 am 
Novatchkov HristoMar 19, 2009 11:40 am 
Timothy WallMar 19, 2009 1:02 pm 
Novatchkov HristoMar 20, 2009 2:54 am 
Timothy WallMar 20, 2009 5:59 am 
Hristo NovatchkovMar 22, 2009 5:30 am 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:AW: [jna-users] CallbackActions...
From:Novatchkov Hristo (hris@univie.ac.at)
Date:Mar 19, 2009 8:19:29 am
List:net.java.dev.jna.users

Thanks for the reply! As you can probably guess the actual goal of this
implementation is to constantly receive (and save) the data received in the
(constant) variable pucRxBuffer (which is now defined as final Memory
aucResponseBuf = new Memory(20);). Since I'm quite inexperienced with such
callback methods, I'm wondering what exactly happens, when the
_ANT_AssignChannelEventFunction is called. For example, I don't think that the
callback pointer as second parameter (new CHANNEL_EVENT_FUNCTION() { public
boolean callback(byte ucChannel, byte ucEvent) {...) is ever processed, since
testing printouts wouldn't be printed to Eclipse's console.

Moreover, how does Java know if there is some data received and how is the data
written into the constant? Is it actually possible to write in a final variable
at all?

-----Ursprüngliche Nachricht----- Von: Timothy Wall [mailto:twal@dev.java.net] Gesendet: Donnerstag, 19. März 2009 15:09 An: use@jna.dev.java.net Betreff: Re: [jna-users] Callback

That looks fine, although you should not use String as pucRxBuffer, since Java String is read-only. The native memory passed to a function as a String argument is only valid for the duration of the call.

Use Memory or a direct byte buffer instead.

On Mar 19, 2009, at 10:00 AM, Novatchkov Hristo wrote:

I am trying to use the callback functionality of JNA in order to call a function from a dll, which has a pointer to a function as one of the parameters. My approach is:

//C function definition void ANT_AssignChannelEventFunction(UCHAR ucChannel, CHANNEL_EVENT_FUNC pfChannelEvent, UCHAR *pucRxBuffer);

// C pointer function definition BOOL ChannelEventFunction(UCHAR ucChannel, UCHAR ucEvent) { Switch (ucEvent) { Case 0: Switch (ucChannel) { Case Channel_0: //defined and initialized beforehand // process received data which is in channel event Break; ... } Break; } }

//JNA implementation public interface ANT_MOD extends Library { ... public boolean _ANT_AssignChannelEventFunction(byte ucChannel, CHANNEL_EVENT_FUNCTION pfChannelEvent, String aucResponseBuf); ... }

public interface CHANNEL_EVENT_FUNCTION extends Callback { public boolean callback(byte ucChannel, byte ucEvent); }

...

public void initANT() { ... ant_mod = (ANT_MOD) Native.loadLibrary("ANT_DLL", ANT_MOD.class); final String aucResponseBuf = ""; boolean check = ant_mod._ANT_AssignChannelEventFunction(channel, new CHANNEL_EVENT_FUNCTION() { public boolean callback(byte ucChannel, byte ucEvent) { Switch (ucEvent) { Case 0: Switch (ucChannel) { Case Channel_0: //defined and initialized beforehand // process received data Break; ... } Break;

} return true; }, aucResponseBuf); ... }

I'm quite unsure about the correct procedure. Could somebody please let me know if this is the right way to do such callbacks. Thanks!