JNA调用c/c++库示例源码
mynative.cpp文件内容如下:// mynative.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
extern "C" __declspec(dllexport) void say()
{
printf("HELLO java user native function");
}
extern "C" __declspec(dllexport) int get(int a)
{
printf("JAVA user c/c++ native function !!!!");
return a+100;
}
对函数get的声明前面添加了__declspec(dllexport)语句。这个语句的含义是声明函数get为DLL的导出函数。DLL内的函数分为两种:
(1)DLL导出函数,可供应用程序调用;
(2) DLL内部函数,只能在DLL程序使用,应用程序无法调用它们。
java代码如下:
package test;
import com.sun.jna.Library;
import com.sun.jna.Native;
interface CLibrary extends Library {
CLibrary instance = (CLibrary) Native.loadLibrary(
"mynative", CLibrary.class);
//==============================================dll public method s (dll发布的函数 用DEPENDS.EXE进行查看dll发布了哪些函数)
public void say();
public int get(int a);
}
public class Test {
public static void main(String[] args) {
try {
System.out.println(CLibrary.instance.getClass());
CLibrary.instance.say();
int a = CLibrary.instance.get(50);
System.out.println(a);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
页:
[1]