java调用dll
首选写dll
# include <Windows.h>
_declspec(dllexport) char* go(char *ch)
{
MessageBox(NULL, TEXT(ch), TEXT("你好"), MB_OK);
return "来自dll的问候";
}
注意改成动态库再生成
java调用
复制生成的dll到java项目的根目录
然后写调用代码
import com.sun.jna.Library;
import com.sun.jna.Native;
public class JNAUtilsTest {
public interface Dll extends Library {
JNAUtilsTest.Dll INSTANCE = (JNAUtilsTest.Dll) Native.loadLibrary("go", JNAUtilsTest.Dll.class);// 加载动态库文件
String go(String s);
}
public static void main(String[] args){
System.setProperty("jna.encoding", "GBK");//解决中文乱码
String go = Dll.INSTANCE.go("777");
System.out.println(go);
}
}
结果: