Meaningless Drivel is fun!*
The moose likes Other JSE/JEE APIs and the fly likes char* equivalent of JNI? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Other JSE/JEE APIs
Bookmark "char* equivalent of JNI?" Watch "char* equivalent of JNI?" New topic
Author

char* equivalent of JNI?

arya putra
Greenhorn

Joined: Apr 27, 2004
Posts: 16
Hi All,
I have a doubt about the C equivalent of char* in JNI.
I am passing a char* pointer from one JNI function call and accepting it as an jbojectArray. But it doesnt seem to work fine. Here is the snapshot .
Pls pass on your comments.Thanks.
int i;
jsize len;
jobject myobj;
char returnString[2048];
char *str = returnString;
len=(*env)->GetArrayLength(env,ptr);
for (i=0;i<len;i++) {
myobj=(*env)->GetObjectArrayElement(env,ptr,i);
str=(*env)->GetStringUTFChars(env,myobj,0);
}
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

You'll have to elaborate a bit on "But it doesnt seem to work fine." before we can help you understand. Are you expecting "returnString" to be involved in this somehow?


[Jess in Action][AskingGoodQuestions]
arya putra
Greenhorn

Joined: Apr 27, 2004
Posts: 16
Hi,
"This doesnt seem to work fine" means that I am not getting the expected return value in the string. I print it in the called function and also in the calling function. Both have different values. In case of calling function it shows me junk values. I hope I make myself clear.
Thanks again.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

I can't tell what you mean by the called or calling functions, and you didn't answer my question about "returnString". Are you printing returnString? Because that's going to contain garbage. "str" is going to point to the characters you want. Also, I don't see where you're freeing "str" with ReleaseStringUTFChars, which is quite important.
arya putra
Greenhorn

Joined: Apr 27, 2004
Posts: 16
Hi There,
I think it would be convenient this way.
I make a function call like this -
(Java_Lx200_readn(env,obj,lx200PortFD,inputStr,1,1)
where inputStr is a char array of size 2048. The same inputStr I have to use from the place I called readn().
JNIEXPORT jint JNICALL
Java_Lx200_readn(JNIEnv *env,jobject obj,jint fd,jobjectArray ptr,jint nbytes,jint sec)
{
int stat;
int nleft, nread;
nleft = nbytes;
int i;
jsize len;
jobject myobj;
char returnString[2048];
char *str;
str = returnString;
len=(*env)->GetArrayLength(env,ptr);
for (i=0;i<len;i++) {
myobj=(*env)->GetObjectArrayElement(env,ptr,i);
str=(*env)->GetStringUTFChars(env,myobj,0);
}
while (nleft > 0) {
stat = Java_Lx200_lx200stat(env,obj,fd,sec,0);
if (stat <= 0 ) break;
nread = read (fd, ptr, nleft);
if (nread <= 0) break;
nleft -= nread;
str += nread;
}
return (nbytes - nleft);
}
I hope I make some sense.
Thanks for your efforts.
Regards.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: char* equivalent of JNI?
 
Similar Threads
JNI problem
JNI : getting an array of arrays of strings back to java
JNI CallObjectMethod casting - Urgent
Runtime.getRuntime().exec("sh export LC_ALL=C") fails!!!!!!!!!!
JNI_CreateJavaVM returns error