Renaming DLL functions in JNA using StdCallFunctionMapper

I am trying to use JNA with a DLL on Windows, so far I have been able to successfully call a function named c_aa_find_devices(). But all functions start with c_aa, and I would like to rename it to find_devices().

From what I'm going to do, it is StdCallFunctionMapper, but I can’t find the documentation on how to use it in the example (for example, how to map a DLL function by name or serial number to the desired name in the Java library interface with the shell). Any suggestions on where the documents are?

+3
source share
4 answers

StdCallMapper - , werid windows std lib, , . std lib ( , 99% you'r ).

dll - , - :

class Mapper implements FunctionMapper{
    public String getFunctionName(NativeLibrary library, Method method) {
       return GenieConnector.FUNCTION_PREFIX + method.getName();
    }
}

GenieConnector.FUNCTION_PREFIX - . , FunctionMapper, StdCallMapper

+4

FunctionMapper loadLibrary, . , :

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

JNA JavaDoc JavaDocs.

- , , StdCallFunctionMapper ( , stdcall). Method.setName() , , . String Java , .

name = super.getFunctionName();
name = name.replace("find_devices", "c_aa_find_devices");

"c_aa_" ( - ), stdcall .

+1

mapper.

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
    }


}
+1

Source: https://habr.com/ru/post/1702789/


All Articles