Java calls C + + DLL with garbled language

Source: Internet
Author: User

Recent developments have used the ability to invoke the native dynamic connection library using Java, and the file path is invoked through Java to invoke C + + code to manipulate the file.

A failure is assumed in the call to include Chinese characters in the path. The execution of the program is aborted.

The following is a sample to illustrate the documented solution.

A string is passed in Java and the C + + code is called to output the string

public class Commonutil{static{system.loadlibrary ("Nativetest");} Public native static void Print (String str);p ublic static void Main (string args[]) {commonutil.print ("Chinese garbled");}}

Run the Javac Commonutil.java and Javah commonutil two commands. A CommonUtil.h C + + header file is generated. CommonUtil.h source code such as the following

/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Commonutil */#ifndef _INCLU Ded_commonutil#define _included_commonutil#ifdef __cplusplusextern "C" {#endif/* * Class:     commonutil * Method:    Print * Signature: (ljava/lang/string;) V */jniexport void Jnicall java_commonutil_print  (jnienv *, Jclass, jstring); #ifdef __cplusplus} #endif #endif

Use VS2005 to create a new C + + DLL for project. Add CommonUtil.h to the project. Create a new. cpp file to implement the Java_commonutil_print function. Implementation code such as the following:

#include "CommonUtil.h" #include <iostream>using namespace std; Jniexport void Jnicall java_commonutil_print (jnienv *env, Jclass obj, jstring jstr) {const char *LOCALSTR = Env->getstri Ngutfchars (jstr,null); Cout<<localstr<<endl;}
In the compilation need to increase the Java comes with the C + + header file, otherwise like jnienv this kind will not find, I use is jdk1.6, so the "C:\Program files\java\jdk1.6.0_10\include; C:\Program Files\java\jdk1.6.0_10\include\win32 "into the project properties.


Compiled into a DLL. Copy the DLL into the same directory as the. class you just compiled (as a simple test, no package is used.) Assume a slightly different scenario for using the package).

Run the command Java commonutil output such as the following


Now it is completely clear that garbled characters, but the web has to convert Java UTF code to gb2312.

Here is the code for the conversion, code Source: http://blog.csdn.net/yiyaaixuexi/article/details/6173592

char* jstringtowindows (jnienv *env, jstring jstr) {//utf8/16 converted to gb2312  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;}
Change the java_commonutil_print to such as the following:

Jniexport void Jnicall java_commonutil_print (jnienv *env, Jclass obj, jstring jstr) {char *localstr = jstringtowindows (env , jstr); Cout<<localstr<<endl;free (LOCALSTR);}
Once again, the generated DLL is then copied to the directory where the. class resides.

Running Java commonutil


Perform normal


Java calls C + + DLL with garbled language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.