• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Pass jbyte to a C function in JNI

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have a program,which reads a String from the user.

char buf[128];
const jbyte *jnistr;
jsize strsize;
jnistr = (*env)->GetStringUTFChars(env,prompt,NULL);
if(jnistr==NULL){
return;
}
strsize = (*env)->GetStringLength(env,prompt);

testBuffer(*jnistr,strsize);


After converion, it is in the varibale jnistr ,which is a const jbyte.

My confusion starts here:
How can I pass the const jbyte to a C function, so that the whole string is accessible.

When I try to return the jnistr, and accept it as a char parameter in the C function testBuffer(), I can only read the first letter of the string and nothing beyond that.


Thank You,
Ann
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess that you have written your testBuffer() C function to accept a jbyte as its first argument. It should be using a pointer, specifically a jbyte* .

I seem to remember you saying you're new to C. I think that the issue you have is related to the fact that C is more lax than Java about arrays. Your jnistr pointer is actually a pointer to the first element of an array of jbyte. But by dereferencing it, you are passing just the value of the first element of the array.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic