Java code
[java] view plain copy print ?
- package zeph.example;
- import java.io.IOException;
- public class JstringExample {
-
-
-
-
- public native String stringMethod(String text);
- public static void main(String[] args) throws IOException{
-
- JstringExample Je = new JstringExample();
- System.loadLibrary("example");
- String str = "天灵灵,地灵灵,乱码乱码,消失吧!!!";
- System.out.println("the String in java is " + '"' + str + '"');
- System.out.println("the String return by C is "+Je.stringMethod(str));
- }
- }
package zeph.example; import java.io.IOException; public class JstringExample { /** * @param Jstring For Chinese * @author Ben Zeph */ public native String stringMethod(String text); public static void main(String[] args) throws IOException{ // TODO Auto-generated method stub JstringExample Je = new JstringExample(); System.loadLibrary("example"); String str = "天灵灵,地灵灵,乱码乱码,消失吧!!!"; System.out.println("the String in java is " + '"' + str + '"'); System.out.println("the String return by C is "+Je.stringMethod(str)); } }
C code example_h.h
[cpp] view plain copy print ?
- #include "jni.h"
- #ifndef _Included_example_h
- #define _Included_example_h
- #ifdef __cplusplus
- extern "C" {
- #endif
- JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *, jobject, jstring);
-
-
-
-
- #ifdef __cplusplus
- }
- #endif
- #endif
#include "jni.h" #ifndef _Included_example_h #define _Included_example_h #ifdef __cplusplus extern "C" { #endif JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *, jobject, jstring); /*Java: stringMethod *Fuction: tranform a java string to c char* *Author: Ben Zeph */ #ifdef __cplusplus } #endif #endif
C code exampledll.cpp
[cpp] view plain copy print ?
- #include "example_h.h"
- #include "jni.h"
- #include <stdio.h>
- #include <windows.h>
- char* jstringToWindows( JNIEnv *env, jstring jstr )
- {
- int length = env->GetStringLength(jstr);
- const jchar* jcstr = env->GetStringChars(jstr, 0);
- char* rtn = (char*)malloc(length*2+1);
- int size = 0;
- size = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL);
- if( size <= 0 )
- return NULL;
- env->ReleaseStringChars(jstr, jcstr);
- rtn[size] = 0;
- return rtn;
- }
- jstring WindowsTojstring( JNIEnv *env, const char* str )
- {
- jstring rtn = 0;
- int slen = strlen(str);
- unsigned short* buffer = 0;
- if( slen == 0 )
- rtn = env->NewStringUTF(str);
- else
- {
- int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, NULL, 0);
- buffer = (unsigned short*)malloc(length*2 + 1);
- if( MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length) >0)
- rtn = env->NewString((jchar*)buffer, length);
- }
- if(buffer)
- free(buffer);
- return rtn;
- }
- JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *env, jobject obj, jstring string)
- {
- const char *str ;
- jstring tostring;
- str = jstringToWindows(env, string);
- printf("The string in C code %s/n",str);
- tostring = WindowsTojstring(env,str);
- return tostring;
- }
- void main(){}
#include "example_h.h" #include "jni.h" #include <stdio.h> #include <windows.h> char* jstringToWindows( JNIEnv *env, jstring jstr ) { int length = env->GetStringLength(jstr); const jchar* jcstr = env->GetStringChars(jstr, 0); char* rtn = (char*)malloc(length*2+1); int size = 0; size = WideCharToMultiByte(CP_ACP, 0, (LPCWSTR)jcstr, length, rtn,(length*2+1), NULL, NULL); if( size <= 0 ) return NULL; env->ReleaseStringChars(jstr, jcstr); rtn[size] = 0; return rtn; } jstring WindowsTojstring( JNIEnv *env, const char* str ) { jstring rtn = 0; int slen = strlen(str); unsigned short* buffer = 0; if( slen == 0 ) rtn = env->NewStringUTF(str); else { int length = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, NULL, 0); buffer = (unsigned short*)malloc(length*2 + 1); if( MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, length) >0) rtn = env->NewString((jchar*)buffer, length); } if(buffer) free(buffer); return rtn; } JNIEXPORT jstring JNICALL Java_zeph_example_JstringExample_stringMethod(JNIEnv *env, jobject obj, jstring string) { const char *str ; jstring tostring; str = jstringToWindows(env, string); printf("The string in C code %s/n",str); tostring = WindowsTojstring(env,str); return tostring; } void main(){}
C中的两个函数
char* jstringToWindows( JNIEnv *env, jstring jstr )
jstring WindowsTojstring( JNIEnv *env, const char* str )
转载于其他文章,在此感谢其作者
代码中需要的两个头文件jni.h和jni_md.h
到这里下载http://download.csdn.net/source/3186188