Add syntax highlighting to C- and Java code
Esse commit está contido em:
@@ -5,61 +5,69 @@ Callback declarations consist of a simple interface that extends the Callback in
|
||||
|
||||
Original C declarations
|
||||
|
||||
typedef void (*sig_t) (int);
|
||||
sig_t signal(int sig, sig_t func);
|
||||
int SIGUSR1 = 30;
|
||||
```c
|
||||
typedef void (*sig_t) (int);
|
||||
sig_t signal(int sig, sig_t func);
|
||||
int SIGUSR1 = 30;
|
||||
```
|
||||
|
||||
Equivalent JNA mappings
|
||||
|
||||
public interface CLibrary extends Library {
|
||||
int SIGUSR1 = 30;
|
||||
interface sig_t extends Callback {
|
||||
void invoke(int signal);
|
||||
}
|
||||
sig_t signal(int sig, sig_t fn);
|
||||
int raise(int sig);
|
||||
```java
|
||||
public interface CLibrary extends Library {
|
||||
int SIGUSR1 = 30;
|
||||
interface sig_t extends Callback {
|
||||
void invoke(int signal);
|
||||
}
|
||||
...
|
||||
CLibrary lib = (CLibrary)Native.loadLibrary("c", CLibrary.class);
|
||||
// WARNING: you must keep a reference to the callback object
|
||||
// until you deregister the callback; if the callback object
|
||||
// is garbage-collected, the native callback invocation will
|
||||
// probably crash.
|
||||
CLibrary.sig_t fn = new CLibrary.sig_t() {
|
||||
public void invoke(int sig) {
|
||||
System.out.println("signal " + sig + " was raised");
|
||||
}
|
||||
};
|
||||
CLibrary.sig_t old_handler = lib.signal(CLibrary.SIGUSR1, fn);
|
||||
lib.raise(CLibrary.SIGUSR1);
|
||||
...
|
||||
sig_t signal(int sig, sig_t fn);
|
||||
int raise(int sig);
|
||||
}
|
||||
/* ... */
|
||||
CLibrary lib = (CLibrary)Native.loadLibrary("c", CLibrary.class);
|
||||
// WARNING: you must keep a reference to the callback object
|
||||
// until you deregister the callback; if the callback object
|
||||
// is garbage-collected, the native callback invocation will
|
||||
// probably crash.
|
||||
CLibrary.sig_t fn = new CLibrary.sig_t() {
|
||||
public void invoke(int sig) {
|
||||
System.out.println("signal " + sig + " was raised");
|
||||
}
|
||||
};
|
||||
CLibrary.sig_t old_handler = lib.signal(CLibrary.SIGUSR1, fn);
|
||||
lib.raise(CLibrary.SIGUSR1);
|
||||
/* ... */
|
||||
```
|
||||
|
||||
Here is a more involved example, using the Win32 APIs to enumerate all native windows:
|
||||
|
||||
Original C declarations
|
||||
|
||||
typedef int (__stdcall *WNDENUMPROC)(void*,void*);
|
||||
int __stdcall EnumWindows(WNDENUMPROC,void*);
|
||||
```c
|
||||
typedef int (__stdcall *WNDENUMPROC)(void*,void*);
|
||||
int __stdcall EnumWindows(WNDENUMPROC,void*);
|
||||
```
|
||||
|
||||
Equivalent JNA mappings
|
||||
|
||||
public interface User32 extends StdCallLibrary {
|
||||
interface WNDENUMPROC extends StdCallCallback {
|
||||
/** Return whether to continue enumeration. */
|
||||
boolean callback(Pointer hWnd, Pointer arg);
|
||||
}
|
||||
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
|
||||
```java
|
||||
public interface User32 extends StdCallLibrary {
|
||||
interface WNDENUMPROC extends StdCallCallback {
|
||||
/** Return whether to continue enumeration. */
|
||||
boolean callback(Pointer hWnd, Pointer arg);
|
||||
}
|
||||
...
|
||||
User32 user32 = User32.INSTANCE;
|
||||
boolean EnumWindows(WNDENUMPROC lpEnumFunc, Pointer arg);
|
||||
}
|
||||
/* ... */
|
||||
User32 user32 = User32.INSTANCE;
|
||||
|
||||
user32.EnumWindows(new WNDENUMPROC() {
|
||||
int count;
|
||||
public boolean callback(Pointer hWnd, Pointer userData) {
|
||||
System.out.println("Found window " + hWnd + ", total " + ++count);
|
||||
return true;
|
||||
}
|
||||
}, null);
|
||||
user32.EnumWindows(new WNDENUMPROC() {
|
||||
int count;
|
||||
public boolean callback(Pointer hWnd, Pointer userData) {
|
||||
System.out.println("Found window " + hWnd + ", total " + ++count);
|
||||
return true;
|
||||
}
|
||||
}, null);
|
||||
```
|
||||
|
||||
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.
|
||||
|
||||
|
||||
Referência em uma Nova Issue
Bloquear um usuário