'jstring'에 해당되는 글 1건

  1. 2011.06.02 JNI string type 다루는 법 4
Android/App개발2011. 6. 2. 13:40


JNI, jstring type example
Java의 String 을 JNI에서 다루는 예

JNI Functions Reference : http://download.oracle.com/javase/1.4.2/docs/guide/jni/spec/functions.html#wp17314

JNITest.java

public class  JNITest
{
 static{
  System.loadLibrary("my_dll");
 }

 public native String greeting(String name);

 public static void main(String[] args) 
 {
  JNITest test = new JNITest();
  String result = test.greeting("Smith");
  System.out.println("C 함수 리턴값:  "+result);
 }
}



JNITest.h

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

#ifndef _Included_JNITest
#define _Included_JNITest
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     JNITest
 * Method:    greeting
 * Signature: (Ljava/lang/String;)Ljava/lang/String;
 */

JNIEXPORT jstring JNICALL Java_JNITest_greeting
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif





my_dll.c

#include <stdio.h>
#include "JNITest.h"
#include <string.h>

JNIEXPORT jstring JNICALL Java_JNITest_greeting
  (JNIEnv *env, jobject obj, jstring jstr) {

 const char *name = (*env)->GetStringUTFChars(env, jstr, NULL);//Java String to C Style string
 char msg[60] = "Hello ";
 jstring result;

 strcat(msg, name);
 (*env)->ReleaseStringUTFChars(env, jstrname);
 puts(msg);

 result = (*env)->NewStringUTF(env, msg); // C style string to Java String
 return result;

}



Posted by 삼스