1 回答

TA貢獻1735條經驗 獲得超5個贊
基于JNA 文檔中的內容:
typedef void (*ExampleCallback)(const char*);
void exampleMethod(const char* value)
{
printf("This is the string: %s\n", value);
}
void example_triggerCallback(const example_ptr func, const char* str) {
printf("provided str: %s", str);
func(str);
}
public interface CLibrary extends Library {
// define an interface that wraps the callback code
public interface ExampleCallbackInterface extends Callback {
void invoke(String val);
}
// functions available in library (name must match)
public void exampleMethod(String value);
public void example_triggerCallback(ExampleCallbackInterface callback);
}
// define an implementation of the callback interface
public static class CallbackExample implements Example22CallbackInterface {
private CLibrary lib;
public CallbackExample(CLibrary useLib) {
lib = useLib;
}
@Override
public void invoke(String val) {
lib.exampleMethod(val);
}
}
...
final CLibrary clib = (CLibrary)Native.loadLibrary("testlib", CLibrary.class);
...
// instantiate a callback wrapper instance
final CallbackExample callback = new CallbackExample(clib);
// pass the callback wrapper to the C library
clib.example_triggerCallback(callback);
由于我在其他互聯網位置回答了這個問題,因此我知道它適用于提問者。
添加回答
舉報