Between Java and C transmission through JNI Chinese string and hash issues

  Between Java and C transmission through JNI Chinese string 

  Introduction 

  This article provides a concrete example that between Java and C transmission through JNI Chinese string containing the parameter to solve Chinese hash (or double-byte hash bytes).    This paper presents specific source code, the source code for an explanation, Part C provides C and C + + in two ways to achieve the integrity of the code.    And all code are in the experimental environment through testing.    In this paper, readers need to Java and C (or C + +) have some basic knowledge. 

  Experimental Environment 

  Windows XP (or Windows2000) 

  J2sdk1.4.2/j2re1.4.2 

  Microsoft VisualC + + 6.0 

  J2sdk installed after the need to set environment variables 

  CLASSPATH =. C: \ j2sdk1.4.2 \ bin; C: \ j2sdk1.4.2 \ lib \ dt.jar; C: \ j2sdk1.4.2 \ lib \ tools.jar; C: \ j2sdk1.4.2 \ lib \ htmlconverter. jar (j2sdk installed in c: \ j2sdk1.4.2 directory) 

  Source code and the code 

  Java code: 

  / * 

  * Javactransfer.java 

  * By dpwu 

  * E-mail: dpwu_js@sina.com.cn 

  * / 

  Public class javactransfer 

  ( 

  Public String hypotenuse (String send_buf, String recv_buf, int errno) 

  ( 

  Return hypotenuse0 (send_buf, recv_buf, errno); 

  ) 

  Private native String hypotenuse0 (String send_buf, String recv_buf, int errno); 

  Static 

  ( 

  System.loadLibrary ( "javactransfer ");// call dll 

  ) 

  Public static void main (String [] args) 

  ( 

  Javactransfer obj = new javactransfer (); 

  System.out.println (""); 

  System.out.println (""); 

  System.out.println ( "begin!"); 

  System.out.println (""); 

  System.out.println (""); 

  String javasend = "sent Chinese chinese!"; 

  System.out.println ( "java send :"+"["+ javasend +"]"); 

  String javarecv = obj.hypotenuse ( "teststr" javasend, 1); 

  / / Javasend-containing Chinese string to C 

  / / C javarecv accept the text string containing 

  System.out.println ( "java recv :"+"["+ javarecv +"]"); 

  System.out.println (""); 

  System.out.println (""); 

  System.out.println ( "end!"); 

  ) 

  ) 

  C codes: 

  / * 

  * Javactransfer.c 

  * By dpwu 

  * E-mail: dpwu_js@sina.com.cn 

  * / 

  # Include <windows.h> 

  # Include "javactransfer.h" / / through javah-jni javactransfer Generation 

  # Include <stdio.h> 

  # Include "stdlib.h" 

  # Include "string.h" 

  Char * jstringToWindows (JNIEnv * env, jstring jstr); 

  Jstring WindowsTojstring (JNIEnv * env, char * str); 

  JNIEXPORT jstring JNICALL 

  Java_javactransfer_hypotenuse0 (JNIEnv * env, jobject obj, jstring send_buf, jstring recv_buf, jint errno) 

  ( 

  Char * Buf_Return; 

  Buf_Return = (char *) malloc (1024); 

  Const char * recvtest = jstringToWindows (env, recv_buf); 

  / * Java-sentence recvtest receive from the Chinese string correctly, if read the sentence, while a hash: 

  Const char * recvtest = (* env) -> GetStringUTFChars (env, recv_buf, 0); 

  * / 

  Printf ( "c recv: [% s] \ n", recvtest); 

  Sprintf (Buf_Return, "receiver Chinese chinese!"); 

  Printf ( "\ n \ n \ nc send: [% s] \ n", Buf_Return); 

  Recv_buf = WindowsTojstring (env, Buf_Return); 

  / * Sentence recv_buf C-windows in the local Chinese string containing Java to correct if the sentence to a hash: 

  Recv_buf = (* env) -> NewStringUTF (env, Buf_Return); 

  * / 

  Return recv_buf; 

  ) 

  Char * jstringToWindows (JNIEnv * env, jstring jstr) 

  ( 

  Int length = (* env) -> GetStringLength (env, jstr); 

  Const jchar jcstr = * (* env) -> GetStringChars (env, 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 (env, jstr, jcstr); 

  Rtn [size] = 0; 

  Return rtn; 

  ) 

  Jstring WindowsTojstring (JNIEnv * env, char * str) 

  ( 

  Jstring rtn = 0; 

  Int slen = strlen (str); 

  Unsigned short * buffer = 0; 

  If (slen == 0) 

  Rtn = (* env) -> NewStringUTF (env, str); 

  Else 

  ( 

  Int length = MultiByteToWideChar (CP_ACP, 0, (LPCSTR) str, slen, NULL, 0); 

  Buffer = malloc (length * 2 + 1); 

  If (MultiByteToWideChar (CP_ACP, 0, (LPCSTR) str, slen, (LPWSTR) buffer, length)> 0) 

  Rtn = (* env) -> NewString (env, (jchar *) buffer, length); 

  ) 

  If (buffer) 

  Free (buffer); 

  Return rtn; 

  ) 

  If javactransfer.java, javactransfer.c are on the d: \ directory javac 

  In order operators under the following steps: 

  Javac javactransfer.java: java compiler source code; 

  Javah-jni javactransfer: generating header files; 

  Cl-Ic: \ j2sdk1.4.2 \ include-Ic: \ j2sdk1.4.2 \ include \ win32-LD javactransfer.c-Fejavactransfer.dll 

  Through Microsoft Visual C + + source code will be generated C dll file for java call. 

  The results are as follows: 

  Type: java javactranfer can see the result 

  C + + code: 

  / * 

  * Javactransfer.cpp 

  * By dpwu 

  * E-mail: dpwu_js@sina.com.cn 

  * / 

  # Include <windows.h> 

  # Include "javactransfer.h" / / through javah-jni javactransfer Generation 

  # Include <stdio.h> 

  # Include "stdlib.h" 

  # Include "string.h" 

  Char * jstringToWindows (JNIEnv * env, jstring jstr); 

  Jstring WindowsTojstring (JNIEnv * env, char * str); 

  JNIEXPORT jstring JNICALL 

  Java_javactransfer_hypotenuse0 (JNIEnv * env, jobject obj, jstring send_buf, jstring recv_buf, jint _tperrno) 

  ( 

  Char * Buf_Return; 

  Buf_Return = (char *) malloc (1024); 

  Const char * ctest = jstringToWindows (env, recv_buf); 

  / * 

  If the last sentence should read the sentence, C Java string receiver will appear hash: 

  Const char * ctest = (env) -> GetStringUTFChars (recv_buf, 0); 

  * / 

  Printf ( "c recv: [% s] \ n", ctest); 

  Sprintf (Buf_Return, "receiver Chinese chinese!"); 

  Printf ( "\ n \ n \ nc send: [% s] \ n", Buf_Return); 

  Recv_buf = WindowsTojstring (env, Buf_Return); 

  / * The implementation of the correct sentence, if the sentence changed to the next sentence in the receiving C Java, a string of gibberish 

  Recv_buf = (env) -> NewStringUTF (Buf_Return); 

  * / 

  Return recv_buf; 

  ) 

  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, 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; 

  ) 

  When the C + +, when only need to compile the following: 

  Cl-Ic: \ j2sdk1.4.2 \ include-Ic: \ j2sdk1.4.2 \ include \ win32-LD javactransfer.cpp-Fejavactransfer.dll 

  After so 

  In this paper can be applied to complex systems as the transformation of existing large-scale production systems (for example: financial, etc.) are mostly unix C + + based on the shared memory tuxedo materialize, and these systems to meet the requirements of security and not in response time completely discarded the short term, this paper provides the way to the client through the tuxedo and VC + + package into various dll, dll in the tuxedo many fine characteristics of its own on the basis of Java-based development to solve new to WEB-based applications.    If wrong, and the author please contact: dpwu_js@sina.com.cn. 

  References: David Wendt, "NLS strings and JNI" 

  WebSphere programmer, IBM 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: , , , , ,