Java JNA (二)—— dll回调函数实现

java调用dll文件需要使用回调函数作为公开函数的参数时,用以下方法实现:

首先,看c++中定义的dll公开函数:

typedef void (*ccback)(char *name ,int length);  
int dllFunction(char *ip, int port, ccback cc); 

引用dll文件方法不再赘述,直接看回调函数的实现,需要通过java的接口实现:

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Callback;
import com.test.DllCallBackTest.JnaCallBackDll.SCBack_Impl;

public class DllCallBackTest {

	public interface JnaCallBackDll extends Library {

		public static JnaCallBackDll instance = (JnaCallBackDll) Native.loadLibrary("src/main/resources/Dll2.dll",
				JnaCallBackDll.class);

		int dllFunction(String host, int port, Callback callback);

		public interface SCBack extends Callback {
			public void MessageHandle(String name, int length);
		}

		public static class SCBack_Impl implements SCBack {
			public void MessageHandle(String name, int length) {
				// TODO Auto-generated method stub
				System.out.println("回调成功!");
				// 此处添加需要的数据处理操作
			}
		}
	}

	public static void main(String[] args) {

		DllCallBackTest.JnaCallBackDll.SCBack callback = new SCBack_Impl();

		JnaCallBackDll.instance.dllFunction("127.0.0.1", 1234, callback);
	}

}

参照:

https://blog.csdn.net/qdqht2009/article/details/50592192

https://www.jb51.net/article/126546.htm

https://blog.csdn.net/qq_31997407/article/details/78247076?locationNum=8&fps=1

你可能感兴趣的:(Java,java,web)