简体   繁体   English

JNA回调问题

[英]JNA Callback Issues

I want to call a DLL file, having successfully created the interface with JNA studio. 我想调用DLL文件,并成功创建了与JNA studio的接口。 I have a problem calling the necessary methods. 我在调用必要的方法时遇到问题。

Please see the interface below. 请参见下面的界面。

public interface MME9000Library extends Library {

    public static final String JNA_LIBRARY_NAME =LibraryExtractor.getLibraryPath(" MME9000", true, TP9000Library.class);  

    public static final NativeLibrary JNA_NATIVE_LIB NativeLibrary.getInstance(TP9000Library.JNA_LIBRARY_NAME,      MangledFunctionMapper.DEFAULT_OPTIONS);    

    public static final VLibrary INSTANCE = (MME9000Library)Native.loadLibrary(TP9000Library.JNA_LIBRARY_NAME,       MME9000Library.class, MangledFunctionMapper.DEFAULT_OPTIONS);     

    public static final int TP_TRUE = (int)-1;
    public static final int TP_NOT_SUPPORTED_IMAGE = (int)-4;
    public static final int TP_FALSE = (int)-2;
    public static final int TP_DLL_NOT_LOADED = (int)-3;

    public interface pfnSetLogPathFile extends StdCallCallback {
        int apply(Pointer pLogFile);
    };
    public interface pfnGetConnectedDevices extends StdCallCallback {
        int apply(int dwDevice, IntByReference pdwCount);
    };

    public static class LPRECT extends PointerType {
        public LPRECT(Pointer address) {
            super(address);
        }
        public LPRECT() {
            super();
        }
    };
    public static class LPBYTE extends PointerType {
        public LPBYTE(Pointer address) {
            super(address);
        }
        public LPBYTE() {
            super();
        }
    };
    public static class HANDLE extends PointerType {
        public HANDLE(Pointer address) {
            super(address);
        }
        public HANDLE() {
            super();
        }
    };
    public static class LPWSTR extends PointerType {
        public LPWSTR(Pointer address) {
            super(address);
        }
        public LPWSTR() {
            super();
        }
    };

    public static class LPDWORD extends PointerType {
        public LPDWORD(Pointer address) {
            super(address);
        }
        public LPDWORD() {
            super();
        }
    };

}

For example if I want to call pfnGetConnectedDevices interface (although it's a method in the C++ header file) how do i go about it? 例如,如果我想调用pfnGetConnectedDevices接口(尽管它是C ++头文件中的方法),该如何处理?

If you have a function which takes a function pointer as an argument, then you do this: 如果您有一个将函数指针作为参数的函数,则可以这样做:

pfnGetConnectedDevices myCallback = new pfnGetConnectedDevices() {
    public int apply(int dwDevice, IntByReference pdwCount) {
        // your code here
    }
};
lib.myFunction(myCallback);
pfnListDevices dvList = new pfnListDevices(){

    @Override
    public int apply(IntByReference pArg1, Pointer pArg2, int pArg3){
        return pArg1.getValue();
    }
};
System.out.println("list device is "+dvList.apply(pdwCount, Pointer.NULL,  0x01000000));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM