10 messages in net.java.dev.jna.usersRe: [jna-users] Callback doesn't work...
FromSent OnAttachments
bao baoJun 14, 2009 8:16 pm 
Timothy WallJun 15, 2009 3:55 am 
bao baoJun 15, 2009 4:43 am 
Timothy WallJun 15, 2009 5:16 am 
bao baoJun 15, 2009 6:35 pm 
Timothy WallJun 16, 2009 3:27 am 
bao baoJun 16, 2009 7:15 am 
bao baoJun 16, 2009 8:14 pm 
Timothy WallJun 17, 2009 3:43 am 
bao baoJun 21, 2009 5:46 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] Callback doesn't work at all.Actions...
From:Timothy Wall (twal@dev.java.net)
Date:Jun 16, 2009 3:27:18 am
List:net.java.dev.jna.users

Here's a better example than "atexit" (which isn't guaranteed to work, since the VM may not be fully functional by the time the exit hooks are run:

import com.sun.jna.*;

public class CallbackTest {

public interface CLibrary extends Library { int SIGUSR1 = 30; interface sig_t extends Callback { void invoke(int signal); } sig_t signal(int signal, sig_t func); int raise(int signal); }

public static void main(String[] args) { CLibrary lib = (CLibrary)Native.loadLibrary("c", CLibrary.class); CLibrary.sig_t fn = new CLibrary.sig_t() { public void invoke(int signal) { System.out.println("received signal " + signal); System.exit(0); } }; CLibrary.sig_t old_handler = lib.signal(CLibrary.SIGUSR1, fn);

lib.raise(CLibrary.SIGUSR1); while (true) { try { Thread.sleep(10); } catch(Exception e) { } } } }

On Jun 15, 2009, at 9:35 PM, bao bao wrote:

thanks your advice, i have tried to run the example on the project home page, but i got: JNA: Can't attach to current thread. I couln't fix it, can you help me out?

On Mon, Jun 15, 2009 at 8:16 PM, Timothy Wall <twal@dev.java.net> wrote:

On Jun 15, 2009, at 7:44 AM, bao bao wrote:

in fact i have extracted some code from my project. but i really don't how to use callback in JNA. Can you modify my code or give me an example? Thanks, sir. Regards

The "atexit" example on the project home page is a complete example of using a JNA Callback instance as a function pointer. Make sure you understand how that works before you try something more complex.