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

Typically when you register a callback function, the library will at some future point in time call that function. Most likely your callback is being called when data has been written to the buffer, which would be the only notification you get that the native memory contents has been changed.

Make sure you keep a reference to the callback object, or it will be GC'd and the native code will have no code to execute when the callback is triggered.

"final" means that you can't change the java reference. The object itself is still mutable.

On Mar 19, 2009, at 11:20 AM, Novatchkov Hristo wrote:

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!