我正在尝试将 JNA 与 Windows 中的 DLL 一起使用,到目前为止,我能够成功调用一个名为c_aa_find_devices()
. 但是所有的函数都以 开头,c_aa
我想将其重命名为find_devices()
.
从我收集的方法来看,我StdCallFunctionMapper
无法在示例中找到有关如何使用它的文档(即如何按名称或按序将 DLL 函数映射到包装的 Java 库接口中的所需名称) )。关于文档在哪里的任何建议?
一个完整的工作示例,使用函数映射器。
import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.NativeLibrary;
import com.sun.jna.win32.StdCallFunctionMapper;
import java.io.File;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
public class JnaTest {
static {
Map options = new HashMap();
options.
put(
Library.OPTION_FUNCTION_MAPPER,
new StdCallFunctionMapper() {
HashMap<String, String> map = new HashMap() {
{
put("testMethod", "testMethod@0");
}
};
@Override
public String getFunctionName(NativeLibrary library, Method method) {
String methodName = method.getName();
return map.get(methodName);
}
}
);
File LIB_FILE = new File("test.dll");
Native.register(NativeLibrary.getInstance(LIB_FILE.getAbsolutePath(), options));
}
private static native int testMethod();
public static void main(String[] args) {
testMethod(); // call the native method in the loaded dll with the function name testMethod@0
}
}
Run Code Online (Sandbox Code Playgroud)
根据文档,您需要在对 loadLibrary 的原始调用中提供一个 FunctionMapper 来转换名称。但是,您还需要保留标准调用映射,因此请尝试以下操作:
Map options = new HashMap();
options.
put(
Library.OPTION_FUNCTION_MAPPER,
new StdCallFunctionWrapper() {
public String getFunctionName(NativeLibrary library, Method method) {
if (method.getName().equals("findDevices")
method.setName("c_aa_find_devices");
// do any others
return super.getFunctionName(library, method);
}
}
);
Native.loadLibrary(..., ..., options);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6245 次 |
最近记录: |