Skip to content
This repository
Newer
Older
100644 74 lines (61 sloc) 2.557 kb
317d41e1 »
2012-06-12 Update master
1 Callbacks, Function Pointers and Closures
2 =========================================
8b22412e » dblock
2011-05-23 The rest of the documentation.
3
4 Callback declarations consist of a simple interface that extends the Callback interface and implements a callback method (or defines a single method of arbitrary name). Callbacks are implemented by wrapping a Java object method in a little bit of C glue code. The simplest usage resembles using anonymous inner classes to register event listeners. Following is an example of callback usage:
4b8d9108 » notnoop
2011-06-21 correct C equivalence of Callback documentation
5
02bd1427 »
2012-06-12 Update master
6 Original C declarations
7
9f1361b5 » headcr4sh
2014-04-20 Add syntax highlighting to C- and Java code
8 ```c
9 typedef void (*sig_t) (int);
10 sig_t signal(int sig, sig_t func);
11 int SIGUSR1 = 30;
12 ```
8b22412e » dblock
2011-05-23 The rest of the documentation.
13
02bd1427 »
2012-06-12 Update master
14 Equivalent JNA mappings
15
9f1361b5 » headcr4sh
2014-04-20 Add syntax highlighting to C- and Java code
16 ```java
17 public interface CLibrary extends Library {
18 int SIGUSR1 = 30;
19 interface sig_t extends Callback {
20 void invoke(int signal);
8b22412e » dblock
2011-05-23 The rest of the documentation.
21 }
9f1361b5 » headcr4sh
2014-04-20 Add syntax highlighting to C- and Java code
22 sig_t signal(int sig, sig_t fn);
23 int raise(int sig);
24 }
25 /* ... */
26 CLibrary lib = (CLibrary)Native.loadLibrary("c", CLibrary.class);
27 // WARNING: you must keep a reference to the callback object
28 // until you deregister the callback; if the callback object
29 // is garbage-collected, the native callback invocation will
30 // probably crash.
31 CLibrary.sig_t fn = new CLibrary.sig_t() {
32 public void invoke(int sig) {
33 System.out.println("signal " + sig + " was raised");
34 }
35 };
36 CLibrary.sig_t old_handler = lib.signal(CLibrary.SIGUSR1, fn);
37 lib.raise(CLibrary.SIGUSR1);
38 /* ... */
39 ```
8b22412e » dblock
2011-05-23 The rest of the documentation.
40
41 Here is a more involved example, using the Win32 APIs to enumerate all native windows:
4b8d9108 » notnoop
2011-06-21 correct C equivalence of Callback documentation
42
02bd1427 »
2012-06-12 Update master
43 Original C declarations
44
9f1361b5 » headcr4sh
2014-04-20 Add syntax highlighting to C- and Java code
45 ```c
46 typedef int (__stdcall *WNDENUMPROC)(void*,void*);
47 int __stdcall EnumWindows(WNDENUMPROC,void*);
48 ```
8b22412e » dblock
2011-05-23 The rest of the documentation.
49
02bd1427 »
2012-06-12 Update master
50 Equivalent JNA mappings
51
9f1361b5 » headcr4sh
2014-04-20 Add syntax highlighting to C- and Java code
52 ```java
53 public interface User32 extends StdCallLibrary {
54 interface WNDENUMPROC extends StdCallCallback {
55 /** Return whether to continue enumeration. */
56 boolean callback(Pointer hWnd, Pointer arg);
8b22412e » dblock
2011-05-23 The rest of the documentation.
57 }
9f1361b5 » headcr4sh
2014-04-20 Add syntax highlighting to C- and Java code
58 boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
59 }
60 /* ... */
61 User32 user32 = User32.INSTANCE;
8b22412e » dblock
2011-05-23 The rest of the documentation.
62
9f1361b5 » headcr4sh
2014-04-20 Add syntax highlighting to C- and Java code
63 user32.EnumWindows(new WNDENUMPROC() {
64 int count;
65 public boolean callback(Pointer hWnd, Pointer userData) {
66 System.out.println("Found window " + hWnd + ", total " + ++count);
67 return true;
68 }
69 }, null);
70 ```
8b22412e » dblock
2011-05-23 The rest of the documentation.
71
72 If your callback needs to live beyond the method invocation where it is used, make sure you keep a reference to it or the native code will call back to an empty stub after the callback object is garbage collected.
73
74 Proxy wrappers are automatically generated for function pointers found within structs initialized by native code. This facilitates calling those functions from Java.
75
Something went wrong with that request. Please try again.