当前位置: 技术问答>java相关
JNI中文问题,100分送上,请笑纳
来源: 互联网 发布时间:2015-05-05
本文导语: ShowMessage.java public class ShowMessage { private native void ShowMessage(String msg); static{ System.loadLibrary("MsgImpl"); } public static void main(String[] args) { ShowMessage app = new ShowMessage(); app.ShowMessage("Generated阿瑟 with JNI"); }...
ShowMessage.java
public class ShowMessage
{
private native void ShowMessage(String msg);
static{
System.loadLibrary("MsgImpl");
}
public static void main(String[] args)
{
ShowMessage app = new ShowMessage();
app.ShowMessage("Generated阿瑟 with JNI");
}
}
ShowMessage.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class ShowMessage */
#ifndef _Included_ShowMessage
#define _Included_ShowMessage
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ShowMessage
* Method: ShowMessage
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ShowMessage_ShowMessage
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
MsgImpl.cpp
#include
#include
#include "ShowMessage.h"
#include "windows.h"
extern "C" JNIEXPORT void JNICALL
Java_ShowMessage_ShowMessage(JNIEnv* env, jobject, jstring jMsg)
{
const char* msg=env->GetStringUTFChars(jMsg,0);
printf("Thinking in Java, JNI: %sn", msg);
env->ReleaseStringUTFChars(jMsg, msg);
}
显示的是
Thinking in Java, JNI: Generated闃跨憻 with JNI
中文显示不对,请问应如何修改??
public class ShowMessage
{
private native void ShowMessage(String msg);
static{
System.loadLibrary("MsgImpl");
}
public static void main(String[] args)
{
ShowMessage app = new ShowMessage();
app.ShowMessage("Generated阿瑟 with JNI");
}
}
ShowMessage.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include
/* Header for class ShowMessage */
#ifndef _Included_ShowMessage
#define _Included_ShowMessage
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: ShowMessage
* Method: ShowMessage
* Signature: (Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ShowMessage_ShowMessage
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
MsgImpl.cpp
#include
#include
#include "ShowMessage.h"
#include "windows.h"
extern "C" JNIEXPORT void JNICALL
Java_ShowMessage_ShowMessage(JNIEnv* env, jobject, jstring jMsg)
{
const char* msg=env->GetStringUTFChars(jMsg,0);
printf("Thinking in Java, JNI: %sn", msg);
env->ReleaseStringUTFChars(jMsg, msg);
}
显示的是
Thinking in Java, JNI: Generated闃跨憻 with JNI
中文显示不对,请问应如何修改??
|
//把java的字符串转换成windows的字符串
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 ReleaseStringChars( jstr, jcstr );
rtn[size] = 0;
return rtn;
}
////把windows的字符串转换成java的字符串
jstring WindowsTojstring( JNIEnv* env, char* str )
{
jstring rtn = 0;
int slen = strlen(str);
wchar_t* buffer = 0;
if( slen == 0 )
rtn = env->NewStringUTF( str ); //UTF ok since empty string
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;
}
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 ReleaseStringChars( jstr, jcstr );
rtn[size] = 0;
return rtn;
}
////把windows的字符串转换成java的字符串
jstring WindowsTojstring( JNIEnv* env, char* str )
{
jstring rtn = 0;
int slen = strlen(str);
wchar_t* buffer = 0;
if( slen == 0 )
rtn = env->NewStringUTF( str ); //UTF ok since empty string
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;
}
|
哦?我写的jni怎么可以?!
就是这么直接用jstring,
不过不是做输出
就是这么直接用jstring,
不过不是做输出
您可能感兴趣的文章:
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。