Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

char* equivalent of JNI?

 
arya putra
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
arya putra
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic