JNA中的回调

1、win10  pch.h

#ifndef PCH_H
#define PCH_H

// 添加要在此处预编译的标头
#include "framework.h"

#ifdef __cplusplus
#define EXPORT extern "C" __declspec(dllexport)
#else 
#define EXPORT __declspec(dllexport)
#endif


typedef struct Example3Struct {
	int val;
	char* name;
} Example3Struct;

typedef struct Example4Struct {
	int val;
	char* name;
	char* add;
} Example4Struct;

//定义回调开始
typedef void(*Example22Callback)(int);

EXPORT void example22_triggerCallback(const Example22Callback pfn);
// 定义回调结束

//函数指针 回调java的方法返回int
typedef int (*max_fun)(int, int);
// 回调
EXPORT void triggerCallback(const max_fun max, int a, int b);

EXPORT void example3_sendStruct(const Example3Struct* sval);

EXPORT Example4Struct example4_getStruct(const Example3Struct* sval3);

#endif //PCH_H

2 、pch.cpp

// pch.cpp: 与预编译标头对应的源文件

#include "pch.h"
#include "iostream"



	void example3_sendStruct(const Example3Struct* sval)
	{
		// note: printfs called from C won't be flushed
		// to stdout until the Java process completes
		std::cout << sval->val << std::endl;
		std::cout << sval->name << std::endl;
	}


	Example4Struct example4_getStruct(const Example3Struct* sval3)
	{
		Example4Struct sval4;
		sval4.val = 23+sval3->val;
		sval4.name = sval3->name;
		sval4.add = sval3->name;
		return sval4;
	}

	typedef void(*Example22Callback)(int);

	void example22_triggerCallback(const Example22Callback pfn)
	{
		//此处是执行java的 Example22CallbackImplementation.invoke
		(*pfn)(300);
	}
	 
	void triggerCallback(max_fun max, int a, int b) {
        //此处是执行 java的代码回调  MaxCallbackImpl.invoke
		int maxValue = (*max)(a, b);
		printf("%d and %d 's max value is: %d", a, b, maxValue);
	}

3、java 代码  VideoAiLibrary.java

package com.example.demo.jna;

import com.sun.jna.Callback;
import com.sun.jna.Library;
import com.sun.jna.Structure;


public interface VideoAiLibrary extends Library {

    @Structure.FieldOrder({"val", "name"})
    public static class Example3Struct extends Structure {
        public static class ByReference extends Example3Struct implements Structure.ByReference {
        }

        public int val;
        public String name;
    }

    @Structure.FieldOrder({"val", "name", "add"})
    public static class Example4Struct extends Structure {
        public static class ByValue extends Example4Struct implements Structure.ByValue {
        }

        public int val;

        public String name;

        public String add;
    }


    //回调
    public interface Example22CallbackInterface extends Callback {
        void invoke(int val);
    }

    // define an implementation of the callback interface
    public static class Example22CallbackImplementation implements Example22CallbackInterface {
        @Override
        public void invoke(int val) {
            // 处理回调函数的结果
            System.out.println("example22: " + val * 20);
        }
    }


    public interface MaxCallback extends Callback {
        int invoke(int a, int b);
    }

    public static class MaxCallbackImpl implements MaxCallback {
        @Override
        public int invoke(int a, int b) {
            return Math.max(a * 10, b * 10);
        }
    }

    //回调方法
    public void example22_triggerCallback(Example22CallbackInterface callback);

    public Example4Struct.ByValue example4_getStruct(Example3Struct.ByReference sval);

    // unless otherwise specified, ByReference is assumed - but it can't hurt to be explicit
    public void example3_sendStruct(Example3Struct.ByReference sval);

    //回调
    public void triggerCallback(MaxCallback maxCallback, int a, int b);


}

测试代码  JnaTest.java

package com.example.demo.jna;

import com.sun.jna.Native;

public class JnaTest {

    public static void main(String... args) {
        final VideoAiLibrary clib = (VideoAiLibrary) Native.load("mycpp11", VideoAiLibrary.class);

        final VideoAiLibrary.Example3Struct.ByReference e3ref = new VideoAiLibrary.Example3Struct.ByReference();
        e3ref.val = 700;
        e3ref.name = "朱dddp";
        clib.example3_sendStruct(e3ref);

        final VideoAiLibrary.Example4Struct.ByValue e4val = clib.example4_getStruct(e3ref);
        System.out.println("example 4: " + e4val.val);
        System.out.println("example 4: " + e4val.name);
        System.out.println("example 4: " + e4val.add);
        //回调
        clib.example22_triggerCallback(new VideoAiLibrary.Example22CallbackImplementation());
        //回调
        clib.triggerCallback(new VideoAiLibrary.MaxCallbackImpl(), 15, 56);

    }

}

5、linux 代码

通过百度网盘分享的文件:JNA_linux_callback.zip
链接:https://pan.baidu.com/s/1-pjXzYlocMrsE2-vL51VqQ?pwd=scyw 
提取码:scyw

猜你喜欢

转载自blog.csdn.net/zsj777/article/details/143187907