JNA - 无法从本机函数调用回调函数

发布于 2024-10-15 03:50:31 字数 1948 浏览 2 评论 0原文

我正在使用 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 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( *CallBackFnName),每当您调用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

I am using JNA to call functions of a dll file.

One of the functions requires a pointer to a callback function

// Dll function
void MyFunction (*CallBackFnName);

Below is the JNA proxy interface in java

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);

}

According to the API of the dll, after passing the pointer to a callback function to the function MyFunction(*CallBackFnName), whenever you call StartReading() function it will send data to the callback function.

When I am trying to do that, It is not calling my callback function. It is not throwing any exception also.

Below is the code from which I am calling functions:

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);
}
}

After running the above code, I am getting the following on the console:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心意如水 2024-10-22 03:50:31

不知道您是否还需要答案,但对于遇到此问题的其他人来说,
我以前遇到过这个问题。我必须创建一个匿名类(使用上面的示例),

dll.myfunc(new Dll.CallBackFnName() {
@Override
public void callback(Pointer dataBuffer, int dataLen) {
    System.err.println("Callback function is called successfully");
} } );

这对我有用。虽然我无法解释为什么。

Don't know if you still need the answer , but to anyone else who has this problem,
I had this problem before.I had to create an anonymous class as such (using the above example),

dll.myfunc(new Dll.CallBackFnName() {
@Override
public void callback(Pointer dataBuffer, int dataLen) {
    System.err.println("Callback function is called successfully");
} } );

This worked for me . although i can't explain why.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文