} This method is rarely used!
Next, let's take a look at how to operate strings in Java in C/C ++.First, let's take a look at some C ++ methods in JNIEnv:
1. Get the length of the string:
Parameter: j_msg: A jstring object
Env-> GetStringLength (jstring j_msg );
2. Copy the jstring object to the const jchar * pointer string
Parameter: j_msg: A jstring object. start indicates the start position of the string to be copied, end indicates the end position of the string to be copied, and jstr indicates the target pointer string.
Env-> GetStringRegion (jstring j_msg, int start, int end, const jchar * jstr );
3. Generate a jstring object
Parameter: jstr is a string pointer and size is the string length.
This method can be considered to convert the string pointer jstr into a string object jstring
Env-> NewString (const jchar * jstr, int size );
4. Convert the jstring object to const jchar * string pointer
Parameter: j_msg is a string object.
Env-> GetStringChars (jstring j_msg, NULL );
The corresponding method for releasing the memory pointer is as follows:
Parameter: j_msg is a jstring object, and jstr is a string pointer.
ReleaseStringChars (jstring j_msg, const jchar * jstr );
5. Convert the jstring object to const jchar * string pointer
Parameter: j_msg is a string object.
This method is the same as the fourth method.
Env-> GetStringCritical (jstring j_msg, NULL );
The corresponding method for releasing the memory pointer is as follows:
Env-> ReleaseStringCritical (jstring j_msg, const jchar * jstr );
Let's take a look at the example below: Define a String attribute in Java, enter a value in the console, and then define a local method callCppFunction. In C ++, the implementation of this method is: obtain this string attribute in Java, perform reverse operations on it, and then output it in Java:
Let's take a look at the code in Java:
[Java]
- Package com. jni. demo;
-
- Import java. io. BufferedReader;
- Import java. io. InputStreamReader;
-
- Public class JNIDemo {
-
- // Define a local method
- Public native void callCppFunction ();
- // Define a String attribute
- Public String msg = null;
- Public static void main (String [] args) throws Exception {
- // Call the dynamic link library
- System. loadLibrary (JNIDemo );
- // Obtain the value from the console
- BufferedReader reader = new BufferedReader (new InputStreamReader (System. in ));
- String str = reader. readLine ();
- JNIDemo jniDemo = new JNIDemo ();
- JniDemo. msg = str;
- JniDemo. callCppFunction ();
- System. out. println (jniDemo. msg );
-
- }
-
- } Let's take a look at the C ++ code:[Cpp]View plaincopy
- # Include
- # Define decom_jni_demo_jnidemo.h
- # Includewindows. h
- # Include
- # Include
- Using namespace std;
-
- JNIEXPORT void JNICALL Java_com_jni_demo_JNIDemo_callCppFunction (JNIEnv * env, jobject obj)
- {
- // Obtain the attributes in java: msg
- JfieldID fid_msg = env-> GetFieldID (env-> GetObjectClass (obj), msg, Ljava/lang/String ;);
- // Get the object of the msg attribute
- Jstring j_msg = (jstring) env-> GetObjectField (obj, fid_msg );
-
- /** Method 1: START */
- /*
- // Obtain the string pointer
- Const jchar * jstr = env-> GetStringChars (j_msg, NULL );
- // Convert to a wide string
- Wstring wstr (const wchar_t *) jstr );
- // Release the pointer
- Env-> ReleaseStringChars (j_msg, jstr );
- */
- /** First method END */
-
- /** Method 2 START */
- /*
- // Obtain the string pointer
- Const jchar * jstr = env-> GetStringCritical (j_msg, NULL );
- // Convert to a wide string
- Wstring wstr (const wchar_t *) jstr );
- // Release the pointer
- Env-> ReleaseStringCritical (j_msg, jstr );
- */
- /** Method 2 END */
-
- /** Method 3: START */
- // Obtain the length of the string
- Jsize len = env-> GetStringLength (j_msg );
- // Generate a string pointer with the length of len
- Jchar * jstr = new jchar [len + 1];
- // The string in C ++ ends with '', otherwise unexpected characters will be output
- Jstr [len] = l '';
- // Copy the string j_msg to jstr.
- Env-> GetStringRegion (j_msg, 0, len, jstr );
- // Convert to a wide string
- Wstring wstr (const wchar_t *) jstr );
- // Release the pointer
- Delete [] jstr;
- /** Method 3 END */
-
- // Reverse the string
- Reverse (wstr. begin (), wstr. end ());
- // Obtain the new string in reverse order
- Jstring j_new_str = env-> NewString (const jchar *) wstr. c_str (), (jint) wstr. size ());
- // Set the new string to the variable
- Env-> SetObjectField (obj, fid_msg, j_new_str );
- } Three methods are used to implement the function. Note that another method is to convert const jchar * To wstring, because the reverse method accepts the wstring parameter. The running result in Eclipse is as follows:
Here we will talk about the content of this article, and the content later will be more exciting!