用JNA调用C/C++,很方便,写了个很简单的例子。
例子是使用Eclipse CDT + MinGW开发的:
C代码,hello.c
#include
<
windows.h
>
#include
"
stdio.h
"
void
say(){
MessageBox (NULL, TEXT (
"
你好, Windows!
"
), TEXT (
"
HelloMsg
"
),
0
);
}
将hello.c编译成libDLL2.dll,放进java的项目文件夹中,java调用方式
Java代码,dll.java
public
class
Dll {
public
interface
TestDll1
extends
Library {
TestDll1 INSTANCE
=
(TestDll1)Native.loadLibrary(
"
libDLL2
"
, TestDll1.
class
);
public
void
say();
}
public
static
void
main(String[] args) {
TestDll1.INSTANCE.say();
}
}
先写一个接口
TestDll1映射C的方法,再经过这接口调用
say(),在eclipse中编译运行这个java代码,能够看到弹出“你好,window!”的窗口。