开发者

09▀30≈ß#@÷g gets displayed when i had not passed this String

开发者 https://www.devze.com 2023-04-02 14:53 出处:网络
I am trying to pass a String to a native c function but the String gets pri开发者_开发知识库nted as 09▀30≈ß#@÷g for the String Type a String : . I don\'t know what the problem is .

I am trying to pass a String to a native c function but the String gets pri开发者_开发知识库nted as 09▀30≈ß#@÷g for the String Type a String : . I don't know what the problem is .

This is the code that i have used to do this :

JAVA CODE

class Prompt {

private native String myGetLine( String prompt ); // native method that prints a prompt and reads a line 

public static void main( String args[] ) {
Prompt prompt = new Prompt();
String input = prompt.myGetLine( "Type a String : ");
System.out.println("User Typed : " + input);
}

static {
 System.loadLibrary("StringPassing"); // StringPassing.dll
 }
}

C CODE

#include <jni.h>
#include <stdio.h>
#include "D:\UnderTest\JNI\StringPassing\Debug\Prompt.h"

JNIEXPORT jstring JNICALL Java_Prompt_myGetLine(JNIEnv *env, jobject obj, jstring str) {
char buf[128];
const jbyte *str_JVM;
str_JVM = (*env) -> GetStringUTFChars( env , str , NULL);
if( str_JVM == NULL ) return NULL;
printf("%s ",str);
(*env)->ReleaseStringUTFChars( env , str , str_JVM);
scanf("%s",buf);
return (*env)->NewStringUTF( env , buf);
}

The screen after the program ends looks like :

D:\UnderTest\JNI\StringPassing\Debug>java Prompt
09▀30≈ß#@÷gV
User Typed : V

Why don't i get the string Type a String : on cmd when i run the program?

Also: my compiler underlines (*env) and points out that Expression must have pointer type. But when I compile I don't get any errors. Why is that and what is it?


You pass str to printf when I'm pretty sure that you wanted to pass str_JVM to it instead.

str_JVM = (*env) -> GetStringUTFChars( env , str , NULL);
if( str_JVM == NULL ) return NULL;
printf("%s ",str_JVM);


As to the 2nd part of your question, JNIEnv is declared as a parameter to Java_Prompt_myGetLine as this type:

JNIEnv *env

That translates in English as "Declare a variable called env that is a pointer to an object of type JNIEnv". The operator -> in C and C++ is used for accessing members of a struct or class instance from a pointer, however, the left hand side of the -> operator is expected to be a pointer. This operator "dereferences" the pointer. Referencing a pointer like this: *env means "give me the object pointed to by env". This also dereferences the pointer.

The problem that your IDE is telling you is that lines such as this (there are 3 occurrences in your code above) are technically illegal code, because the -> operator expects a pointer on the left-hand-side, but you have already dereferenced the pointer using (*env), so your code is passing it an object instead of a pointer:

return (*env)->NewStringUTF( env , buf);

Your compiler is actually very forgiving by letting this compile anyway. To correct it, change the line to either:

return env->NewStringUTF( env , buf);

-OR-

return (*env).NewStringUTF( env , buf);

Usually the first line is the more common usage (it looks cleaner).

0

精彩评论

暂无评论...
验证码 换一张
取 消