Java JNA-无法获取从本机函数到回调函数的调用
我正在使用JNA调用dll文件的函数 其中一个函数需要指向回调函数的指针Java JNA-无法获取从本机函数到回调函数的调用,java,dll,java-native-interface,jna,Java,Dll,Java Native Interface,Jna,我正在使用JNA调用dll文件的函数 其中一个函数需要指向回调函数的指针 // Dll function void MyFunction (*CallBackFnName); 下面是java中的JNA代理接口 import com.sun.jna.Callback; import com.sun.jna.Library; import com.sun.jna.Pointer; public interface Dll extends Library { interface CallBack
// Dll function
void MyFunction (*CallBackFnName);
下面是java中的JNA代理接口
import com.sun.jna.Callback;
import com.sun.jna.Library;
import com.sun.jna.Pointer;
public interface Dll extends Library {
interface CallBackFnName extends Callback {
void callback(Pointer dataBuffer, int dataLen);
}
public void MyFunction(Dll.CallBackFnName fn);
public int StartReading(short arg1, short arg2);
}
根据dll的API,将指向回调函数的指针传递给函数MyFunction(*callbacknname)后,无论何时调用StartReading()函数,它都会向回调函数发送数据
当我尝试这样做时,它并没有调用我的回调函数。它也不会抛出任何异常
下面是我调用函数的代码:
import com.sun.jna.Native;
import com.sun.jna.Pointer;
public class Start {
private static Dll dll = (Dll) Native.loadLibrary("MyDll", Dll.class);
private static Dll.CallBackFnName fn = new Dll.CallBackFnName() {
@Override
public void callback(Pointer dataBuffer, int dataLen) {
System.err.println("Callback function is called successfully");
}
};
public static void main(String[] args) throws InterruptedException {
dll.MyFunction(fn); //passed the pointer to the callback function
short arg1 = 0;
short arg2 = 0;
dll.StartReading(arg1, arg2));
Thread.sleep(10 * 1000);
}
}
运行上述代码后,我在控制台上获得以下信息:
DeviceAttach: received and accepted attach for vendor id 0x3eb, product id 0x2ffd, interface 0, device handle 0x037825E8
Main Menu (active Dev/Prod/Interface/Alt. Setting: 0x3eb/0x2ffd/0/0)
Read FailReadWritePipesMenu: WDU_Transfer(control receive) failed: error 0x2000000e ("
Read Fail")
Read FailReadWritePipesMenu: WDU_Transfer(control receive) failed: error 0x2000000e ("
Read Fail")
Read FailReadWritePipesMenu: WDU_Transfer(control receive) failed: error 0x2000000e ("
Read Fail")
Transferred 0 bytes
0 0
不知道你是否还需要答案,但对于其他有这个问题的人, 我以前遇到过这个问题。我必须创建一个匿名类(使用上面的示例)
这对我有用。虽然我无法解释原因。不知道您是否还需要答案,但对于其他有此问题的人, 我以前遇到过这个问题。我必须创建一个匿名类(使用上面的示例) 这对我有用。虽然我无法解释为什么
dll.myfunc(new Dll.CallBackFnName() {
@Override
public void callback(Pointer dataBuffer, int dataLen) {
System.err.println("Callback function is called successfully");
} } );