⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 java与cc++之间通过jni传递中文字符串 - 爱睇资讯 - 博客园.htm

📁 java调用JNI中解决中文符串问题
💻 HTM
📖 第 1 页 / 共 5 页
字号:
recv_buf;<BR>}<BR>char* jstringToWindows( JNIEnv&nbsp;&nbsp;*env, jstring jstr 
)<BR>{<BR>&nbsp;&nbsp;int length = (env)-&gt;GetStringLength(jstr 
);<BR>&nbsp;&nbsp;const jchar* jcstr = (env)-&gt;GetStringChars(jstr, 0 
);<BR>&nbsp;&nbsp;char* rtn = (char*)malloc( length*2+1 );<BR>&nbsp;&nbsp;int 
size = 0;<BR>&nbsp;&nbsp;size = WideCharToMultiByte( CP_ACP, 0, (LPCWSTR)jcstr, 
length, rtn,(length*2+1), NULL, NULL );<BR>&nbsp;&nbsp;if( size &lt;= 0 
)<BR>&nbsp; &nbsp; return 
NULL;<BR>&nbsp;&nbsp;(env)-&gt;ReleaseStringChars(jstr, jcstr 
);<BR>&nbsp;&nbsp;rtn[size] = 0;<BR>&nbsp;&nbsp;return rtn;<BR>}<BR><BR>jstring 
WindowsTojstring( JNIEnv* env, char* str )<BR>{<BR>&nbsp;&nbsp;jstring rtn = 
0;<BR>&nbsp;&nbsp;int slen = strlen(str);<BR>&nbsp;&nbsp;unsigned short * buffer 
= 0;<BR>&nbsp;&nbsp;if( slen == 0 )<BR>&nbsp; &nbsp; rtn = 
(env)-&gt;NewStringUTF(str ); <BR>&nbsp;&nbsp;else<BR>&nbsp;&nbsp;{<BR>&nbsp; 
&nbsp; int length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 
);<BR>&nbsp; &nbsp; buffer = (unsigned short *)malloc( length*2 + 1 );<BR>&nbsp; 
&nbsp; if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, 
length ) &gt;0 )<BR>&nbsp; &nbsp;&nbsp; &nbsp;rtn = 
(env)-&gt;NewString(&nbsp;&nbsp;(jchar*)buffer, length 
);<BR>&nbsp;&nbsp;}<BR>&nbsp;&nbsp;if( buffer )<BR>&nbsp;&nbsp;free( buffer 
);<BR>&nbsp;&nbsp;return rtn;<BR>}<BR>当为C++实现时,只需要在编译时改为:<BR>cl 
-Ic:\j2sdk1.4.2\include -Ic:\j2sdk1.4.2\include\win32 -LD javactransfer.cpp 
-Fejavactransfer.dll 
<BR><BR>后话<BR>本文所述内容可以应用于复杂系统的改造,现有大型生产系统(例如:金融等)很多都是unix+C+tuxedo基于共享内存实现,而这些系统为了满足安全和响应时间要求不可能在短期内完全丢弃,本文中提供的方法,可以和tuxedo客户端通过VC++封装成各种dll,这些dll可以在tuxedo本身很多优良特性的基础上进行基于Java的开发,以用来解决新的基于WEB应用的需求。如有错误之处,请与笔者联系:<A 
href="mailto:dpwu_js@sina.com.cn"><FONT 
color=#003366>dpwu_js@sina.com.cn</FONT></A>。<BR><BR>参考资料:David Wendt 《NLS 
strings and JNI》<BR>WebSphere programmer, IBM<BR><BR><BR>下面是我在这个基础上完成的C++ Call 
Java的反向例程:<BR><STRONG>C++传递中文字符给java<BR><BR></STRONG>/*<BR>* Chinese.java<BR>* 
By Rookie.Zhang<BR>* e-mail:<A href="mailto:rookieport@msn.com"><FONT 
color=#003366>rookieport@msn.com</FONT></A><BR>*/<BR>public class 
Chinese<BR>{<BR>&nbsp; &nbsp; &nbsp; &nbsp; private static void sayHello(String 
name){<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
System.out.println("Hello: " + name);<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
}<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; public static void main(String[] 
args){<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int idx = 
0; idx &lt; args.length; idx ++){<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sayHello(args[idx]);<BR>&nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
}<BR>};<BR><BR>/*<BR>* jnidemo.cpp<BR>* By Rookie.Zhang<BR>* e-mail:<A 
href="mailto:rookieport@msn.com"><FONT 
color=#003366>rookieport@msn.com</FONT></A><BR>*<BR>*/<BR>/*<BR>* 
环境:jdk1.5.0_05+vc7+winxp<BR>* Usage:<BR>1。编译<BR>cl -I%JAVA_HOME%\include 
-I%JAVA_HOME%\include\win32 jnidemo.cpp /link 
%JAVA_HOME%\lib\jvm.lib<BR>2。设置JVM路径<BR>set 
path=%JAVA_HOME%\jre\bin\client;%PATH%<BR>3。运行<BR>jnidemo<BR>4. 输出<BR>Hello: 
Rookie.张[中文]<BR>*/<BR><BR>#include <WINDOWS.H><BR>#include <JNI.H><BR>#include 
<STDLIB.H><BR><BR>jstring WindowsTojstring( JNIEnv* env, char* str );<BR><BR>int 
main()<BR>{<BR>&nbsp; &nbsp; &nbsp; &nbsp; JavaVMOption options[1];<BR>&nbsp; 
&nbsp; &nbsp; &nbsp; JavaVMInitArgs vm_args;<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
JavaVM *jvm;<BR>&nbsp; &nbsp; &nbsp; &nbsp; JNIEnv *env;<BR>&nbsp; &nbsp; &nbsp; 
&nbsp; long status;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; jclass 
class_Chinese;<BR>&nbsp; &nbsp; &nbsp; &nbsp; jclass class_String;<BR>&nbsp; 
&nbsp; &nbsp; &nbsp; jobjectArray args;<BR>&nbsp; &nbsp; &nbsp; &nbsp; jmethodID 
id_main;<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; options[0].optionString = 
"-Djava.class.path=.";<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; memset(&amp;vm_args, 
0, sizeof(vm_args));<BR>&nbsp; &nbsp; &nbsp; &nbsp; vm_args.version = 
JNI_VERSION_1_2;<BR>&nbsp; &nbsp; &nbsp; &nbsp; vm_args.nOptions = 1;<BR>&nbsp; 
&nbsp; &nbsp; &nbsp; vm_args.options = options;<BR><BR>&nbsp; &nbsp; &nbsp; 
&nbsp; status = JNI_CreateJavaVM(&amp;jvm, (void**)&amp;env, 
&amp;vm_args);<BR>&nbsp; &nbsp; &nbsp; &nbsp; if (status == JNI_ERR)<BR>&nbsp; 
&nbsp; &nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; printf("Error creating VM\n");<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; return 1;<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR><BR>&nbsp; 
&nbsp; &nbsp; &nbsp; class_Chinese = (*env).FindClass("Chinese");<BR>&nbsp; 
&nbsp; &nbsp; &nbsp; id_main = (*env).GetStaticMethodID(class_Chinese, "main", 
"([Ljava/lang/String;)V");<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; class_String = 
(*env).FindClass("java/lang/String");<BR>&nbsp; &nbsp; &nbsp; &nbsp; // Call 
main(String[]{""Rookie.张[中文]"})<BR>&nbsp; &nbsp; &nbsp; &nbsp; args = 
(*env).NewObjectArray(1, class_String, WindowsTojstring(env, 
"Rookie.张[中文]"));<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; 
(*env).CallStaticVoidMethod(class_Chinese, id_main, args);<BR><BR>&nbsp; &nbsp; 
&nbsp; &nbsp; (*jvm).DestroyJavaVM();<BR><BR>&nbsp; &nbsp; &nbsp; &nbsp; return 
0;<BR>}&nbsp;&nbsp;<BR><BR>// 修改自dpwu's代码<BR>jstring WindowsTojstring( JNIEnv* 
env, char* str )<BR>{<BR>&nbsp; &nbsp; &nbsp; &nbsp; jstring rtn = 0;<BR>&nbsp; 
&nbsp; &nbsp; &nbsp; int slen = strlen(str);<BR>&nbsp; &nbsp; &nbsp; &nbsp; 
unsigned short* buffer = 0;<BR>&nbsp; &nbsp; &nbsp; &nbsp; if( slen == 0 
)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rtn = 
(*env).NewStringUTF(str );<BR>&nbsp; &nbsp; &nbsp; &nbsp; else<BR>&nbsp; &nbsp; 
&nbsp; &nbsp; {<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int 
length = MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, NULL, 0 );<BR>&nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; buffer = (unsigned short 
*)malloc( length*2 + 1 );<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; if( MultiByteToWideChar( CP_ACP, 0, (LPCSTR)str, slen, (LPWSTR)buffer, 
length ) &gt;0 )<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 
&nbsp; &nbsp; &nbsp; &nbsp; rtn = (*env).NewString( (jchar*)buffer, length 
);<BR>&nbsp; &nbsp; &nbsp; &nbsp; }<BR>&nbsp; &nbsp; &nbsp; &nbsp; if( buffer 
)<BR>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; free( buffer 
);<BR>&nbsp; &nbsp; &nbsp; &nbsp; return rtn;<BR>}<BR>
<DIV class=postDesc>posted on 2005-10-18 13:50 <A 
href="http://www.cnblogs.com/rookieport/">Rookie.Zhang</A> 阅读(990) <A 
href="http://www.cnblogs.com/rookieport/archive/2005/10/18/257154.html#Post">评论(0)</A> 
&nbsp;<A 
href="http://www.cnblogs.com/rookieport/admin/EditPosts.aspx?postid=257154">编辑</A> 
<A href="http://www.cnblogs.com/rookieport/AddToFavorite.aspx?id=257154">收藏</A> 
<A onclick="PutInWz();return false;" 
href="http://www.cnblogs.com/rookieport/archive/2005/10/18/257154.html#">网摘</A> 
所属分类: <A href="http://www.cnblogs.com/rookieport/category/26205.html">Java技术</A> 
</DIV></DIV><IMG height=1 
src="Java与CC++之间通过JNI传递中文字符串 - 爱睇资讯 - 博客园.files/257154.jpg" width=1> <!--
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
<rdf:Description
rdf:about="http://www.cnblogs.com/rookieport/archive/2005/10/18/257154.html"
dc:identifier="http://www.cnblogs.com/rookieport/archive/2005/10/18/257154.html"
dc:title="Java与C\C++之间通过JNI传递中文字符串"
trackback:ping="http://www.cnblogs.com/rookieport/services/trackbacks/257154.aspx" />
</rdf:RDF>
-->
<DIV id=AjaxHolder_UpdatePanel1>
<STYLE>TD {
	FONT-SIZE: 12px
}
.commentTextBox {
	FONT-SIZE: 13px; FONT-FAMILY: Verdana
}
A.blue:visited {
	COLOR: blue
}
A.blue:active {
	COLOR: blue
}
A.blue:link {
	COLOR: blue
}
A.blue:hover {
	COLOR: blue
}
</STYLE>
<!--Beging Temp Save-->
<STYLE>.userData {
	BEHAVIOR: url(#default#userdata)
}
</STYLE>

<DIV class=userData id=CommentsPersistDiv></DIV>
<SCRIPT type=text/javascript>
function pageLoad()
{
    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(handleInitializeRequest);
    //Sys.WebForms.PageRequestManager.getInstance().add_endRequest(handleEndRequest);
}

function handleInitializeRequest(sender, args)
{
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    var eid = args.get_postBackElement().id;
    /*if (eid.indexOf("DeleteLink")>0) 
    {
       args.get_postBackElement().innerHTML = "<font color='red'>正在删除...</font>";         
    }    
    else */
    if (eid.indexOf("btnSubmit")>0) 
    {
       document.getElementById("AjaxHolder_PostComment_ltSubmitMsg").innerHTML="正在提交...";
       document.getElementById("AjaxHolder_PostComment_btnSubmit").disabled = true;       
    }   
    else if(eid.indexOf("refreshList")>0)
    {
        document.getElementById("AjaxHolder_PostComment_refreshList").innerHTML="<font color='red'>正在刷新...</font>";
    }
   
}	

function TempSave(ElementID)
{
	try
	{
	CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
	CommentsPersistDiv.save("CommentXMLStore");
	}
	catch(ex)
	{
	}
	
}
function Restore(ElementID)
{
	CommentsPersistDiv.load("CommentXMLStore");
	document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
}	
			
			
</SCRIPT>
<!--Ene TempSave-->
<DIV id=divRefreshComments 
style="FONT-SIZE: 12px; MARGIN-BOTTOM: 5px; MARGIN-RIGHT: 10px; TEXT-ALIGN: right"><A 
href="http://book.cnblogs.com/zt/">图书专题</A>&nbsp;&nbsp;<A 
href="http://www.cnblogs.com/RequireRegister.aspx">新用户注册</A>&nbsp;&nbsp;<A 
id=AjaxHolder_PostComment_refreshList 
href="javascript:__doPostBack('AjaxHolder$PostComment$refreshList','')">刷新评论列表</A>&nbsp;&nbsp;</DIV>
<DIV class=commentform><SPAN id=AjaxHolder_PostComment_ltSubmitMsg 
style="COLOR: red"></SPAN><BR><A name=Feedback></A>
<TABLE style="WIDTH: 600px" cellSpacing=1 cellPadding=1 border=0>
  <TBODY>
  <TR>
    <TD colSpan=3><BR>
      <SCRIPT type=text/javascript><!--
google_ad_client = "pub-4210569241504288";
/* cnblogs_comment_box_top_468x60 */
google_ad_slot = "7468723776";
google_ad_width = 468;
google_ad_height = 60;
//-->
</SCRIPT>

      <SCRIPT src="Java与CC++之间通过JNI传递中文字符串 - 爱睇资讯 - 博客园.files/show_ads.js" 
      type=text/javascript>
</SCRIPT>
      <BR><BR></TD></TR>
  <TR>
    <TD></TD>
    <TD></TD>
    <TD></TD></TR>
  <TR>
    <TD width=55>标题</TD>
    <TD colSpan=2><INPUT class=commenttb id=AjaxHolder_PostComment_tbTitle 
      value="re: Java与C\C++之间通过JNI传递中文字符串" 
      name=AjaxHolder$PostComment$tbTitle><SPAN 
      id=AjaxHolder_PostComment_RequiredFieldValidator1 
      style="VISIBILITY: hidden; COLOR: red">请输入标题</SPAN></TD></TR>
  <TR>
    <TD>姓名</TD>
    <TD colSpan=2><INPUT class=commenttb id=AjaxHolder_PostComment_tbName 
      name=AjaxHolder$PostComment$tbName><SPAN 
      id=AjaxHolder_PostComment_RequiredFieldValidator2 
      style="VISIBILITY: hidden; COLOR: red">请输入你的姓名</SPAN></TD></TR>
  <TR>
    <TD>主页</TD>
    <TD><INPUT class=commenttb id=AjaxHolder_PostComment_tbUrl 
      name=AjaxHolder$PostComment$tbUrl></TD>
    <TD></TD></TR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -