vs2013封装dll以及java调用

public class SVBufferAppend {
    public static native int byteAppend(byte[] svBuffer, int offset,int ct,int cl,int vl,int value);

}

编译成.class文件   javah -classpath . -jni SVBufferAppend   生成.h文件  如下

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class SVBufferAppend */

#ifndef _Included_SVBufferAppend
#define _Included_SVBufferAppend
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     SVBufferAppend
 * Method:    byteAppend
 * Signature: ([BIIIII)I
 */
JNIEXPORT jint JNICALL Java_SVBufferAppend_byteAppend
  (JNIEnv *, jclass, jbyteArray, jint, jint, jint, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

用VS新建win32 的项目   配置如下

选择  程序类型  DLL     勾上导出符号      去掉 安全开发生命周期检查的勾(SDL)

右击项目选择属性    设置平台  X64  

配置属性 常规  配置类型  dll动态库

vc++目录   选择包含目录  C:\java\jdk1.7.0_80\include   C:\java\jdk1.7.0_80\include\win32

                  库 目录     C:\java\jdk1.7.0_80\lib

然后包含之前生成的 .h文件 

编写对应的方法代码

JNIEXPORT jint JNICALL Java_SVBufferAppend_byteAppend
(JNIEnv * a, jclass b, jbyteArray c, jint offset, jint ct, jint cl, jint vl, jint value){

    /*
	jbyte* bytebuf = new jbyte[byteArrayLength];

	//JAVA to DLL
	a->GetByteArrayRegion(c, 0, byteArrayLength, bytebuf);
	
	printf("%x\n", *bytebuf);
	printf("%x\n", *(bytebuf+1));
	printf("%x\n", *(bytebuf+2));
	
	//DLL set to JAVA
	//a->SetByteArrayRegion(c, 0, byteArrayLength, bytebuf);
	
	delete[] bytebuf;
    */

	// 获取传过来的数组长度
	int byteArrayLength = a->GetArrayLength(c);
	printf(" byteArrayLength=%d\n", byteArrayLength);

    // 获取数组操作
	jbyte* bytebuf = a->GetByteArrayElements(c, JNI_FALSE);

	
	a->ReleaseByteArrayElements(c, bytebuf, 0);

	return 0;

}

最后java直接使用方法名调用即可

猜你喜欢

转载自blog.csdn.net/qq_34232027/article/details/84494730