• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

char* equivalent of JNI?

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic