9 messages in net.java.dev.jna.usersRe: [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:Re: [jna-users] CallbackActions...
From:Timothy Wall (twal@dev.java.net)
Date:Mar 19, 2009 7:08:20 am
List:net.java.dev.jna.users

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!