관리 메뉴

드럼치는 프로그래머

[JNI/NDK] JNI에서 자바 기본형 파라미터를 JNI함수에 전달하는 예 본문

★─Programing/☆─JNI | NDK

[JNI/NDK] JNI에서 자바 기본형 파라미터를 JNI함수에 전달하는 예

드럼치는한동이 2013. 4. 22. 09:22
HelloJNI.java

public class HelloJNI {
static{
System.loadLibrary("my_dll");
}
/* 기본형 파라미터를 전달하는 경우 */
public native void printSize(boolean b, byte bt, short s, char c, int i, long lg, float f, double d);

public static void main(String[] args) throws Exception{
new HelloJNI().printSize(true, (byte)1, (short)2, 'A', 3, 4L, 5.0F, 6.0);
}
}




HelloJNI.h

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

#ifndef _Included_HelloJNI
#define _Included_HelloJNI
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: HelloJNI
* Method: printSize
* Signature: (ZBSCIJFD)V
*/

JNIEXPORT void JNICALL Java_HelloJNI_printSize
(JNIEnv *, jobject, jboolean, jbyte, jshort, jchar, jint, jlong, jfloat, jdouble);

#ifdef __cplusplus
}
#endif
#endif


my_dll.c

#include "HelloJNI.h"
#include <stdio.h>

JNIEXPORT void JNICALL Java_HelloJNI_printSize
(JNIEnv *env, jobject obj, jboolean b, jbyte bt, jshort s, jchar c, jint i, jlong lon, jfloat f, jdouble d){
printf("jboolean size=%d \n", sizeof(b));
printf("jbyte size=%d \n", sizeof(bt));
printf("jshort size=%d \n", sizeof(s));
printf("jchar size=%d \n", sizeof(c));
printf("jint size=%d \n", sizeof(i));
printf("jlong size=%d \n", sizeof(lon));
printf("jfloat size=%d \n", sizeof(f));
printf("jdouble size=%d \n", sizeof(d));
return;
}





자바 클래스 실행결과

jboolean size=1
jbyte size=1
jshort size=2
jchar size=2
jint size=4
jlong size=8
jfloat size=4
jdouble size=8


 

[출처] http://micropilot.tistory.com/category/J%20N%20I?page=5

Comments